码迷,mamicode.com
首页 > 移动开发 > 详细

移动端长按事件

时间:2017-07-05 15:20:16      阅读:312      评论:0      收藏:0      [点我收藏+]

标签:list   als   超过   例子   ams   span   长按   chm   javascrip   

   第一种方法:这个例子我获取不到当前长按元素;

$.fn.longPress = function(fn) {
    var timeout = undefined;
    var $this = this;
    for(var i = 0;i<$this.length;i++){
        $this[i].addEventListener(‘touchstart‘, function(event) {
            timeout = setTimeout(fn, 800);  //长按时间超过800ms,则执行传入的方法
            }, false);
        $this[i].addEventListener(‘touchend‘, function(event) {
            clearTimeout(timeout);  //长按时间少于800ms,不会执行传入的方法
            }, false);
    }
}
调用:
$(‘.object‘).longPress(function(){
    //do something...
});


第二种方法:这个方法能获取到当前元素;

var timeOutEvent=0,cardId;
  $(".card-list li").on({
  touchstart: function(e){
    var that = this;
    timeOutEvent = setTimeout(function () {
      //长按触发事件
      timeOutEvent = 0;
      alert(‘我在长按‘);
    },800);
    // e.preventDefault();
  },
  touchmove: function(){
    clearTimeout(timeOutEvent);
    timeOutEvent = 0;
  },
  touchend: function(){
    clearTimeout(timeOutEvent);
    // return false;
  }
})



移动端长按事件

标签:list   als   超过   例子   ams   span   长按   chm   javascrip   

原文地址:http://www.cnblogs.com/lcazzz/p/7121174.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!