Fixed menu in refresh (F5)

0

I made a menu that perceives when the scroll passes from a certain point and stays fixed, until all right, but when I rolled the page a little lower, and updated (F5) I noticed that the menu disappeared, and then scrolling once up / down scrolls it back up.

Does anyone know what I could do for this not to occur, pop up the tbm menu when giving a refresh / f5?

Follow the 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><divid="menusite" class="container-fluid">
  <div class="row">
    <div class="col-md-12"> Menu </div>
  </div>
<div>
    
asked by anonymous 27.10.2017 / 16:12

1 answer

2

You can store your scroll to sessionStorage and make validations from there, here is 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, for you to do n forms using sessionStorage , just improve for your need.

    
27.10.2017 / 17:04