如何使Jquery滑动功能在整个页面上工作

时间:2017-08-03 10:08:01

标签: javascript jquery html swipe

我已按照此link创建向右滑动功能。但是,这只适用于在文本内部滑动时。当我在此页面的任何位置向右滑动时,如何触发向右滑动功能。谢谢!

    $(document).on("pagecreate","#pageone",function(){
      $("p").on("swiperight",function(){
        $(this).hide();
      });                       
    });
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<!DOCTYPE html>
    <html>
    <head>
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <link rel="stylesheet" href="https://code.jquery.com/mobile/1.4.5/jquery.mobile-1.4.5.min.css">
    <script src="https://code.jquery.com/jquery-1.11.3.min.js"></script>
    <script src="https://code.jquery.com/mobile/1.4.5/jquery.mobile-1.4.5.min.js"></script>
    </head>
    <body>
    <div data-role="page" id="pageone">
      <div data-role="header">
        <h1>The swiperight Event</h1>
      </div>
    
      <div data-role="main" class="ui-content">
        <p>If you swipe me in the right direction, I will disappear.</p>
        <p>Swipe me in the right direction!</p>
        <p>Swipe me in the right direction, too!</p>
      </div>
    
      <div data-role="footer">
        <h1>Footer Text</h1>
      </div>
    </div> 
    
    </body>
    </html>

4 个答案:

答案 0 :(得分:0)

您是否尝试在#pageone上设置“swiperight”?

    $(document).on("pagecreate","#pageone",function(){
      $("#pageone").on("swiperight",function(){
        $(this).hide();
      });                       
    });
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<!DOCTYPE html>
    <html>
    <head>
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <link rel="stylesheet" href="https://code.jquery.com/mobile/1.4.5/jquery.mobile-1.4.5.min.css">
    <script src="https://code.jquery.com/jquery-1.11.3.min.js"></script>
    <script src="https://code.jquery.com/mobile/1.4.5/jquery.mobile-1.4.5.min.js"></script>
    </head>
    <body>
    <div data-role="page" id="pageone">
      <div data-role="header">
        <h1>The swiperight Event</h1>
      </div>
    
      <div data-role="main" class="ui-content">
        <p>If you swipe me in the right direction, I will disappear.</p>
        <p>Swipe me in the right direction!</p>
        <p>Swipe me in the right direction, too!</p>
      </div>
    
      <div data-role="footer">
        <h1>Footer Text</h1>
      </div>
    </div> 
    
    </body>
    </html>

答案 1 :(得分:0)

使用:

$("body").on("swiperight",function(){
  console.log('swipe happend, what to do now');
});

$("body").on("swiperight",function(){
  console.log('swipe happend, what to do now');
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<!DOCTYPE html>
    <html>
    <head>
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <link rel="stylesheet" href="https://code.jquery.com/mobile/1.4.5/jquery.mobile-1.4.5.min.css">
    <script src="https://code.jquery.com/jquery-1.11.3.min.js"></script>
    <script src="https://code.jquery.com/mobile/1.4.5/jquery.mobile-1.4.5.min.js"></script>
    </head>
    <body>
    <div data-role="page" id="pageone">
      <div data-role="header">
        <h1>The swiperight Event</h1>
      </div>
    
      <div data-role="main" class="ui-content">
        <p>If you swipe me in the right direction, I will disappear.</p>
        <p>Swipe me in the right direction!</p>
        <p>Swipe me in the right direction, too!</p>
      </div>
    
      <div data-role="footer">
        <h1>Footer Text</h1>
      </div>
    </div> 
    
    </body>
    </html>

答案 2 :(得分:0)

我找到了解决方案。

$(document).on('swipeleft', 'body', function(event){ });
$(document).on('swiperight', 'body', function(event){ });

答案 3 :(得分:0)

我无法执行以上任何操作。但是,确实如此

jQuery(function(){

  jQuery( "body" ).on( "swipeleft", swipeleftHandler );
  jQuery( "body" ).on( "swiperight", swiperightHandler );

  function swipeleftHandler( event ){
    // do stuff here
  }
  function swiperightHandler( event ){
    // do stuff here
  }
});