如何将滑动功能添加到图库?

时间:2014-11-12 21:38:36

标签: javascript jquery

我有一个图片库,我想添加滑动功能,下一个和prev大图像。我不想使用任何插件。我有一些代码,我尝试了一些,但我无法使它工作。任何建议都非常感谢。

$(document).on("pagecreate","#pageone",function(){
 $("img").on("swipeleft",function(){
    console.log("Left");
  }); 

 $("img").on("swiperight",function(){
    console.log("Right");
  });                          

});

Jsfiddle

谢谢!

1 个答案:

答案 0 :(得分:0)

swipeleft事件侦听器不适用于jQuery。您可以使用jQuery Mobile,或使用touchstarttouchmovetouchend制作自己的。假设您只想执行一次,则应执行以下代码:

var swiping = false;

$('#div').on('touchmove', function (event) {
  swiping = true;
})


$('#div').on('touchstart', function (event) {
  setTimeout(function() {
    if ( swiping = true ) {
      console.log('swiping');
    }
  }, 50)
})

由于setTimeouttouchmove同时开始,touchstart可能不是必需的 - 但是如果任何给定的浏览器执行不同的话,我会把它留在那里。