1

我想禁用滚动事件监听器一段时间

我在这里实现了滑块

我的代码在这里

$(window).bind('DOMMouseScroll', function (e) {
            if (e.originalEvent.detail > 0) {
                //scroll down
                if(swiperV.activeSlide<(total-1)){
                    console.log('down');
                    swiperV.swipeNext();
                    console.log('down-after');
                }
            } else {
                //scroll up
                if(swiperV.activeSlide>0){
                    console.log('up');
                    swiperV.swipePrev();
                    console.log('up-after');
                }
            }
            //prevent page fom scrolling
            return false;
        });

现在我想禁用滚动,直到幻灯片改变

当我调用 swiperV.swipeNext(); 它会改变幻灯片。

4

5 回答 5

0

您可以通过以下方式取消绑定 eventlistner

$(window).unbind('DOMMouseScroll');

您可以通过方法取消注册任何事件处理程序$.unbind()

于 2013-07-20T06:44:32.980 回答
0

命名事件处理程序,然后您可以在其主体中引用它,如下所示:

$(window).bind('DOMMouseScroll', function SomeName(e) {
    if (SomeReason) $(window).unbind('DOMMouseScroll', SomeName);
}
于 2013-07-20T06:44:49.553 回答
0

您可以unbind使用事件列表器,然后swiperV.swipeNext();再次bind使用它:-

$(window).bind('DOMMouseScroll', function (e) {
     $(window).unbind('DOMMouseScroll');
            if (e.originalEvent.detail > 0) {                    
                if(swiperV.activeSlide<(total-1)){
                    console.log('down');
                    swiperV.swipeNext();                  
                    console.log('down-after');
                    $(window).bind('DOMMouseScroll');
                }
            } 
            else {                    
                if(swiperV.activeSlide>0){
                    console.log('up');
                    swiperV.swipePrev();
                    console.log('up-after');
                    $(window).bind('DOMMouseScroll');
                }
            }                
            return false;
        }); 
于 2013-07-20T06:54:47.477 回答
0

对于 IE、Opera、Safari,您可以使用

$(window).bind('mousewheel', function (e) { //your code });
于 2013-07-20T07:43:08.603 回答
0

所以最后使用

火狐

$(window).bind('DOMMouseScroll', function (e) {
     $(window).unbind('DOMMouseScroll');
            if (e.originalEvent.detail > 0) {                    
                if(swiperV.activeSlide<(total-1)){                    
                    swiperV.swipeNext();                                      
                    $(window).bind('DOMMouseScroll');
                }
            } 
            else {                    
                if(swiperV.activeSlide>0){                    
                    swiperV.swipePrev();                    
                    $(window).bind('DOMMouseScroll');
                }
            }                
            return false;
        });

对于其他

    $(window).bind('mousewheel', function (e) {
$(window).unbind('mousewheel');
                    if (e.originalEvent.wheelDelta < 0) {
                        //scroll down total
                        if(swiperV.activeSlide<(total-1)){                            
                            swiperV.swipeNext();  
                          $(window).bind('mousewheel');
                        }
                    } else {
                        //scroll up
                        if(swiperV.activeSlide>0){                            
                            swiperV.swipePrev();   
                          $(window).bind('mousewheel');
                        }
                    }
                    //prevent page fom scrolling
                    return false;
                });

感谢 adeneo 和 Ishan Jain

于 2013-07-20T07:58:21.943 回答