當前位置: 首頁>>編程示例 >>用法及示例精選 >>正文


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。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。