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

javascript - Prevent body scroll on click and close of overlay menu css - Stack Overflow

programmeradmin2浏览0评论

Currently I have an overlay CSS menu that is initiated through a click. However the page behind it is still scrollable. I'm sure this is a simple fix, but i'm new to css/js and any help would be great!

Currently I have a js toggle function that when .icon (hamburger icon) is hit the .mobilenav (overlay menu) is faded over the page.

What kind of function can I add to prevent body scrolling once this toggle function is enabled?

Anything I can easily add to this function below?

    $(document).ready(function () {
    $(".icon, .link").click(function () {
        $(".mobilenav").fadeToggle(700);
        $(".top-menu").toggleClass("top-animate");
        $(".mid-menu").toggleClass("mid-animate");
        $(".bottom-menu").toggleClass("bottom-animate");
    });
});

Currently I have an overlay CSS menu that is initiated through a click. However the page behind it is still scrollable. I'm sure this is a simple fix, but i'm new to css/js and any help would be great!

Currently I have a js toggle function that when .icon (hamburger icon) is hit the .mobilenav (overlay menu) is faded over the page.

What kind of function can I add to prevent body scrolling once this toggle function is enabled?

Anything I can easily add to this function below?

    $(document).ready(function () {
    $(".icon, .link").click(function () {
        $(".mobilenav").fadeToggle(700);
        $(".top-menu").toggleClass("top-animate");
        $(".mid-menu").toggleClass("mid-animate");
        $(".bottom-menu").toggleClass("bottom-animate");
    });
});
Share Improve this question edited Mar 25, 2015 at 17:36 Niroshan asked Mar 25, 2015 at 17:30 NiroshanNiroshan 192 silver badges7 bronze badges
Add a ment  | 

2 Answers 2

Reset to default 9

I guess you are looking for something like this:

document.body.style.overflow="hidden"

And to be able to scroll again, use

document.body.style.overflow="scroll"

Example:

document.body.onclick=function(){
  this.style.overflow="hidden"
}
Click anywhere to disable scrolling<hr>
Click anywhere to disable scrolling<hr>
Click anywhere to disable scrolling<hr>
Click anywhere to disable scrolling<hr>
Click anywhere to disable scrolling<hr>
Click anywhere to disable scrolling<hr>
Click anywhere to disable scrolling<hr>
Click anywhere to disable scrolling<hr>
Click anywhere to disable scrolling<hr>
Click anywhere to disable scrolling<hr>
Click anywhere to disable scrolling<hr>
Click anywhere to disable scrolling<hr>
Click anywhere to disable scrolling<hr>
Click anywhere to disable scrolling<hr>
Click anywhere to disable scrolling<hr>
Click anywhere to disable scrolling<hr>
Click anywhere to disable scrolling<hr>
Click anywhere to disable scrolling<hr>
Click anywhere to disable scrolling<hr>
Click anywhere to disable scrolling<hr>

Also, as you are using jQuery, something like this would work too:

$('body').css('overflow', 'hidden'); // disables scrolling
$('body').css('overflow', 'scroll'); // enables scrolling
    $(document).ready(function () {

        $(".icon, .link").click(function (event) {
            event.preventDefault();
            $('body').toggleClass('overflow');
            $(".mobilenav").fadeToggle(700);
            $(".top-menu").toggleClass("top-animate");
            $(".mid-menu").toggleClass("mid-animate");
            $(".bottom-menu").toggleClass("bottom-animate");
        });
    });

   Add the below piece of code into css file

  .overflow
  {
     overflow:hidden;
  }
发布评论

评论列表(0)

  1. 暂无评论