zoukankan      html  css  js  c++  java
  • 移动端touchstar、touchmove、touchend 事件如果页面有滚动时不让触发 touchend 事件。

    /*仅适用于内容中点击元素。对于拖动等元素,需要自行在页面处理。
    * 主要是绑定touchstart和touchmove事件,并判断用户按下之后手指移动了多少像素。
    * 如果手指移动距离小于10像素,则还是认为用户在做点击操作。如果移动距离超过了10像素,则取消后续事件监听函数的执行。*/



    <script type="text/javascript"> function makeTouchableButton(ele) { if (!ele) { console.error("MIGlobals.makeTouchableButton 无效的元素!"); return false; } ele.addEventListener("touchstart", function(evt){ this.setAttribute("data-moved", "n"); var p = evt.touches[0]; this.setAttribute("data-touch-start-clientx", p.clientX); this.setAttribute("data-touch-start-clienty", p.clientY); }); ele.addEventListener("touchmove", function(evt){ if (this.getAttribute("data-moved") == "y") return false; var p = evt.touches[0]; var startClientX = parseInt(this.getAttribute("data-touch-start-clientx"), 10); var startClientY = parseInt(this.getAttribute("data-touch-start-clienty"), 10); var deltax = p.clientX - startClientX; var deltay = p.clientY - startClientY; if (Math.abs(deltax) > 10 || Math.abs(deltay) > 10) { this.setAttribute("data-moved", "y"); } }); ele.addEventListener("touchend", function(evt) { if (this.getAttribute("data-moved") == "y") { evt.stopImmediatePropagation(); return false; } }); } var divs = document.querySelector(".touchdiv"); makeTouchableButton(divs); divs.addEventListener("touchend",function(){ console.log("您点击我啦。"); }); </script>
  • 相关阅读:
    各个版本中Notification对象创建的方法
    数据结构一:线性表
    安装eclipse中文汉化包后无法打开eclipse【转】
    在MFC里面使用自定义的OpenGL类进行绘图(基于VS2010)
    2016-2-25 我的博客开通了
    从C#到Swift原来这么简单,So Easy!
    CocoaPods安装及使用(包含重装步骤)
    Xcode键盘快捷键
    参考资料收集
    重温算法和数据结构:二分查找
  • 原文地址:https://www.cnblogs.com/sunkunqi/p/6165586.html
Copyright © 2011-2022 走看看