有时候禁用滚动事件监听器

时间:2013-07-20 06:34:45

标签: javascript jquery scroll

我想暂时禁用滚动事件监听器

我在这里实现了滑块

我的代码在这里

$(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();它会改变幻灯片。

5 个答案:

答案 0 :(得分:0)

您可以通过

取消绑定eventlistner
$(window).unbind('DOMMouseScroll');

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

答案 1 :(得分:0)

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

$(window).bind('DOMMouseScroll', function SomeName(e) {
    if (SomeReason) $(window).unbind('DOMMouseScroll', SomeName);
}

答案 2 :(得分: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;
        }); 

答案 3 :(得分:0)

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

$(window).bind('mousewheel', function (e) { //your code });

答案 4 :(得分:0)

最后使用

for firefox

$(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