【问题标题】:Fire scroll event after a certain scroll offset or scrolling time在某个滚动偏移或滚动时间后触发滚动事件
【发布时间】:2013-10-31 00:57:19
【问题描述】:

我正在使用fullPage.js JQuery 插件(参见演示here)。该插件定义了全窗口大小的部分,并在用户向上或向下滚动时从“幻灯片”部分滚动到“幻灯片”。

我正在尝试修改它,以便滚动事件不会在滚动开始时立即触发,而是在短时间内或一定滚动量后触发,以防止用户意外滑动几个像素,这将触发滚动事件并滚动到下一张幻灯片。

例如,如果用户滚动偏移 100px,则滚动到下一张幻灯片。

Herefullpage.js 代码。我认为我将不得不修改此功能以获得我想要的:

//when scrolling...
$(window).scroll(function(e){
        if(!options.autoScrolling){
                var currentScroll = $(window).scrollTop();

                var scrolledSections = $('.section').map(function(){
                        if ($(this).offset().top < (currentScroll + 100)){
                                return $(this);
                        }
                });

                //geting the last one, the current one on the screen
                var currentSection = scrolledSections[scrolledSections.length-1];

                //executing only once the first time we reach the section
                if(!currentSection.hasClass('active')){
                        var yMovement = getYmovement(currentSection);

                        $('.section.active').removeClass('active');
                        currentSection.addClass('active');

                        var anchorLink  = currentSection.data('anchor');
                        $.isFunction( options.onLeave ) && options.onLeave.call( this, currentSection.index('.section'), yMovement);

                        $.isFunction( options.afterLoad ) && options.afterLoad.call( this, anchorLink, (currentSection.index('.section') + 1));

                        activateMenuElement(anchorLink);        
                }
        }                                        
});

但我真的不知道该怎么做... 我想我可能应该在滚动开始和滚动结束时获取滚动偏移量/时间,获取差异,然后根据差异滚动到当前或下一张幻灯片,但我不知道如何在此函数中实现这一点以及如何获取滚动结束事件。

而且我不确定是否使用“滚动结束事件”,因为如果用户想要向下或向上滚动,脚本不应该等待用户停止滚动以滚动到它必须滚动的位置..

也许我应该在滚动过程中获得滚动偏移量,当偏移量 > x 时滚动到下一个,如果滚动停止并且偏移量

【问题讨论】:

    标签: javascript jquery jquery-plugins scroll fullpage.js


    【解决方案1】:

    我猜您在使用笔记本电脑及其触控板而不是鼠标滚轮时遇到了问题。我对么? 或者,可以使用 Apple 鼠标,它在滚动方面就像一个触控板。

    好吧,只是让您知道,问题不在于$(window).scroll(function(e){ 函数,而在于MouseWheelHandler().scroll 函数仅在选项autoScrolling 设置为false 时使用,如您在第一个条件中所见。

    也就是说,MouseWheelHandler() 函数在每次检测到鼠标滚轮或触控板上的任何滚动时都会被触发。避免在第一次滚动时触发它的方法是为其创建一个计数器。

    您必须小心,因为触控板中的滚动比普通鼠标滚轮更敏感,因此,在定义您要使用的最小滚动数之前,请在这两种设备上尝试。

    我已经定义了一个设置为 0 的新变量:

    var scrolls = 0;
    

    然后,在MouseWheelHandler() 中,我添加了一个新条件scrolls &gt; 2

    if(options.autoScrolling && scrolls>2){
    

    这样,我们只会在滚动超过 3 次时触发事件,我相信这对于鼠标滚轮来说已经足够了。

    如果我们不输入条件,那么我们增加变量:

    }else{
        scrolls++;    
    };
    

    这是工作演示:http://jsfiddle.net/prYUq/2/

    这里是完整的修改功能:

    var scrolls = 0;
    
     function MouseWheelHandler() {
         return function (e) {
             if (options.autoScrolling && scrolls > 2) {
                 // cross-browser wheel delta
                 e = window.event || e;
                 var delta = Math.max(-1, Math.min(1, (e.wheelDelta || -e.detail)));
                 e.wheelData
    
                 console.log("e.wheelDelta: " + e.wheelDelta);
                 console.log("-e.detail: " + e.detail);
    
                 if (!isMoving) { //if theres any #
                     var scrollable = $('.section.active').find('.scrollable');
    
                     //scrolling down?
                     if (delta < 0) {
                         if (scrollable.length > 0) {
                             //is the scrollbar at the end of the scroll?
                             if (isScrolled('bottom', scrollable)) {
                                 $.fn.fullpage.moveSlideDown();
                             } else {
                                 return true; //normal scroll
                             }
                         } else {
                             $.fn.fullpage.moveSlideDown();
                         }
                     }
    
                     //scrolling up?
                     else {
                         if (scrollable.length > 0) {
                             //is the scrollbar at the start of the scroll?
                             if (isScrolled('top', scrollable)) {
                                 $.fn.fullpage.moveSlideUp();
                             } else {
                                 return true; //normal scroll
                             }
                         } else {
                             $.fn.fullpage.moveSlideUp();
                         }
                     }
                 }
                 scrolls = 0;
    
                 return false;
             } else {
                 scrolls++;
             };
         }
     }
    

    如果您对该插件有具体问题或要求,请不要忘记此插件在 github 上的问题论坛:https://github.com/alvarotrigo/fullPage.js/issues?state=open

    我希望它有所帮助。

    【讨论】:

    • 这正是我想要的!非常感谢你:)
    • @Alvaro,如果我使用来自 'jsfiddle.net/prYUq/2' 的小提琴代码,插件会在某处中断...但是对函数 MouseWheelHandler() 应用上述建议的更改效果很好,谢谢
    猜你喜欢
    • 2012-02-14
    • 2013-01-08
    • 2015-02-09
    • 1970-01-01
    • 1970-01-01
    • 2015-03-16
    • 1970-01-01
    • 2013-06-24
    • 1970-01-01
    相关资源
    最近更新 更多