Fixed menu in refresh (F5)

Asked

Viewed 113 times

0

I’ve created a menu that understands when the scroll goes from a certain point and is fixed, until then everything right, but when I rolled the page a little lower, and updated (F5) I noticed that the menu is gone, and then when scrolling once the scroll up/down it reappears.

Does anyone know what I could do to make it not occur, pop up the tbm menu while giving a refresh/F5?

Follows code below.

$(document).ready(function(){
  var div = $('#menu');
  $(window).scroll(function () {
      if ($(this).scrollTop() > 35) {
          div.addClass("menu-fixo");
      } else {
          div.removeClass("menu-fixo");
      }
  });
});
#menu {
  min-height: 112px;
  background-color: tomato;
}
.menu-fixo {
  position: fixed;
  top: 0px;
  left: 0;
  right: 0;
  z-index: 100;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>

<div id="menusite" class="container-fluid">
  <div class="row">
    <div class="col-md-12"> Menu </div>
  </div>
<div>

  • Do you have a URL to check this behavior? I imagine the script is being loaded or fired after the scroll happens, but I wanted to be sure before giving an answer

  • @Celsobessa have no friend, is running local. The script is just that right there.

1 answer

2


You can store your scroll in sessionStorage and validate from there, follows an example of how it would work.

if(sessionStorage.scroll){
  $('.menu').show()
}

$(document).ready(function(){
  var div = $('.menu');
  $(window).scroll(function () {
      if ($(this).scrollTop() > 35) {
          sessionStorage.scroll = true
          div.show()
      } else {
          div.hide();
      }
  });
});
.menu{
  position: fixed;
  width: 100%;
  height: 50px;
  background: red;
  display: none;
}


.content{
  height: 2000px;
}
<!DOCTYPE html>
<html>
<head>
  <meta charset="utf-8">
  <meta name="viewport" content="width=device-width">
  <title>JS Bin</title>
</head>
<body>
  <div class="menu"></div>
  <div class="content">
<script src="https://code.jquery.com/jquery-3.1.0.js"></script>
    dasd
  </div>
</body>
</html>

Remembering that this is a simple example, da para você fazer de n forms using sessionStorage, just improve for your need.

Browser other questions tagged

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