Skip to content

Instantly share code, notes, and snippets.

@kobitoDevelopment
Last active May 23, 2022 08:58
Show Gist options
  • Select an option

  • Save kobitoDevelopment/d9563064ef43b64c3511b8d02ad1ac7c to your computer and use it in GitHub Desktop.

Select an option

Save kobitoDevelopment/d9563064ef43b64c3511b8d02ad1ac7c to your computer and use it in GitHub Desktop.
<p class="JS-scrollDisable">スクロール禁止</p>
<p class="JS-scrollAble">スクロール可能</p>
const triggerScrollAble = document.querySelector(".JS-scrollAble");
const triggerScrollDisable = document.querySelector(".JS-scrollDisable");
function handle(event) {
event.preventDefault();
}
/* {passive:false}を記述する事で、ブラウザに「この関数内ではevent.preventDefault()を使用する」事を宣言 */
window.addEventListener("load", function () {
document.addEventListener("touchmove", handle, { passive: false });
document.addEventListener("mousewheel", handle, { passive: false });
});
triggerScrollDisable.addEventListener("click", function () {
document.addEventListener("touchmove", handle, { passive: false });
document.addEventListener("mousewheel", handle, { passive: false });
});
triggerScrollAble.addEventListener("click", function () {
document.removeEventListener("touchmove", handle, { passive: false });
document.removeEventListener("mousewheel", handle, { passive: false });
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment