Growth Hackで得た知見やWeb制作の備忘録をまとめています。

This is standard

javascript

jQueryスワイプしたらアイコンを消す

投稿日:

スマホでtableレイアウトをみせる時、画面幅より大きく作ってスワイプで横に流す見せ方ありますよね。
iPhoneだとスムーズにスクロールさせる-webkit-overflow-scrolling: touch;プロパティを使うと、スクロールが重くなるので、スクロールできますよーって事を明示するためにアイコンを表示したりしますね。

ここでは、スワイプしたらアイコンを非表示にする例をご紹介します。
※PCでは再現できないので、スマホでご確認ください。

See the Pen スワイプ by This is standard (@koutafunahashi) on CodePen.

<div id="wrap">
  <div id="icon"><img src="https://thisisstandarz.com/wordpress/wp-content/uploads/2019/10/icon_swipe.png" width="60" alt="" /></div>
  <div id="inner">
    <p>Where does it come from?<br />
Contrary to popular belief, Lorem Ipsum is not simply random text. It has roots in a piece of classical Latin literature from 45 BC, making it over 2000 years old. Richard McClintock, a Latin professor at Hampden-Sydney College in Virginia, looked up one of the more obscure Latin words, consectetur, from a Lorem Ipsum passage, and going through the cites of the word in classical literature, discovered the undoubtable source. Lorem Ipsum comes from sections 1.10.32 and 1.10.33 of "de Finibus Bonorum et Malorum" (The Extremes of Good and Evil) by Cicero, written in 45 BC. This book is a treatise on the theory of ethics, very popular during the Renaissance. The first line of Lorem Ipsum, "Lorem ipsum dolor sit amet..", comes from a line in section 1.10.32.

The standard chunk of Lorem Ipsum used since the 1500s is reproduced below for those interested. Sections 1.10.32 and 1.10.33 from "de Finibus Bonorum et Malorum" by Cicero are also reproduced in their exact original form, accompanied by English versions from the 1914 translation by H. Rackham.</p>
  </div>
</div>
#wrap{
  width:250px;
  height:200px;
  overflow-x: scroll;
  -webkit-overflow-scrolling: touch;
  position:relative;
}
#wrap::-webkit-scrollbar {
  height: 4px;
}
#wrap::-webkit-scrollbar-thumb{
  background: #008338;
  border-radius: 2px;
}
#wrap::-webkit-scrollbar-track-piece {
  background: #f0f0ed;
}
#inner{
  width:2000px
}

#icon{
  position:absolute;
  top:50%;
  left:50%;
  width:80px;
  height:70px;
  background-color:rgba(0,0,0,0.5);
  border-radius:10px;
  text-align:center;
  padding-top:10px;
  transform: translate(-50%, -50%);
}
$(function() {
  $('#inner').on('touchstart', onTouchStart);
  $('#inner').on('touchmove', onTouchMove);
  $('#inner').on('touchend', onTouchEnd);
  var direction, position;

  //スワイプ開始時の横方向の座標を格納
  function onTouchStart(event) {
    position = getPosition(event);
    direction = '';
  }

  //スワイプの方向(left/right)を取得
  function onTouchMove(event) {
    if (position - getPosition(event) > 20) {
      direction = 'left';
    } else if (position - getPosition(event) < -20){
      direction = 'right';
    }
  }

  function onTouchEnd(event) {
    if (direction == 'left'){
      $("#icon").remove();
    }
  }

  //横方向の座標を取得
  function getPosition(event) {
    return event.originalEvent.touches[0].pageX;
  }
});

https://qiita.com/Wataru/items/a9548015e00683e142e4

スマホのスワイプ検知

-javascript

Copyright© This is standard , 2019 All Rights Reserved Powered by STINGER.