700字范文,内容丰富有趣,生活中的好帮手!
700字范文 > jQuery JS 禁用滚动条和启用滚动条

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

时间:2020-10-29 06:55:11

相关推荐

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

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

本内容不代表本网观点和政治立场,如有侵犯你的权益请联系我们处理。
网友评论
网友评论仅供其表达个人看法,并不表明网站立场。