防止滚动鼠标滚轮,但不滚动事件。 JavaScript的 [英] Prevent mouse wheel scrolling, but not scrollbar event. JavaScript
问题描述
我需要制作一个网页才能通过滚动条进行滚动。我试图找到如何捕捉滚动条事件,但因为我看到它是不可能的。目前我使用这个函数:
function preventDefault(e){
e = e || window.event;
if(e.preventDefault){
e.preventDefault();
} else {
e.returnValue = false;
}
}
函数轮(e){
preventDefault(e);
}
function disable_scroll(){
if(window.addEventListener){
window.addEventListener('DOMMouseScroll',wheel,false);
}
window.onmousewheel = document.onmousewheel = wheel;
$ b 但它们在我的情况下并不是非常有用,因为它们会阻止所有滚动事件。你有什么想法?我正在考虑3天,我没有找到任何答案(和问题)。
解决方案您可以试试这个;
window.onwheel = function(){return false; }
这将防止用鼠标滚轮滚动窗口。如果< div>的内容或其他元素是可滚动的,你可以像这样防止它;
document.getElementById('{element-id}')。 onwheel = function(){return false; }
干杯...
I need to make a webpage scrollable only by scrolling bar. I have tried to find how to catch scroll bar event, but as i see it is impossible. Currently i use this functions:
function preventDefault(e) {
e = e || window.event;
if (e.preventDefault) {
e.preventDefault();
} else {
e.returnValue = false;
}
}
function wheel(e) {
preventDefault(e);
}
function disable_scroll() {
if (window.addEventListener) {
window.addEventListener('DOMMouseScroll', wheel, false);
}
window.onmousewheel = document.onmousewheel = wheel;
}
But they are not very useful in my situation, because they block all scroll events. Do you have any ideas? I am thinking about it 3 days already and i didn't find any answer (and questions also). Thanks!
解决方案 You can try this;
window.onwheel = function(){ return false; }
This will prevent window scrolling with mouse wheel. If a content of <div> or other element is scrollable you can prevent it too like this;
document.getElementById('{element-id}').onwheel = function(){ return false; }
Cheers...
这篇关于防止滚动鼠标滚轮,但不滚动事件。 JavaScript的的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!