简体   繁体   English

如何禁用滚动而不使用溢出:隐藏和位置:固定?

[英]how to disable scroll without using overflow:hidden & position:fixed?

Give me solution to disable scroll of the page. 给我解决方案以禁用页面滚动。 Right now I'm using 现在我正在使用

overflow:hidden

or 要么

position:fixed

I use given code as well: 我也使用给定的代码:

var keys = { 37: 1, 38: 1, 39: 1, 40: 1 };

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

function preventDefaultForScrollKeys(e) {
    if (keys[e.keyCode]) {
        preventDefault(e);
        return false;
    }
}

function disableScroll() {
    if (window.addEventListener) // older FF
        window.addEventListener('DOMMouseScroll', preventDefault, false);
    window.onwheel = preventDefault; // modern standard
    window.onmousewheel = document.onmousewheel = preventDefault; // older browsers, IE
    window.ontouchmove = preventDefault; // mobile
    document.onkeydown = preventDefaultForScrollKeys;
}

function enableScroll() {
    if (window.removeEventListener) window.removeEventListener('DOMMouseScroll', preventDefault, false);
    window.onmousewheel = document.onmousewheel = null;
    window.onwheel = null;
    window.ontouchmove = null;
    document.onkeydown = null;
}

but it is not giving me proper output. 但这没有给我适当的输出。 My page getting fluctuate when I use above code... I don't want to use any library. 当我使用上述代码时,我的页面会起伏……我不想使用任何库。

试着做

window.onscroll = function () { window.scrollTo(0, 0); };

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM