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

跟風遠走 提交于 2019-12-04 05:21:59

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:

#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;
        }
    }
}
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!