javascript スクロールを禁止する

javascript スクロールを禁止する

javascriptで、スクロールを禁止するサンプルコードを記述してます。

環境

  • OS windows11 pro 64bit
  • Apache 2.4.43
  • ブラウザ chrome 106.0.5249.103

スクロール禁止

スクロールを禁止するには、イベント「touchmove」と「mousewheel」に「preventDefault」を使用して、禁止します。

function handle(event) {
    event.preventDefault();
}

window.onload = function() {
  document.addEventListener('touchmove', handle, { passive: false });
  document.addEventListener('mousewheel', handle, { passive: false });
}

実際に禁止に設定して、解除もしてみます。

<button id="btn" onclick="hoge()">解除</button>

<div id="sample" 
style="
height: 1500px;
width: 200px;
border: 4px solid;
border-color: green;
"
>mebee</div>

<script>

function handle(event) {
    event.preventDefault();
}

window.onload = function() {
  document.addEventListener('touchmove', handle, { passive: false });
  document.addEventListener('mousewheel', handle, { passive: false });
}

function hoge() {
  document.removeEventListener('touchmove', handle, { passive: false });
  document.removeEventListener('mousewheel', handle, { passive: false });
}

</script>

実行結果

また、以下のように記述すればスクロールを禁止にはできますが、無名関数は「removeEventListener」ができないので、解除はできません。

window.onload = function() {
  document.addEventListener('touchmove', function(e){e.preventDefault()}, { passive: false });
  document.addEventListener('mousewheel', function(e){e.preventDefault()}, { passive: false });
}

function hoge() {
  document.removeEventListener('touchmove', function(e){e.preventDefault()}, { passive: false });
  document.removeEventListener('mousewheel', function(e){e.preventDefault()}, { passive: false });
}

サンプルコード

以下は、
「実行」ボタンをクリックした際に、「禁止」ボタンをクリックすればスクロールを禁止して、「解除」をクリックされば解除されるサンプルコードとなります。

※cssには「tailwind」を使用して、アロー関数で関数は定義してます。

<!DOCTYPE html>
<html lang="ja">

<head>
  <meta charset="utf-8">
  <title>mebeeサンプル</title>
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <script src="https://cdn.tailwindcss.com"></script>
</head>

<script>

  const handle = (event) => {
    event.preventDefault();
  }


  window.onload = () => {

    btn1.onclick = () => {
      document.addEventListener('touchmove', handle, { passive: false });
      document.addEventListener('mousewheel', handle, { passive: false });
    }

    btn2.onclick = () => {
      document.removeEventListener('touchmove', handle, { passive: false });
      document.removeEventListener('mousewheel', handle, { passive: false });
    }

  }

</script>

<body>
  <div class="container mx-auto my-56 w-64 px-4">

    <div id="sample" class="flex flex-col justify-center">

      <button id="btn1"
        class="mb-2 md:mb-0 bg-transparent hover:bg-pink-500 text-pink-700 font-semibold hover:text-white py-2 px-4 border border-pink-500 hover:border-transparent rounded">
        禁止
      </button>

      <button id="btn2"
        class="mb-2 md:mb-0 bg-transparent hover:bg-indigo-500 text-indigo-700 font-semibold hover:text-white py-2 px-4 border border-indigo-500 hover:border-transparent rounded">
        解除
      </button>

      <div style="height:1500px;"></div>

    </div>

  </div>
</body>

</html>

実行結果を確認すると、スクロールが禁止されていることが確認できます。