Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

jQuery Mousewheel doesn't support trackpad?

I'm using the popular mousewheel plugin to to emulate the fullpage scroll like this website.

jQuery(function() {
    var top = 0,
        viewport = jQuery(window).height(),
        step = jQuery(".home .l-section:first").outerHeight(),
        body = jQuery.browser.webkit ? jQuery('body') : jQuery('html'),
        wheel = false;
    jQuery('body').mousewheel(function(event, delta) {
        wheel = true;
        if (delta < 0) {
            top = (top + viewport) >= jQuery(document).height() ? top : top += step;
            body.stop().animate({
                scrollTop: top
            }, 400, function() {
                wheel = false;
            });
        } else {
            top = top <= 0 ? 0 : top -= step;
            body.stop().animate({
                scrollTop: top
            }, 400, function() {
                wheel = false;
            });
        }
        return false;
    });
    jQuery(window).on('resize', function(e) {
        viewport = jQuery(window).height();
        step = jQuery(".home .l-section:first").outerHeight();
    });
    jQuery(window).on('scroll', function(e) {
        if (!wheel) top = jQuery(this).scrollTop();
    });
});

It works great but NOT when using the trackpad. Using trackpad, it just scrolls to the bottom of the page no matter how slowly I try to scroll.

like image 937
eozzy Avatar asked May 21 '26 21:05

eozzy


2 Answers

Works well with my laptop's trackpad. However, this is known issue, did you try using debounce?

function mouseHandle(event) {
    newDate = new Date();
    var scrollAllowed = true;

    if( wheel < 10 && (newDate.getTime()-oldDate.getTime()) < 100 ) {
        scrollPos -= event.deltaY*(10-wheel);
        wheel++;
    }
    else {
        if( (newDate.getTime()-oldDate.getTime()) > 100 ) {
            wheel = 0;
            scrollPos -= event.deltaY*60;
        }
        else {
            scrollAllowed = false;
        }
    }

    oldDate = new Date();

    if( scrollAllowed ) {
        // do your stuff here
    }
}

//Apply this effect with jQuery On event
$('body').on('mousewheel', function(event) { mouseHandle(event); });

This is already reported issue here : https://github.com/brandonaaron/jquery-mousewheel/issues/36

You will get multiple solutions there. Hope that helps. Help yourself :-)

Peace,

Rahul

like image 77
Rahul Patil Avatar answered May 24 '26 10:05

Rahul Patil


I solved my problem tracking with date(), so the user is not able to scroll any more if a certain time set by me, is not in the past.

Here is my snippet just using the 'wheel' function

  var last_time = new Date(); 
  $(window).bind('wheel', function(event) {
      var now = new Date();
      if((now - last_time) >= 1200)//Miliseconds
      {
        if (event.originalEvent.deltaY <= 0) {

            //Go up
          }
          else {
            //Go down
          }

          last_time = new Date(); 
        }
  });
like image 28
Simon Berton Avatar answered May 24 '26 12:05

Simon Berton