LoginSignup
214
208

More than 5 years have passed since last update.

タッチデバイスでCSSの:activeや:hoverを機能させる。

Last updated at Posted at 2016-06-29

demo
https://jsfiddle.net/junya_5102/pgu54owz/6/

タッチさせる要素に ontouchstart=""と指定する。
これを指定しないとタッチでは:activeと:hoverは反応しない。

<div class="box" ontouchstart=""></div>

あとはCSSで:active,:hoverを書く。

:active タッチ中のみ処理を行う

.box:active{
  background-color: red;
}

:hover タッチ後、違う要素がタッチされるまで

.box:hover{
  background-color: red;
}

補足

bodyにontouchstart=""を指定すれば、子要素がタッチ時に反応するようになる。
ontouchstart=""である必要はない ontouchendでも可能。

demo
https://jsfiddle.net/junya_5102/pgu54owz/7/

環境

iOS 9 で Safari と Chrome で確認

214
208
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
214
208