pointer events on click but not on scroll

喜你入骨 提交于 2019-12-07 02:44:36

问题


Is it possible to allow click but not scroll events?

pointer-events: none;

Will disable both types of inputs, I would like to disable only scroll. Any other ideas for workarounds?


回答1:


Do it with javascript:

function noScroll(event) {
    event = event || window.event;
    if (event.preventDefault) {
        event.preventDefault();
    }
    event.returnValue = false;
    return false;
}

// disable scolling on the whole window:
if (!window.addEventListener) {
    // old IE only
    window.attachEvent("onscroll", noScroll);
} else {
    // Firefox only
    window.addEventListener("DOMMouseScroll", noScroll);
    // anything else
    window.addEventListener("scroll", noScroll);
}

// disable scrolling on a single element:
var el = document.getElementById("elementID");

if (!el.addEventListener) {
    el.attachEvent("onscroll", noScroll);
} else {
    el.addEventListener("DOMMouseScroll", noScroll);
    el.addEventListener("scroll", noScroll);
}

That should do the trick.




回答2:


Add this css:

.stopScroll{
height:100%;
overflow:hidden;
}

Then in jQuery:

$('body').addClass('stopScroll');

Look at the fiddle:https://jsfiddle.net/26dct8o3/1/

Would help if you are looking for this. Otherwise let me know in comments if this is not what you want.



来源:https://stackoverflow.com/questions/40091283/pointer-events-on-click-but-not-on-scroll

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!