When pinning menu at the top after scroll, everything below moves

Asked

Viewed 2,072 times

1

Whoa, guys, first I’ll explain what I’m gonna do.

I want you to when using scroll and going through my menu, it gets fixed at the top. My site will have the menu in this style EXAMPLE SITE, an initial part which in my case 100% of the screen (is set to 100% height in css), and only then comes the menu.

So in order for me to make this possible, I took the example of another question and put this in my code.

jQuery("document").ready(function($){

var nav = $('.nav-container');

$(window).scroll(function () {
    if ($(this).scrollTop() > 750) {
        nav.addClass("f-nav");
    } else {
        nav.removeClass("f-nav");
    }
});

In css I put:

.f-nav{ z-index: 9999; position: fixed; left: 0; top: 0; width: 100%;}

And in html, I put the class "Nav-container" in the session that encompasses my entire menu, in case a < Nav >.

However, the following errors occur:

As the height to fix the menu is set in pixels in case the 750 that is in the JS code, and my home screen is set in % (100%) when I increase the screen or put to expand the browser, the position that the fixed menu changes places, not exactly when it passes by .

And another error is happening, when the fixed menu, everything below it moves about 30 pixels up, it’s as if the menu when it’s fixed frees up space for the content to go up, and it’s getting ugly.

Does anyone know how to correct these mistakes?

  • puts position: Absolute in the menu, and in the container, ai in the margin-top container: 30px; I think that only this already solves, if n solve, puts the executable code to see us

1 answer

0


One solution to this is to do a control margin-top of div below the menu. The code below does this. It fixes the menu when the div exits the screen and adds an upper margin of the same menu height to the div below the menu, and does the opposite process as well, when the div before the menu back to the screen with the scroll:

$(window).on('scroll load', function(){
   var win_scl = $(window).scrollTop(); // valor do scroll da janela
   var nav = $('.nav-container'); // menu
   var nav_ant = nav.prev('div'); // div antes do menu
   var nav_hgt = nav.outerHeight(); // altura do menu
   var nav_ant_hgt = nav_ant.outerHeight(); // altura da div antes do menu
   var nav_ant_top = nav_ant.offset().top; // distância da div antes do menu ao topo
   var nav_ant_dst = nav_ant_top-win_scl; // distancia do final da div antes do menu ao topo da janela

   if(nav_ant_dst <= nav_ant_hgt && win_scl > nav_ant_hgt) {
      nav.addClass("f-nav");
       // adiciono margem superior à primeira div depois do menu
      $(".nav-container+section").css('margin-top',nav_hgt+'px');
   }else{
      nav.removeClass("f-nav");
       // retiro margem superior à primeira div depois do menu
      $(".nav-container+section").css('margin-top','0');
   }
});
html, body{ margin: 0; padding: 0; height: 100%; }
.f-nav{ z-index: 9999; position: fixed; left: 0; top: 0; width: 100%;}
.nav-container{
   display: block; width: 100%; height: 50px; background: yellow;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div style="display: block; width: 100%; height: 100%; background: black; color: white;">
   Role para baixo
</div>
<div class="nav-container">
   menu
</div>
<section style="display: block; width: 100%; background: gray; position: relative;">
   Lorem ipsum dolor sit amet, consectetur adipiscing elit. Nullam fringilla ornare vehicula. Quisque sit amet placerat mauris. Cras sed risus luctus, gravida tortor id, tristique felis. Nam nisl nisl, semper viverra augue a, tempus feugiat tellus. Quisque in justo volutpat felis finibus euismod. Quisque ullamcorper vel ipsum vel porta. In tortor lacus, sagittis et erat et, semper venenatis odio. Sed feugiat mollis ornare. Sed iaculis eros lacus, nec efficitur elit suscipit at. Vivamus ullamcorper volutpat gravida. Vestibulum accumsan vehicula felis, vel gravida mauris consectetur quis.
</section>
<br /><br /><br /><br /><br /><br />
<br /><br /><br /><br /><br /><br />
<br /><br /><br /><br /><br /><br />
<br /><br /><br /><br /><br /><br />
<br /><br /><br /><br /><br /><br />
<br /><br /><br /><br /><br /><br />
<br /><br /><br /><br /><br /><br />

  • Look, thank you so much! When I increase the screen size, screen resolution, etc... the menu only fixed when it reaches the top 0, this I got and it worked, thank you. However, I couldn’t get you to add the margin top, so the bottom content doesn’t move a little higher. *PS: I need to take an urgent js course

  • The content below is a Section, and inside that Section has a first div where I started to put the items, but the general parent is the same Section. The menu I made with NAV, and below the menu is this Section. For example: In what you sent, you put the Nav-container class in a div, in my I put it in the NAV, and it worked.. only missing the bottom not moving kk

  • @Lukasmonteiro Then change the +div in the code by +section on the 2 lines it has +div.

  • even if there are other sections below this first section? wouldn’t disturb anything down?

  • @Lukasmonteiro No, he only takes the first.

  • @Lukasmonteiro Until I updated the answer and exchanged for section.

  • hasn’t worked yet, I must be doing something wrong... I changed the two +div, by +Section... I’ll redo everything again

  • that I had done, read the description next door and put "header" in place of div.

  • Hey, kid, I got my problem solved. It was just necessary there in css to put a top padding for Section below the menu... pro size of the Section get big enough to get behind the menu, then it worked. Thanks for your help.

  • @Lukasmonteiro ah yes, if you change the margin-top by padding-top in my answer is able to work also.

Show 5 more comments

Browser other questions tagged

You are not signed in. Login or sign up in order to post.