最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Disable outer content's scrolling on touch based devices (Twitter bootstrap navbar) - Stack Overflow

programmeradmin1浏览0评论

I use twitter bootstrap 3. I added fixed top navbar. I have dropdown buttons on navbar. When user clicks on button, a dropdown menu opens. For desktop users it is ok. But for mobile users, when user scrolls down dropdown links, the page in the back also scrolls.

Is it possible to disable background page scroll when user scrolls dropdown links ?

Fiddle: /

When user touches dropdown part and touches like the green arrow, background page scrolls like red arrow in this screenshot:

<nav class="navbar navbar-inverse navbar-fixed-top visible-xs" role="navigation">
    <div class="navbar-header" style="text-align:center;">
     .......
    </div>

Edit: I checked that, that and that.

Edit: This doesn't work. This stops all page scrolling.

$('#my_navbar_div').bind('touchmove', function(event)
{
    event.preventDefault();
});

This doesn't work. Doesn't changed anything in behaviour:

$('#my_navbar_div').hover(function() {
    $(document).bind('touchstart touchmove',function(){
        $("body").css("overflow","hidden");
    });
}, function() {
    $(document).unbind('touchstart touchmove');
});

I use twitter bootstrap 3. I added fixed top navbar. I have dropdown buttons on navbar. When user clicks on button, a dropdown menu opens. For desktop users it is ok. But for mobile users, when user scrolls down dropdown links, the page in the back also scrolls.

Is it possible to disable background page scroll when user scrolls dropdown links ?

Fiddle: http://jsfiddle/mavent/2g5Uc/1/

When user touches dropdown part and touches like the green arrow, background page scrolls like red arrow in this screenshot:

<nav class="navbar navbar-inverse navbar-fixed-top visible-xs" role="navigation">
    <div class="navbar-header" style="text-align:center;">
     .......
    </div>

Edit: I checked that, that and that.

Edit: This doesn't work. This stops all page scrolling.

$('#my_navbar_div').bind('touchmove', function(event)
{
    event.preventDefault();
});

This doesn't work. Doesn't changed anything in behaviour:

$('#my_navbar_div').hover(function() {
    $(document).bind('touchstart touchmove',function(){
        $("body").css("overflow","hidden");
    });
}, function() {
    $(document).unbind('touchstart touchmove');
});
Share Improve this question edited May 23, 2017 at 11:54 CommunityBot 11 silver badge asked Nov 15, 2013 at 21:13 trantetrante 34k49 gold badges196 silver badges275 bronze badges 2
  • jsfiddle/2g5Uc/4/show try this on your phone. I just checked on iPhone 4s – Aamir Afridi Commented Nov 19, 2013 at 12:19
  • Now I rechecked with Galaxy S3 and Chrome. Problem exists. – trante Commented Nov 19, 2013 at 21:22
Add a ment  | 

2 Answers 2

Reset to default 12 +50

To have more control of the scrolling abilities, you can try the following.

Disable all scrolling:

//create the exception variable.
var Scrollable = '.scrollable';

    //prevent all scrolling.
    $(document).on('touchmove', function(e) {
        e.preventDefault();
    });

    $('body').on('touchstart', Scrollable, function(e) {
        if (e.currentTarget.scrollTop === 0) {
            e.currentTarget.scrollTop = 1;
        } else if (e.currentTarget.scrollHeight === e.currentTarget.scrollTop + e.currentTarget.offsetHeight) {
            e.currentTarget.scrollTop -= 1;
        }
    });

The following is to allow scrolling on the exception:

    // Stops preventDefault from being called on document if it sees a scrollable div
    $('body').on('touchmove', Scrollable, function(e) {
        e.stopPropagation();
    });

What you want to do, is set a class called scrollable on every element that you want to be scrollable. You can of course always call the stopPropagation() when you want.

This way you can easily decide if you want something to be scrollable or not, overflow is not always working as you think it would.

I hope this helps.

Edit: You should also try to set the z-index of your #nav higher then the z-index of your body/content container.

I had the same problem and I solved using Billy's answer as well as adding a class to the body when the menu is clicked.

In your CSS:

    .no-scroll{
         overflow: hidden;
         position: fixed;
         width: 100%;
    }

In your JavaScript do something like:

    $(".navbar-toggle").click(function() {
         if ($("#mobile-toggle-menu").hasClass("in")) {
             return $("body").removeClass("no-scroll");
         } else {
             return $("body").addClass("no-scroll");
         }
    });

I use CoffeeScript so I'm not 100% sure the JavaScript syntax is correct but this did the job for me and it works both on Android and iPhone.

与本文相关的文章

发布评论

评论列表(0)

  1. 暂无评论