当前位置: 首页>>代码示例 >>用法及示例精选 >>正文


JQuery Mobile swipe用法及代码示例


swipe event

添加的版本:1.0

说明:在 1 秒的持续时间内发生 30 像素或更多(垂直小于 30 像素)的水平拖动时触发。

  • jQuery( window ).on( "swipe", function( event ) { ... } )

当在 1 秒内发生 30 像素或更多(垂直小于 30 像素)的水平拖动时触发,但可以配置这些:

  • $.event.special.swipe.scrollSupressionThreshold (default: 10px) - 超过这个水平位移,我们将抑制滚动。
  • $.event.special.swipe.durationThreshold(默认值:1000 毫秒)- 比这更多的时间,而且它不是滑动。
  • $.event.special.swipe.horizontalDistanceThreshold(默认值:30px) - 滑动水平位移必须大于此。
  • $.event.special.swipe.verticalDistanceThreshold(默认值:30px) - 滑动垂直位移必须小于此值。

还可以扩展滑动事件以添加您自己的逻辑或函数。可以扩展以下方法:

  • $.event.special.swipe.start默认:
    function( event ) {
      var data = event.originalEvent.touches ?
          event.originalEvent.touches[ 0 ] : event;
      return {
          time: ( new Date() ).getTime(),
          coords: [ data.pageX, data.pageY ],
          origin: $( event.target )
        };
    }

    此方法接收 touchstart 事件并返回有关起始位置的数据对象。

  • $.event.special.swipe.stop默认:
    function( event ) {
      var data = event.originalEvent.touches ?
          event.originalEvent.touches[ 0 ] : event;
      return {
          time: ( new Date() ).getTime(),
          coords: [ data.pageX, data.pageY ]
        };
    }

    此方法接收一个 touchend 事件并返回一个关于结束位置的数据对象。

  • $.event.special.swipe.handleSwipe默认:
    function( start, stop ) {
      if ( stop.time - start.time < $.event.special.swipe.durationThreshold &&
        Math.abs( start.coords[ 0 ] - stop.coords[ 0 ] ) > $.event.special.swipe.horizontalDistanceThreshold &&
        Math.abs( start.coords[ 1 ] - stop.coords[ 1 ] ) < $.event.special.swipe.verticalDistanceThreshold ) {
     
        start.origin.trigger( "swipe" )
          .trigger( start.coords[0] > stop.coords[ 0 ] ? "swipeleft" : "swiperight" );
      }
    }

    此方法接收开始和停止对象并处理滑动事件的逻辑和触发。

例子:

捕获和作用于滑动事件的简单示例

<!doctype html>
<html lang="en">
<head>
  <meta charset="utf-8">
  <meta name="viewport" content="width=device-width, initial-scale=1">
  <title>swipe demo</title>
  <link rel="stylesheet" href="//code.jquery.com/mobile/1.5.0-alpha.1/jquery.mobile-1.5.0-alpha.1.min.css">
  <script src="//code.jquery.com/jquery-3.2.1.min.js"></script>
  <script src="//code.jquery.com/mobile/1.5.0-alpha.1/jquery.mobile-1.5.0-alpha.1.min.js"></script>
  <style>
  html, body { padding: 0; margin: 0; }
  html, .ui-mobile, .ui-mobile body {
    height: 105px;
  }
  .ui-mobile, .ui-mobile .ui-page {
    min-height: 105px;
  }
  #nav {
    font-size: 200%;
    width:17.1875em;
    margin:17px auto 0 auto;
  }
  #nav a {
    color: #777;
    border: 2px solid #777;
    background-color: #ccc;
    padding: 0.2em 0.6em;
    text-decoration: none;
    float: left;
    margin-right: 0.3em;
  }
  #nav a:hover {
    color: #999;
    border-color: #999;
    background: #eee;
  }
  #nav a.selected,
  #nav a.selected:hover {
    color: #0a0;
    border-color: #0a0;
    background: #afa;
  }
  div.box {
    width: 30em;
    height: 3em;
    background-color: #108040;
  }
  div.box.swipe {
    background-color: #7ACEF4;
  }
  </style>
</head>
<body>
 
<h3>Swipe the green rectangle to change its color:</h3>
<div class="box"></div>
 
<script>
$(function(){
  // Bind the swipeHandler callback function to the swipe event on div.box
  $( "div.box" ).on( "swipe", swipeHandler );
 
  // Callback function references the event target and adds the 'swipe' class to it
  function swipeHandler( event ){
    $( event.target ).addClass( "swipe" );
  }
});
</script>
 
</body>
</html>

演示:

相关用法


注:本文由纯净天空筛选整理自jquerymobile.com大神的英文原创作品 swipe。非经特殊声明,原始代码版权归原作者所有,本译文未经允许或授权,请勿转载或复制。