移动端滑动事件方法

Dear 丶 2022-06-02 05:40 300阅读 0赞

今天使用jquery.fullpage.js的时候,发现移动端调试不支持滑动事件,于是自己写了一个滑动的事件,如下,使用这个插件时只需要给window绑定一个滑动事件就可以了,不需要添加方法,由于第一次使用,还没看他js的具体信息,只是粗略过了一眼,好,具体内容看代码:

  1. <!DOCTYPE html>
  2. <html lang='en'>
  3. <head>
  4. <meta charset='UTF-8'>
  5. <meta name='referrer' content='always'>
  6. <meta name='author' content='qiphon'>
  7. <meta name='robots' content='none'>
  8. <meta name='keywords' content=''>
  9. <meta name='description' content=''>
  10. <meta name='renderer' content='webkit'>
  11. <meta name='revisit-after' content='7 days'>
  12. <meta http-equiv=widow-target Content=_top>
  13. <meta name='viewport'
  14. content='width=device-width, initial-scale=1,maximum-scale=1,minimum-scale=1,user-scalable=no, shrink-to-fit=no'
  15. viewport-fit=cover />
  16. <meta http-equiv='X-UA-Compatible' content='ie=edge,chrome=1'>
  17. <title></title>
  18. <style>
  19. * {
  20. margin: 0;
  21. padding: 0;
  22. box-sizing: border-box;
  23. -webkit-tap-highlight-color: transparent;
  24. }
  25. h1 {
  26. width: 100px;
  27. height: 100px;
  28. background: cyan;
  29. font-size: 14px;
  30. margin: 300px auto;
  31. position: relative;
  32. }
  33. </style>
  34. </head>
  35. <body>
  36. <h1>在我上面试试touchmove吧</h1>
  37. <script>
  38. // 滑动事件
  39. /*
  40. * @params el DOM 点击的元素
  41. * @params fncontainer obj 事件对象集合
  42. * left Function 向左滑动的时候触发
  43. * right Function 向右滑动的时候触发
  44. * up Function 向上滑动的时候触发
  45. * down Function 向下滑动的时候触发
  46. * @params offset Num 事件触发的值
  47. */
  48. export function touchevent(el = window, fncontainer = {}, offset = 50) {
  49. var startX, startY,
  50. endX = 0,
  51. endY = 0,
  52. startT = 0;
  53. el.addEventListener('touchstart', function (e) {
  54. // console.log(e)
  55. startT = +new Date()
  56. startX = e.touches[0].clientX;
  57. startY = e.touches[0].clientY;
  58. // console.log("startX->"+startX,"startY ->"+startY)
  59. }, false);
  60. el.addEventListener('touchmove', function (e) {
  61. endX = e.touches[0].clientX;
  62. endY = e.touches[0].clientY;
  63. // console.log("endX ->"+endX,"endY ->"+endY)
  64. }, false);
  65. el.addEventListener('touchend', function (e) { //debugger
  66. var lengthX = endX == 0 ? 0 : Math.abs(startX - endX);
  67. var lengthY = endY == 0 ? 0 : Math.abs(startY - endY);
  68. // console.log("lengthX"+lengthX,"lengthY"+lengthY)
  69. if (+new Date() - startT < 600) {
  70. lengthX > lengthY && lengthX >= offset &&
  71. ((startX - endX) > 0 ?
  72. (fncontainer.left && (fncontainer.left).call(el)) :
  73. (fncontainer.right && (fncontainer.right).call(el)));
  74. lengthY > lengthX && lengthY >= offset &&
  75. ((startY - endY) > 0 ?
  76. (fncontainer.up && (fncontainer.up).call(el)) :
  77. (fncontainer.down && (fncontainer.down).call(el)));
  78. }
  79. startX = startY = endX = endY = startT = 0
  80. }, false);
  81. }
  82. touchevent(document.querySelector('h1'), {
  83. left: function () {
  84. console.log('left move', this)
  85. },
  86. right: function () {
  87. console.log('right move')
  88. },
  89. up: function () {
  90. console.log('up move')
  91. },
  92. down: function () {
  93. console.log('down move')
  94. },
  95. })
  96. </script>
  97. </body>
  98. </html>

其实在jQuery.fullpage.js里直接使用touchevent(window)就好,不需要添加额外的方法,但是为了以后使用方便就写了一个,仅供参考

发表评论

表情:
评论列表 (有 0 条评论,300人围观)

还没有评论,来说两句吧...

相关阅读

    相关 移动keyup事件

    最近在做一个项目需求,需要实现一个类似谷歌搜索的功能,可以模糊匹配到中文和字母。当时想到了keyup事件,每当键盘按下时监控input框值的变化,在电脑和安桌手机上测试都没有问

    相关 移动滑动事件方法

    今天使用jquery.fullpage.js的时候,发现移动端调试不支持滑动事件,于是自己写了一个滑动的事件,如下,使用这个插件时只需要给window绑定一个滑动事件就可以了,

    相关 移动滑动

    前言 移动端,滑动是很常见的需求。很多同学都用过[swiper.js][],本文从原理出发,实践出一个类swiper的滑动小插件[ice-skating][]。 小插件