手机滑动touchSwipe

jQuery.touchswipe插件

TouchSwipe一款专门为移动设备设计的jquery插件,用于监听单个和多个手指触摸等事件。

一、特点:

1、监听滑动的4个方向:上、下、左、右;

2、监听多个手指收缩还是外张;

3、支持单手指或双手指触摸事件;

4、支持单击事件touchSwipe对象和它的子对象;

5、可定义临界值和最大时间来判断手势的实际滑动;

6、滑动事件有4个触发点:“开始”,“移动”,“结束”和“取消”;

7、结束事件可以在触摸释放或是达到临界值时触发;

8、允许手指刷和页面滚屏;

9、可禁止用户通过输入元素(如:按钮、表单、文本框等)触发事件;

二、安装

1、NPM

npm install jquery-touchswipe --save

2、Bower

bower install jquery-touchswipe --save

3、将压缩文件添加到你的项目里

<script src="js/jquery.touchSwipe.min.js" type="text/javascript"></script>

4、使用

1
2
3
4
5
6
7
8
9
10
11
$( function () {
   $( "#test" ).swipe( {
     //Generic swipe handler for all directions
     swipe: function (event, direction, distance, duration, fingerCount, fingerData)                     {
       $( this ).text( "You swiped "  + direction ); 
     }
   });
 
   //Set some options later
   $( "#test" ).swipe( {fingers:2} );
});   

  

三、Methods(方法)

1、swipe:初始化

例子:$("#element").swipe({

//给id为element的容器触摸滑动监听事件

});

2、destroy:彻底销毁swipe插件,必须重新初始化插件才能再次使用任何方法

例子:$("#element").swipe("destroy");

3、disable:禁止插件,使插件失去作用

返回值:现在与插件绑定的DOM元素

例子:$("#element").swipe("disable");

4、enable:重新启用插件,恢复原有的配置

返回值:现在与插件绑定的DOM元素

例子:$("#element").swipe("enable");

5、option:设置或获取属性

例子:

$("#element").swipe("option", "threshold"); // 返回阈值

$("#element").swipe("option", "threshold", 100); // 设置阈值

$("#element").swipe("option", {threshold:100, fingers:3} ); // 设置多个属性

$("#element").swipe({threshold:100, fingers:3} ); // 设置多个属性 -"option"方法可省略

$("#element").swipe("option"); // 返回现有的options

四、Events事件

1、swipe:滑动事件

1
2
3
swipe: function (event, direction, distance, duration, fingerCount, fingerData) {
             $( this ).text( "You swiped "  + direction ); 
             }

参数:

 

2、swipeDown:向下滑动事件

swipeDown: function(event, direction, distance, duration, fingerCount, fingerData) {              

}

参数同swipe事件

3、swipeUp:向上滑动事件

swipeUp: function(event, direction, distance, duration, fingerCount, fingerData) {              

}

参数同swipe事件

4、swipeLeft:向左滑动事件

swipeLeft: function(event, direction, distance, duration, fingerCount, fingerData) {              

}

参数同swipe事件

5、swipeRight:向右滑动事件

swipeLeft: function(event, direction, distance, duration, fingerCount, fingerData) {              

}

参数同swipe事件

6、swipeStatus:滑动过程会持续触发swipeStatus事件,不受阈值限制

swipeLeft: function(event, direction, distance, duration, fingerCount, fingerData, currentDirection) {              

}

参数:

 

7、tap:当用户简单地点击或者轻击而不是滑动一个元素时tap/click事件将被触发

tap:function(event,target){

console.log($(target).attr("class"));

}

参数:

event:原生事件对象

target:被点击的元素(DOM对象)

8、doubleTap:当用户连续两次点击或者轻击而不是滑动一个元素时事件将被触发

doubleTap:function(event,target){

console.log($(target).attr("class"));

}

参数:

event:原生事件对象

target:被点击的元素(DOM对象)

  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <meta charset="UTF-8">
  5. <meta name="viewport" id="viewport" content="width=device-width, initial-scale=1">
  6. <title>demo </title>
  7. <script src="../js/jquery-1.11.3.min.js"> </script>
  8. <script src="js/jquery.touchSwipe.min.js"> </script>
  9. <style>
  10. * { margin: 0; padding: 0;}
  11. body, html { width: 100%; height: 100%; background: pink;}
  12. .div1 { height: 200px; background: red;}
  13. .div2 { height: 200px; background: blue;}
  14. </style>
  15. </head>
  16. <body>
  17. <p>改 demo 待完善... </p>
  18. <p>建议在手机端测试 </p>
  19. <p class="disable">禁用 </p>
  20. <p class="enable">恢复 </p>
  21. <p class="destroy">销毁 </p>
  22. <p class="init">初始化 </p>
  23. <div class="div1">
  24. 滑动这里
  25. </div>
  26. <div class="div2">
  27. </div>
  28. <script>
  29. $( function() {
  30. //兼容ie8+、手机端
  31. $( '.div1').swipe({
  32. /*swipe: function(event, direction, distance, duration, fingerCount, fingerData) {
  33. $('body').append('direction -> '+ direction);
  34. $('body').append('distance -> '+ distance);
  35. $('body').append('duration -> '+ duration);
  36. $('body').append('fingerCount -> '+ fingerCount);
  37. },*/
  38. /*//向左滑
  39. swipeLeft: function(event, direction, distance, duration, fingerCount, fingerData) {
  40. $('body').append('direction -> '+ direction +'<br>');
  41. $('body').append('distance -> '+ distance +'<br>');
  42. $('body').append('duration -> '+ duration +'<br>');
  43. $('body').append('fingerCount -> '+ fingerCount +'<br>');
  44. },*/
  45. /*//滑动过程中
  46. swipeStatus:function(event, phase, direction, distance, duration, fingerCount, fingerData, currentDirection) {
  47. var html = 'phase -> '+ phase +'<br>'+ 'direction -> '+ direction +'<br>'+'distance -> '+ distance +'<br>'+'duration -> '+ duration +'<br>'+'fingerCount -> '+ fingerCount +'<br>'+'currentDirection -> '+ currentDirection +'<br>';
  48. $('.div2').html(html);
  49. },
  50. triggerOnTouchEnd: false,//启动超过阈值停止插件
  51. <p> threshold: 200,//滑动阈值 distance > threshold 时 phase = end</p><p><span style="white-space:pre"> excludedElements: "button, input, select, textarea, .noSwipe",</span>//让a标签也支持滑动事件</p>allowPageScroll: 'auto',//滑动时不影响滚动条的正常滚动,该项只在swipeStatus生效 maxTimeThreshold: 5000,//触摸阈值 duration > maxTimeThreshold 时 phase = cancle
  52. triggerOnTouchLeave: true,//手指离开对象,停止插件
  53. fingers: 'all',//手指数量(手机端生效)*/
  54. tap: function(event, target) {
  55. $( '.div2').append( '<br>tap:'+ $(target).attr( 'class'));
  56. },
  57. doubleTap: function(event, target) {
  58. $( '.div2').append( '<br>doubleTap:'+ $(target).attr( 'class'));
  59. },
  60. hold: function(event, target) {
  61. $( '.div2').append( '<br>hold:'+ $(target).attr( 'class'));
  62. }
  63. }).on( 'click', function(e) { //同时绑定click
  64. $( '.div2').append( '<br>click:'+ $( this).attr( 'class'));
  65. }).on( 'dblclick', function(e) { //同时绑定click
  66. $( '.div2').append( '<br>dblclick:'+ $( this).attr( 'class'));
  67. });
  68. //
  69. $( '.disable').on( 'click', function() {
  70. $( '.div1').swipe( 'disable');
  71. });
  72. $( '.enable').on( 'click', function() {
  73. $( '.div1').swipe( 'enable');
  74. });
  75. $( '.destroy').on( 'click', function() {
  76. $( '.div1').swipe( 'destroy');
  77. });
  78. $( '.init').on( 'click', function() {
  79. $( '.div1').swipe({
  80. swipe: function() {
  81. console.log( 1);
  82. },
  83. });
  84. });
  85. });
  86. </script>
  87. </body>
  88. </html>



猜你喜欢

转载自blog.csdn.net/y525794266/article/details/80918796
今日推荐