jQuery JS 禁用滚动条和启用滚动条

来源:互联网 发布:程序员 网络工程师 编辑:程序博客网 时间:2024/05/29 23:46

CSS:

.lock {            overflow: hidden;            touch-action: none;        }            .lock body {                overflow: hidden;                touch-action: none;                margin-right: 17px;            }            .lock.mobile body {                margin-right: 0;            }

JS:

var methods = {            /** 禁用滚动*/            forbidScroll: function () {                document.querySelector("html").classList.add("lock");                window.addEventListener("mousewheel", this.forbidScroll);                window.addEventListener("touchmove", this.forbidScroll, { passive: false });            },            /** 启用滚动*/            enabledScroll: function () {                document.querySelector("html").classList.remove("lock");                window.removeEventListener("mousewheel", this.forbidScroll);                window.removeEventListener("touchmove", this.forbidScroll, { passive: false });            }        }

调用方法:

    $("#test1").click(function () {        methods.forbidScroll();    });    $("#test2").click(function () {        methods.endabledScroll();    });


原创粉丝点击