简体   繁体   English

仅阻止水平滚动移动应用

[英]Prevent only horizontal scrolling mobile app

I'm developing an web app. 我正在开发一个Web应用程序。 On the left side is an sidebar using the Sidr-plugin ( jQuery Plugin: Sidr ). 左侧是使用Sidr插件( jQuery插件:Sidr )的侧边栏。 The test site is on my developing server . 测试站点在我正在开发的服务器上 My problem is that if I'm swipe from left-to-right the sidebar is displayed. 我的问题是,如果我从左向右滑动,则会显示侧边栏。 That's very good. 这是非常好的。 But if I want to close the sidebar by swiping from right-to-left I must prevent the scrolling by add this following code: 但是,如果我想通过从右向左滑动来关闭侧边栏,则必须通过添加以下代码来防止滚动:

$('body').bind('touchmove', function(e){e.preventDefault()})

I did that, but now: My navigation in the top of the page (menu) doesn't work fine. 我这样做了,但是现在:页面顶部(菜单)中的导航无法正常工作。 I can't scroll until to the end. 我无法滚动到最后。

So my question is: How can I change this. 所以我的问题是:我该如何改变。 It should only prevent the vertical Scrolling if I'm going to close the Sidebar on the left. 如果我要关闭左侧的侧边栏,则只能阻止垂直滚动。

Here's my complete JavaScript: 这是我完整的JavaScript:

$(function(){
    $(document).foundation();
    $('#sidebar-toggle').sidr();

    var hammertime = Hammer(document.getElementById("wrapper")).on("swiperight", function(event) {
        $.sidr('open');
    });

    var hammertime = Hammer(document.getElementById("wrapper")).on("swipeleft", function(event) {
        $.sidr('close');
    });

    var hammertime = Hammer(document.getElementById("content")).on("tap", function(event) {
        $.sidr('close');
    });

    $('body').bind('touchmove', function(e){e.preventDefault()})
});

Make page not scrollable 使页面不可滚动

Something like this? 像这样吗

I think that using $(document) would be better! 我认为使用$(document)会更好!

Reference: Prevent horizontal scroll on jQuery Mobile 参考: 防止在jQuery Mobile上水平滚动

var lastY;
$(document).bind('touchmove', function (e){
    var currentY = e.touches[0].clientY;
    if (currentY !== lastY){
        // moved vertically
        return;
    }
    lastY = currentY;
    //insert code if you want to execute something when an horizontal touchmove is made
});

I was trying to integrate Sidr with Hammer JS but there is conflict, so I got rid of Hammer and used the code below, which uses TouchWipe JS combined with Sidr JS. 我试图将Sidr与Hammer JS集成在一起,但是存在冲突,因此我摆脱了Hammer并使用下面的代码,该代码将TouchWipe JS与Sidr JS结合使用。

<!-- Swipe to open or close mobile menu -->

<script>

      jQuery(window).touchwipe({
        wipeLeft: function() {
          // Close
          jQuery.sidr('close', 'sidr-main');
        },
        wipeRight: function() {
          // Open
          jQuery.sidr('open', 'sidr-main');
        },
        preventDefaultEvents: false
      });

</script>

I guess the problem you are facing with right-to-left swipe should be fixed. 我想应该解决从右向左滑动时遇到的问题。

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

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