0

In my nav bar, i have a scrollable dropdown list.Once user hovers over it, it appears and he can hover scroll up and down to select a link. It works perfectly in all desktop browsers, but on iPad once the dropdown is shown, it gets stuck and the user cant scroll up and down.

Here is the JS i used:

var maxHeight = 400; $(function(){

$(".dropdown > li").hover(function() {

     var $container = $(this),
         $list = $container.find("ul"),
         $anchor = $container.find("a"),
         height = $list.height() * 1.1,       // make sure there is enough room at the bottom
         multiplier = height / maxHeight;     // needs to move faster if list is taller

    // need to save height here so it can revert on mouseout            
    $container.data("origHeight", $container.height());

    // so it can retain it's rollover color all the while the dropdown is open
    $anchor.addClass("hover");

    // make sure dropdown appears directly below parent list item    
    $list
        .show()
        .css({
            paddingTop: $container.data("origHeight")
        });

    // don't do any animation if list shorter than max
    if (multiplier > 1) {
        $container
            .css({
                height: maxHeight,
                overflow: "hidden"
            })
            .mousemove(function(e) {
                var offset = $container.offset();
                var relativeY = ((e.pageY - offset.top) * multiplier) - ($container.data("origHeight") * multiplier);
                if (relativeY > $container.data("origHeight")) {
                    $list.css("top", -relativeY + $container.data("origHeight"));
                };
            });
    }

}, function() {

    var $el = $(this);

    // put things back to normal
    $el
        .height($(this).data("origHeight"))
        .find("ul")
        .css({ top: 0 })
        .hide()
        .end()
        .find("a")
        .removeClass("hover");

});  

});

I dont know did i explained it properly (english isnt my first language), but you can see what i mean on https://jsfiddle.net/x7L4zgfk/1/

Medvjed Jedan
  • 75
  • 1
  • 1
  • 7
  • possible duplicate of [JavaScript - mousemove event not triggered on iPad/iPhone](http://stackoverflow.com/questions/14071285/javascript-mousemove-event-not-triggered-on-ipad-iphone) – twill Jun 11 '15 at 17:39

1 Answers1

1

Duplicate from JavaScript - mousemove event not triggered on iPad/iPhone

Just use jquery.event.move. Move events provide an easy way to set up press-move-release interactions on mouse AND touch devices. So you don't have to worry which device your users have.

Source and examples: http://stephband.info/jquery.event.move/

Community
  • 1
  • 1
twill
  • 755
  • 4
  • 15
  • Sorry for this, but as complete JS newbie, can you give me little more informations? Would be very gratefull! – Medvjed Jedan Jun 11 '15 at 17:44
  • You will need to get the library from the link above and add to your site (https://github.com/stephband/jquery.event.move/blob/master/js/jquery.event.move.js) - then replace `.mousemove(function(e) { // etc etc })` With `.bind('move', function(e) {// etc etc })` – twill Jun 11 '15 at 17:48
  • Just tried it, and it doesnt work, the dropdown isnt scrollable anymore.Sorry for boring you, but can you take a look? http://jsfiddle.net/x7L4zgfk/6/ – Medvjed Jedan Jun 11 '15 at 17:57
  • Actually, my bad, it does work on iPad now, but not on desktop as it was before http://jsfiddle.net/x7L4zgfk/ – Medvjed Jedan Jun 11 '15 at 17:59