2

I have a little issue with Jquery and CSS, maybe the title sounds duplicate but I really searched and I can't find a solution, or maybe I missed something.

This is the situation. I'm building a website with an hamburger menu, but I want that it display like a lightbox. The first problem that I found is that the lightboxes doesn't fixed in mobile devices, so instead I used an overlay that works great, but on the mobile, the scroll feels weird. I discovered that it happens for the property "overflow: auto;" in the background div, but if I remove it the overlay will scroll along the page, and that is exactly what I don't want.

So, I need that the background have the property "overflow:visible;" when the page load to make the scroll smooth, and only when the overlay appears it will change to "auto" to fixed it. I tried to do this in the jquery script of the overlay, but when the overlay it close, the new property it keeps and the scroll becomes weird.

Here is the code:

    <div class='navbar-toggle' title='Menu'>

                <div class='bar1'></div>
                <div class='bar2'></div>
                <div class='bar3'></div>
            </div>
            <nav class="nav-hide overlay">
          <ul class="overlay-content">
            <li><a href="#">Inicio</a></li>
            <li><a href="#">Servicios</a></li>
            <li><a href="#">Citas</a></li>
            <li><a href="#">Portfolio</a></li>
            <li><a href="#">Blog</a></li>
          </ul>
        </nav>
            <div class="background-content">
            <div id="col">Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. 
Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.<br><br>
        Sed ut perspiciatis unde omnis iste natus error sit voluptatem accusantium doloremque laudantium, totam rem aperiam, eaque ipsa quae ab illo inventore veritatis et quasi architecto beatae vitae dicta sunt explicabo. Nemo enim ipsam voluptatem quia voluptas sit aspernatur aut odit aut fugit, sed quia consequuntur magni dolores eos qui ratione voluptatem sequi nesciunt. Neque porro quisquam est, qui dolorem ipsum quia dolor sit amet, consectetur, adipisci velit, sed quia non numquam eius modi tempora incidunt ut labore et dolore magnam aliquam quaerat voluptatem. Ut enim ad minima veniam, quis nostrum exercitationem ullam corporis suscipit laboriosam, nisi ut aliquid ex ea commodi consequatur? Quis autem vel eum iure reprehenderit qui in ea voluptate velit esse quam nihil molestiae consequatur, vel illum qui dolorem eum fugiat quo voluptas nulla pariatur?<br><br>
        But I must explain to you how all this mistaken idea of denouncing pleasure and praising pain was born and I will give you a complete account of the system, and expound the actual teachings of the great explorer of the truth, the master-builder of human happiness. No one rejects, dislikes, or avoids pleasure itself, because it is pleasure, but because those who do not know how to pursue pleasure rationally encounter consequences that are extremely painful. Nor again is there anyone who loves or pursues or desires to obtain pain of itself, because it is pain, but because occasionally circumstances occur in which toil and pain can procure him some great pleasure. 
To take a trivial example, which of us ever undertakes laborious physical exercise, except to obtain some advantage from it? But who has any right to find fault with a man who chooses to enjoy a pleasure that has no annoying consequences, or one who avoids a pain that produces no resultant pleasure?</div>
            </div>

            <script src='//cdnjs.cloudflare.com/ajax/libs/jquery/2.1.3/jquery.min.js'></script>

CSS:

body,
html {
  margin: 0;
  padding: 0;
  height: 100%;
}

nav,
.navbar-toggle {
  -webkit-touch-callout: none;
  -webkit-user-select: none;
  -khtml-user-select: none;
  -moz-user-select: none;
  -ms-user-select: none;
  user-select: none;
}

nav {
  position: absolute;
  z-index: 10;
  background-color: #E2B7C0;
  width: 100%;
  height: 100%;
  text-align: center;
  display: table;
  color: white;
}

.nav-hide { display: none; }

nav ul {
  display: table-cell;
  vertical-align: middle;
  padding-left: 0 !important;
}

nav ul li { list-style: none; margin-bottom: 15px;}

nav ul li a {
  font-size: 25px;
  color: inherit;
  font-weight: normal;
  text-decoration: none;
}

.navbar-toggle {
  position:fixed;
  top: 30px;
  left: 30px;
  width: 45px;
  height: 45px;
  z-index: 20;
  cursor: pointer;
}

.bar1,
.bar2,
.bar3 {
  width: 100%;
  height: 3px;
  margin-bottom: 10px;
  background-color: #1c1f72;
  transition: all 0.3s ease-in-out;
}

.navbar-on .bar1,
.navbar-on .bar2,
.navbar-on .bar3 { background-color: white; }

.navbar-on .bar1 {
  transform-origin: 10% 40%;
  transform: rotate(45deg);
}

.navbar-on .bar3 {
  transform-origin: 10% 40%;
  transform: rotate(-45deg);
}

.navbar-on .bar2 { background-color: transparent; }

#col{
  margin: 80px 30px;
  max-width: 600px;
}

.overlay{
        position: fixed;
        top: 0px;
        left: 0px;
        right: 0px;
        bottom: 0px;
    }

.overlay .overlay-content {
            height: 100%;
            overflow: scroll;
        }

.background-content{
        height: 100%;
    }

Script:

    $(function() {
    $('.navbar-toggle, nav').click(function(){
        $('.navbar-toggle').toggleClass('navbar-on');
        $('nav').fadeToggle();
        $('nav').removeClass('nav-hide');
        $('.overlay').css('display','');
        $('.background-content').css('overflow','auto'); //This is the line that I add
    });
});

https://jsfiddle.net/Trebohdz/t2nz3aoy/5/

The code of the overlay is a solution that I found here, I have tried to use .addClass and .removeClass events, even .on and .off, but it doesn't work or I don't know how integrate it to the code.

I hope you can help me.

1 Answer 1

1

you could create a css class and toggle:-

$(function() {
  $('.navbar-toggle, nav').click(function() {
    $('.navbar-toggle').toggleClass('navbar-on');
    $('nav').fadeToggle();
    $('nav').removeClass('nav-hide');
    $('.overlay').css('display', '');
    $('.background-content').toggleClass('overflow-auto');
  });
});
.overflow-auto {
  overflow: auto;
}
Sign up to request clarification or add additional context in comments.

1 Comment

Thank you, it works, but now when the overlay appears, the page moves to the top

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.