美文网首页
H5长按事件

H5长按事件

作者: 杉虎 | 来源:发表于2019-07-23 17:06 被阅读0次

举个例子,获取html中的所有图片元素,给它们添加长按事件

function getImages() {
  window.time = 0;
  var objs = document.getElementsByTagName('img');
    for(var i=0; i<objs.length;i++) {
      objs[i].addEventListener('touchstart',function(e) {
        e.stopPropagation();
        time = setTimeout(function() {
          //这里写需要执行操作的代码 
        }, 1000); //这里设置长按响应时间
      })
      objs[i].addEventListener('touchend',function(e) {
        e.stopPropagation();
        clearTimeout(time);
      })
    }
}

相关文章

网友评论

      本文标题:H5长按事件

      本文链接:https://www.haomeiwen.com/subject/ucyalctx.html