Fixed nav-bar dissapearing after scroll up
Hi!I am having a problem which I don't know how to fix anymore.I have a nav-bar which i would like to make it fixed on scroll.I have the following code:
- <div class="nav-placeholder"></div>
- <nav class="nav-bar" role="navigation" >
- <div class="wrapper">
- <div class="medium-down--hide">
- {% comment %}
- List out your main-menu linklist (default)
- More info on linklists:
- - http://docs.shopify.com/themes/liquid-variables/linklists
- {% endcomment %}
- <!-- begin site-nav -->
- <ul class="site-nav" id="AccessibleNav">
- {% for link in linklists.main-menu.links %}
- {% comment %}
- Create a dropdown menu by naming a linklist the same as a link in the parent nav
- More info on dropdowns:
- - http://docs.shopify.com/manual/your-website/navigation/create-drop-down-menu
- {% endcomment %}
- {% assign child_list_handle = link.title | handleize %}
- {% if linklists[child_list_handle].links != blank %}
- <li class="site-nav--has-dropdown{% if link.active %} site-nav--active{% endif %}" aria-haspopup="true">
- <a href="{{ link.url }}" class="site-nav__link">
- {{ link.title }}
- <span class="icon icon-arrow-down" aria-hidden="true"></span>
- </a>
- <ul class="site-nav__dropdown">
- {% for childlink in linklists[child_list_handle].links %}
- <li{% if childlink.active %} class="site-nav--active"{% endif %}>
- <a href="{{ childlink.url }}" class="site-nav__link">{{ childlink.title | escape }}</a>
- </li>
- {% endfor %}
- </ul>
- </li>
- {% else %}
- <li {% if link.active %}class="site-nav--active"{% endif %}>
- <a href="{{ link.url }}" class="site-nav__link">{{ link.title }}</a>
- </li>
- {% endif %}
- {% endfor %}
- </ul>
- <!-- //site-nav -->
- </div>
- <div class="large--hide medium-down--show">
- <div class="grid">
- <div class="grid__item one-half">
- <div class="site-nav--mobile">
- <button type="button" class="icon-fallback-text site-nav__link js-drawer-open-left" aria-controls="NavDrawer" aria-expanded="false">
- <span class="icon icon-hamburger" aria-hidden="true"></span>
- <span class="fallback-text">{{ 'layout.navigation.menu' | t }}</span>
- </button>
- </div>
- </div>
- <div class="grid__item one-half text-right">
- <div class="site-nav--mobile">
- <a class="cart" href="/cart">
- <a href="/cart" class="js-drawer-open-right site-nav__link" aria-controls="CartDrawer" aria-expanded="false">
-
- {% include 'svg-cart' %}{% if cart.item_count > 0 %}<div style="display:inline-block;position:absolute;font-weight:bold;">{{ cart.item_count }}</div>{% endif %}
-
- </a>
-
- </a>
- </div>
- </div>
- </div>
- </div>
- </div>
- </nav>
My target in the "nav-bar" element which I make it fixed with Jquery as follows:
- </script>
- <script src="http://code.jquery.com/jquery-latest.min.js" type="text/javascript"></script>
- <script>
- $(document).scroll(function() {
- var y = $(document).scrollTop(), //get page y value
- header = $(".nav-bar"); // your div id
- if(y >= 100) {
- $(".nav-placeholder").height($(".nav-bar").outerHeight());
- header.css({position: "fixed", "top" : "0", "left" : "0"});
- } else {
- $(".nav-placeholder").height(0);
- header.css("position", "static");
- }
- });
</script>
I have made a separate div "nav-placeholder" in order to help the "nav-bar" become fixed and the following css for the nav-bar :
- .nav-bar {
- background-color: #116466;
- font-family:"Diverplate";
- text-transform:uppercase;
- font-weight:bold;
- z-index:99999;
- overflow-x: auto;
- height:80px;
- width:100%;
- position:static;
- }
My problem is in the fact that every time I scroll up and the nav-bar reaches the top of the screen,it just vanishes.I have already set the z-index as I thought initially this might be the issue along with a height for it and also the top of the header that is above it to 0.I have also set the top of the content as the nav-bar was initially covering it's title when I set position:fixed.I have tried almost everything I found on the internet but nothing worked as it won't stay fixed at all on the screen.I don't have any ideas left so I would really appreciate if you can share your thoughts on why this might not be working.Best regards//