当前位置: 首页 > 工具软件 > TouchSwipe > 使用案例 >

jQuery移动设备上支持滑动事件(jquery.touchSwipe.min.js)

子车飞鹏
2023-12-01

cdn地址:http://www.bootcdn.cn/jquery.touchswipe/
Github地址: https://github.com/mattbryson/TouchSwipe-Jquery-Plugin

基本操作

$(function() {      
      //Enable swiping...
      $("#test").swipe( {
        //Generic swipe handler for all directions
        swipe:function(event, direction, distance, duration, fingerCount, fingerData) {
          $(this).text("You swiped " + direction );  
        },
        //Default is 75px, set to 0 for demo so any distance triggers swipe
         threshold:0
      });
    });

引入了jQuery库和TouchSwipe后,可以使用上面的代码检测到滑动事件,其中的direction指方向,有’up’, ‘down’, ‘left’, ‘right’ 而distance指滑动距离。下面的threshold指的是触发范围,既最小滑动多少才触发,注意,实际使用时,一般不能为0,因为设置为0将覆盖click, tap等点击操作。如果要检测整个页面的滑动,可以把#test改为body。

左右滑动监控

$(window).swipe({
    swipeLeft:function(){
        // 向左滑动执行
    },
    swipeRight:function(){
        // 向右滑动执行
    }
});

如果某个盒子加了滑动效果,而其中某个或者多个子元素不需要这个滑动效果怎么做呢?

有两种方法,一种是这样写:

win.swipe({
    swipeLeft:function(){
        //向左滑动
    },
    swipeRight:function(){
        //向右滑动
    },
    excludedElements:$.fn.swipe.defaults.excludedElements+", #bar_nav , #banner ,#my_nav"
});

如上,加上最后一句,把不需要的ID写在后面即可。注意逗号。

另外,还有一种方法,就是直接给不需要滑动的元素加上.noSwipe 这个样式名即可。

 类似资料: