简体   繁体   English

试图防止jQueryMobile滑动手势起泡,但不起作用

[英]Trying to prevent jQueryMobile swipe gesture from bubbling, but it's not working

I'm using jQuery Mobile and created something that somewhat resembles Android Holo Tabs: 我正在使用jQuery Mobile,并创建了一些类似于Android Holo Tabs的东西:

http://note.io/18RNMRk http://note.io/18RNMRk

In order to get the swipe gesture to work for switching between tabs, this is the code I've added: 为了使滑动手势在选项卡之间切换时起作用,这是我添加的代码:

$("#myPage #pageTabs").on('swipeleft swiperight', function(e){
    e.stopPropagation();
    e.preventDefault();
});
$("#myPage").on('swipeleft', function(){
    ui.activities.swipe(1);
}).on('swiperight', function(){
    ui.activities.swipe(-1);
});

With the tabs' HTML resembling: 标签的HTML类似于:

<div id="pageTabs">
    <div class="tab">
        <a href="#" data-dayOfMonth="26">Thu</a>
    </div>
    <div class="tab">
        <a href="#" data-dayOfMonth="27">Fri</a>
    </div>
    <div class="tab">
        <a href="#" data-dayOfMonth="28" data-meridian="am">Sat AM</a>
    </div>
    <div class="tab">
        <a href="#" data-dayOfMonth="28" data-meridian="pm">Sat PM</a>
    </div>
    <div class="tab">
        <a href="#" data-dayOfMonth="29">Sun</a>
    </div>
</div>

I'm listening for the swipe gesture at the page-level because the div[data-role=content] can sometimes not fill the screen vertically, if there isn't enough content to do so. 我正在听页面级别的滑动手势,因为如果没有足够的内容, div[data-role=content]有时可能无法垂直填充屏幕。 If I listened on this div and it was not covering the screen, and you swiped close to the bottom, the event wouldn't fire on this div, it would be on the root page ( div[data-role=page] ). 如果我在此div上收听并且没有覆盖屏幕,而您在底部滑动,则该事件不会在该div上触发,而是在根页面( div[data-role=page] )上。

Here's Firefox's 3D rendering of that page for proof of the above assertion. 这是Firefox对该页面的3D渲染,以证明上述主张。 I've annotated div[data-role=content] : 我已经注释了div[data-role=content]

http://note.io/18RPhyK http://note.io/18RPhyK

For that reason, I'm listening for it at the page level; 因此,我在页面级别上进行监听。 but since the number of tabs can scroll out of the viewport (as seen above: Sunday is off-screen to the right), I would like the user to be able to scroll that horizontally as well. 但是由于选项卡的数量可以滚动到视口之外(如上所示:星期日不在屏幕右侧),我希望用户也可以水平滚动。 I've already got the horizontal scrolling working (that's just some simple CSS), but the problem is that, even with my e.stopPropagation() seen above, the swipe gesture is bubbling up to the page element and my swipe gesture is preventing the smooth scrolling that was available before I added the swipe gesture. 我已经可以进行水平滚动了(这只是一些简单的CSS),但是问题是,即使上面看到了我的e.stopPropagation() ,滑动手势也会冒泡到页面元素,而滑动手势却阻止了添加滑动手势之前可以进行的平滑滚动。

Am I misunderstanding how event bubbling works, or how to stop it in this scenario? 我是否误解了事件冒泡的工作原理,或者在这种情况下如何阻止它?

I've found a workaround, but an actual solution would be better. 我找到了一种解决方法,但是实际的解决方案会更好。

Since the problem is that I don't have a container that fills these two requirements: 由于问题在于我没有一个容器可以满足以下两个要求:

  1. contain the page content, but not the tabs 包含页面内容,但不包含标签
  2. use min-height to always fill the screen vertically 使用最小高度始终垂直填充屏幕

I decided to add a wrapper: 我决定添加一个包装器:

<div id="pageTabs">
    <div class="tab">
        <a href="#" data-dayOfMonth="26">Thu</a>
    </div>
    <div class="tab">
        <a href="#" data-dayOfMonth="27">Fri</a>
    </div>
    <div class="tab">
        <a href="#" data-dayOfMonth="28" data-meridian="am">Sat AM</a>
    </div>
    <div class="tab">
        <a href="#" data-dayOfMonth="28" data-meridian="pm">Sat PM</a>
    </div>
    <div class="tab">
        <a href="#" data-dayOfMonth="29">Sun</a>
    </div>
</div>
<div class="contentWrapper">
    <!-- ... -->
</div>

I've moved my swipe gesture attachment to this new wrapper and removed the swipe listener from the tab bar: 我已将滑动手势附件移至此新包装器,并从选项卡栏中删除了滑动侦听器:

$(".contentWrapper").on('swipeleft', function(){
    ui.activities.swipe(1);
}).on('swiperight', function(){
    ui.activities.swipe(-1);
});

And in order to make sure it always fills the screen, every time the page is loaded, I set its min-height to the calculated height of the viewport minus the wrapper's top offset: 并且为了确保每次加载页面时始终填充屏幕,我将其最小高度设置为计算出的视口高度减去包装器的顶部偏移量:

$("#myPage").on('pageshow', function(){

    var viewPortHeight = document.documentElement.clientHeight;
    var offset = $(".contentWrapper", this)[0].offsetTop;

    $(".contentWrapper", this).css({'min-height': viewPortHeight - offset + 'px', 'display': 'block'});
});

This does exactly what I want. 这正是我想要的。 I can swipe on the page content to switch tabs, and I can horizontally scroll the tabs without activating a swipe gesture. 我可以在页面内容上滑动以切换标签,也可以在不激活滑动手势的情况下水平滚动标签。

For anyone interested in achieving the same effect, here's my LESSCSS: 对于有兴趣实现相同效果的人,这里是我的LESSCSS:

#pageTabs {
    @tab-highlight: @lightblue;

    margin: -15px -15px 15px -15px;
    padding: 0;
    height: 38px;
    overflow-x: scroll;
    overflow-y: hidden;
    white-space: nowrap;
    border-bottom: 2px solid @tab-highlight;

    .tab {
        display: inline-block;
        vertical-align: top;
        border-left: 1px solid @tab-highlight;
        margin-left: -5px;
        height: 100%;

        &:first-child {
            margin-left: 0;
        }
        &.active {
            height: 32px;
            border-bottom: 8px solid @tab-highlight;
        }
        a {
            padding: 12px 20px 0px 20px;
            display: block;
            height: 100%;
            text-decoration: none;
        }
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM