jQuery鼠标滚轮不支持触摸板?

3

我正在使用流行的mousewheel插件来模拟像这个网站一样的全屏滚动效果。

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();
    });
});

它的表现很好,但当使用触摸板时却不行。使用触摸板时,无论我尝试多慢地滚动,它都会直接滚动到页面底部。

4个回答

4

适用于我的笔记本电脑触摸板。然而,这是已知的问题,您是否尝试使用去抖动(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); });

这个问题已经在这里报告了:https://github.com/brandonaaron/jquery-mousewheel/issues/36。你会在那里得到多种解决方案。希望对你有所帮助。自助自愉 :-)。
和平,
Rahul

那么我应该将我的函数(在原帖中发布)放在“如果可以滚动”的内部吗? - eozzy
几乎可以工作,但仍然太快了,不过如果我轻触它,它就可以从一个部分到另一个部分,所以至少看起来是在工作。 - eozzy
哦,它破坏了正常的鼠标滚动。 - eozzy
让我看一下。理想情况下不应该出问题。 - Rahul Patil
这里的oldDate是什么?它没有被定义。 - novellino
显示剩余2条评论

1

我使用date()解决了我的跟踪问题,因此如果由我设置的某个时间还未到来,用户将无法滚动。

以下是我仅使用“wheel”函数的代码片段:

  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(); 
        }
  });

1

对于任何其他发现此问题的人,“mousewheel”事件已被弃用。 https://developer.mozilla.org/en-US/docs/Web/Events/wheel

相反,使用“wheel”事件 您可以在滚轮事件上使用“originalEvent”属性来获取您期望的属性。(deltaX,deltaY等...)


这仅适用于Firefox。 - user3325783
好的,Firefox是用户的大部分,这意味着你必须为它编写代码 :) - Gambai

0
我使用了下划线的防抖函数来解决这个问题。这是我使用的代码。
 $('...').mousewheel(_.debounce(function(event) {
   event.preventDefault();

   if(event.originalEvent.deltaY < 0){
      /* do your stuff when scrolling up */

   }else {
      /* do your stuff when scrolling down */

}, 40,true));

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接