0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

pointerupイベントを使用するときにはtouch-actionをnoneにする

Last updated at Posted at 2025-12-08

八戸高専 アドカレ
8日目← →10日目

pointerupイベントを使用するときにはtouch-actionをnoneにする

 スマホ環境などで、pointerdownpointermoveは発生するのに、pointerupだけ発生しない場合があります。これは、pointerupの代わりにブラウザのズームなどが機能してしまうためです。

 cssのtouch-actionnoneにしてあげると、ブラウザのズームアクションが起こらなくなり、pointerupが発生するようになります。

<style>
  #flex {
    display: flex;
  }

  #auto {
    background-color: #ce5228;
    touch-action: auto;
  }

  #none {
    background-color: #aeee37;
    touch-action: none;
  }

  #console {
    height: 10rem;
    overflow-y: scroll;
  }
</style>

<div id="flex">
  <div id="auto">&nbsp;touch-action-auto&nbsp;</div>
  <div id="none">&nbsp;touch-action-none&nbsp;</div>
</div>
<div id="console"></div>

<script>
  let csl = document.getElementById("console")
  let a = document.getElementById("auto")
  a.addEventListener("pointerdown", () => {
    csl.innerText += "auto : pointerdown\n"
  })
  a.addEventListener("pointermove", () => {
    csl.innerText += "auto : pointermove\n"
  })
  a.addEventListener("pointerup", () => {
    csl.innerText += "auto : pointerup\n"
  })
  let n = document.getElementById("none")
  n.addEventListener("pointerdown", () => {
    csl.innerText += "none : pointerdown\n"
  })
  n.addEventListener("pointermove", () => {
    csl.innerText += "none : pointermove\n"
  })
  n.addEventListener("pointerup", () => {
    csl.innerText += "none : pointerup\n"
  })
</script>

上記のHTMLをスマホ(又はdevtool)で開いて、autoにしている要素内でドラック動作をしてみると、pointerupが発生しません。

noneにしている要素内でドラック動作をしてみると、pointerupが発生します。

 環境によってはautoでも問題なかったりして、忘れがちなので備忘録として残します。

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?