javascript - Sticky menu quickly jump to the top while header is not fully hidden or scroll to top -
i trying create sticky navigation using jquery. navigation jump top while header not yet hidden or scrolled top. want header scrolled top before navigation stick position.
question: how can make navigation stick position after header flow hidden top after scrolling?
this jsfiddle link http://jsfiddle.net/rodgefhi/577br99o/ illustration.
this code
<style type="text/css"> .clearer { clear: both; margin: 0; padding: 0; } .header { width: 84%; margin: 0 auto; } .dummy-content { width: 10%; margin: auto; } .fixed { position: fixed; top: 0; } .nav-placeholder { width: 84%; margin: 0 auto; background: transparent; height: auto; } nav { width: 84%; background: red; } nav ul li { list-style: none; float: left; padding: 10px; text-align: center; } nav ul li:hover { cursor: pointer; background: white; } </style> <script type="text/javascript" src="jquery-1.10.2.js"></script> <script type="text/javascript"> $(document).ready(function() { var navoffset = $("nav").offset().left; $("nav").wrap('<div class="nav-placeholder"></div>'); $(".nav-placeholder").height($('nav').outerheight()); $(window).scroll(function() { var scrollpos = $(window).scrolltop(); $(".value").html(scrollpos); if (scrollpos >= navoffset) { $("nav").addclass("fixed"); } else { $("nav").removeclass("fixed"); } }); }); </script> <div class="header"> <p>this header</p> <p>this header</p> <p>this header</p> <p>this header</p> <p>this header</p> </div> <nav> <ul> <li>one</li> <li>two</li> <li>three</li> <li>four</li> </ul> <div class="clearer"></div> </nav> <div class="value"></div> <div class="dummy-content"> <p>lorem ipsum dolor sit amet, consectetuer adipiscing elit. aenean commodo ligula eget dolor. aenean massa. cum sociis natoque penatibus et magnis dis parturient montes, nascetur ridiculus mus. donec quam felis, ultricies nec, pellentesque eu, pretium quis, sem. nulla consequat massa quis enim. donec pede justo, fringilla vel, aliquet nec, vulputate eget, arcu. in enim justo, rhoncus ut, imperdiet a, venenatis vitae, justo. nullam dictum felis eu pede mollis pretium. integer tincidunt. cras dapibus. vivamus elementum semper nisi. aenean vulputate eleifend tellus. aenean leo ligula, porttitor eu, consequat vitae, eleifend ac, enim. aliquam lorem ante, dapibus in, viverra quis, feugiat a, tellus. phasellus viverra nulla ut metus varius laoreet. quisque rutrum. aenean imperdiet. etiam ultricies nisi vel augue. curabitur ullamcorper ultricies nisi. nam eget dui. etiam rhoncus. maecenas tempus, tellus eget condimentum rhoncus, sem quam semper libero, sit amet adipiscing sem neque sed ipsum. nam quam nunc, blandit vel, luctus pulvinar, hendrerit id, lorem. maecenas nec odio et ante tincidunt tempus. donec vitae sapien ut libero venenatis faucibus. nullam quis ante. etiam sit amet orci eget eros faucibus tincidunt. duis leo. sed fringilla mauris sit amet nibh. donec sodales sagittis magna. sed consequat, leo eget bibendum sodales, augue velit cursus nunc,</p> </div>
thanks!
you getting left offset using this:
var navoffset = $("nav").offset().left;
i think want top value:
var navoffset = $("nav").offset().top;
updated fiddle: http://jsfiddle.net/577br99o/8/
Comments
Post a Comment