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();
});