2014-09-03 146 views
3

有什麼辦法可以禁用只能在jQuery中向下滾動嗎?禁用向下滾動jQuery

非常感謝你提前

+0

嘗試$(文件)。 scroll(function(){return false;}); – 2014-09-03 08:40:46

+0

試試這個問題:http://stackoverflow.com/questions/11940351/jquery-tools-touch-horizo​​ntal-only-disable-vertical-touch – 2014-09-03 08:41:06

+0

我只想禁用向下滾動,而不是向上滾動 – Genzotto 2014-09-03 08:46:26

回答

5
$('html, body').bind('DOMMouseScroll mousewheel MozMousePixelScroll', function(e) { 
    var scrollTo = 0; 

    if (e.type == 'mousewheel') { 
     scrollTo = (e.originalEvent.wheelDelta * -1); 
    } 
    else if (e.type == 'DOMMouseScroll') { 
     // scrollTo = 20 * e.originalEvent.detail; // turns out, this sometimes works better as expected... 
     scrollTo = e.originalEvent.detail; 
    } 

    if (scrollTo > 0) { 
    e.preventDefault(); 
    return false; 
    } 
}); 

Here you have a working fiddle

+0

我做了體驗'scrollTo = 20 * e.originalEvent.detail;'需要一點調整,我不知道爲什麼我把它乘以20 :) – Alex 2014-09-03 09:01:07

+0

然而你可以看看https://developer.mozilla.org/en -US/docs/Web/Events/mousewheel並根據您的需要調整它 – Alex 2014-09-03 09:30:27

+0

您忘記了觸摸事件。但你的回答很酷。 – 2014-09-03 09:46:04

1

繼Alex的答案,我設法做到這一點,在未來的方式:

$(window).on('DOMMouseScroll mousewheel MozMousePixelScroll', function(e) { 
    var direction = 0; 
    if (e.type == 'mousewheel') { 
     direction = (e.originalEvent.wheelDelta * -1); 
    } else if (e.type == 'DOMMouseScroll') { 
     direction = e.originalEvent.detail; 
    } 
    var divHeight = $("#myDiv").height(); 
    var scrollTop = $(this).scrollTop(); 
    if (direction > 0 && scrollTop > divHeight - window.innerHeight) { 
     e.preventDefault(); 
     return false; 
    } 
});