【问题标题】:jQuery Mobile panel- adding swipe only at right sidejQuery Mobile 面板 - 仅在右侧添加滑动
【发布时间】:2014-11-28 15:10:28
【问题描述】:

我已经使用 Bootsrap 和 jQuery mobile 为 toch 设备创建了网页。

页面在页面中间包含一个轮播,根据用户的滑动向左/向右滑动。 页面包含Jquery Mobile panel,只有在用户向右滑动时才会打开。

我的问题是如果用户在 carousal 上向右滑动,面板会自动打开。我希望当用户在右侧边缘向右滑动(大约 75 像素)然后只有面板必须打开。

代码:

<!DOCTYPE html>
<html>
    <head>
        <meta charset="utf-8" />
        <title>Bxxloder and JQM</title>
        <meta name="viewport" content="width=device-width, initial-scale=1.0" />
        <link rel="stylesheet" type="text/css" href="bootstrap/css/bootstrap.min.css" />
        <link rel="stylesheet" href="jqm/jquery.mobile-1.4.1.min.css">
        <!--<script src="//ajax.googleapis.com/ajax/libs/jquery/1.8.2/jquery.min.js"></script>-->
        <script type="text/javascript" src="js/jquery-1.10.2.min.js"></script>
        <script type="text/javascript" src="bootstrap/js/bootstrap.min.js"></script>
        <script type="text/javascript" src="bxslider/jquery.bxslider.js"></script>
        <script src="jqm/jquery.mobile-1.4.1.min.js"></script>
    </head>
    <body>
    <!--#########################JQM#########################-->
    <div class="container-fluid" id="demo-page" data-url="demo-page">
        <div data-role="panel" id="left-panel" data-theme="b">
            <p>Left reveal panel.</p>
            <a href="#" data-rel="close" class="ui-btn ui-corner-all ui-shadow ui-mini ui-btn-inline ui-icon-delete ui-btn-icon-left ui-btn-right">Close</a>
        </div><!-- /panel -->
        <div data-role="panel" id="right-panel" data-display="overlay" data-position="right" data-theme="b">
            <p>Right push panel.</p>
            <a href="#" data-rel="close" class="ui-btn ui-corner-all ui-shadow ui-mini ui-btn-inline ui-icon-delete ui-btn-icon-right">Close</a>
        </div><!-- /panel -->
    <!--#########################//JQM#########################-->
    <!--#########################Bx slider#########################-->
    <section class="row col-lg-10" style="float: none; margin: 0 auto;">
    <ul class="bxslider">
      <li><img src="image/b.png" title="Funky roots" /></li>
      <li><img src="image/b.png" title="The long and winding road" /></li>
      <li><img src="image/b.png" title="Happy trees" /></li>
    </ul>
    </section>              
    <!--######################### End Swipe Carousel ##############################-->
    </div>
    <script>
$(document).ready(function()
{
    $('.bxslider').bxSlider(
    {
        mode: 'horizontal',
        captions: true,
        auto: true,
        touchEnabled: true,
        oneToOneTouch: true,
        /* controls: false,*/
        pager: false,
        speed: 200
    });
});
    </script>
    <script>
$(document).on("pagecreate", function()
{    
    $(document).on("swipe", function(e)
    {         
        // We check if there is no open panel on the page because otherwise
        // a swipe to close the left panel would also open the right panel (and v.v.).
        // We do this by checking the data that the framework stores on the page element (panel: open).
        //if ( $( ".ui-page-active" ).jqmData( "panel" ) !== "open" )
        if($(".ui-page-active").jqmData("panel") !== "open" && !$(e.target).hasClass("bxslider"))
        {            
            if(e.type === "swipe")
            {                
                $("#right-panel").panel("open");            
            }
            else if(e.type === "swipe")
            {                
                $("#left-panel").panel("open");            
            }        
        }    
    });
});
    </script>
    </body>
</html>

【问题讨论】:

  • if语句中添加另一个条件if ( $( ".ui-page-active" ).jqmData( "panel" ) !== "open" &amp;&amp; !e.target.hasClass("bxslider"))
  • 更正:!$(e.target).hasClass("bxslider") 应该是一个 jQuery 对象。
  • @Omar:代码已根据评论更新,现在整个面板无法正常工作。
  • ok console.log(e.target),检查哪个项目收到了滑动。
  • @Omar:我的面板在任何情况下都无法在 FF 中运行。在哪里写 console.log。

标签: jquery html twitter-bootstrap jquery-mobile jquery-mobile-panel


【解决方案1】:

在第一个if 语句中添加另一个条件,即接收swipe 的元素没有被滑块包裹。

$(document).on("swipe", function (e) {
  if ($(".ui-page-active").jqmData("panel") !== "open" &&  $(e.target).closest(".bxslider").length === 0) {
    /* code */
  }
});

Demo

【讨论】:

  • 它工作得很好,非常感谢!!!,但是我的面板不能在触摸设备中一键触摸,我们可以在右边缘添加一些触摸效果半径吗。
  • @MikePhils 不客气 :) 面板没有在滑动时打开?你能解释一下情况吗?
  • 面板在 PC 上工作正常,但在移动设备上,当我向右滑动时有时会出现有时不会。我们可以在右边缘附近添加一些滑动效果区域吗?
  • @MikePhils 我刚刚在 ipad 中测试了代码,它按预期工作。您可以在触发滑动之前增加/减少滑动距离。 stackoverflow.com/a/16523579/1771795
  • 使用它并且它工作 $.event.special.swipe.horizo​​ntalDistanceThreshold = (screen.availWidth) / 60; // (default: 30) (pixels) – 滑动水平位移必须小于这个。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2015-08-01
  • 1970-01-01
  • 1970-01-01
  • 2021-11-26
  • 1970-01-01
  • 2012-10-04
  • 1970-01-01
相关资源
最近更新 更多