Note that there are some explanatory texts on larger screens.

plurals
  1. POjQuery - how to make sticky navigation 'stick' in two cases? by default, and with top panel div expanded
    primarykey
    data
    text
    <p>I have a "sticky navigation" that "sticks" to the top (function changes its position to :fixed, top:0) when the user scrolls down past it. I also have a 'expandalbe' sign in area at the top of the page. First problem was when I expand the sign in area at the top of the page, the point at which the navbar 'sticks' changes. Now I got it to stick when the panel is open, however when it is closed, it sticks at the wrong point (seems to stick at the point of additional height of the #panel). see fiddle </p> <p>How do I make it so the #sticky_navigation sticks at top:0 in both cases -- when the #panel is open and closed/default?</p> <p><em>(Dead YouTube link removed)</em></p> <p>Thanks</p> <pre><code> **// FIRST VERSION sticky navigaiton ------------//** $(document).ready(function(){ var sticky_navigation_offset_top = $('#sticky_navigation').offset().top; var sticky_navigation = function(){ var scroll_top = $(window).scrollTop(); // our current vertical position from the top if (scroll_top &gt; sticky_navigation_offset_top) { $('#sticky_navigation').css({ 'position': 'fixed', 'top':0, 'left':0 }) } else { $('#sticky_navigation').css({ 'position': 'relative' }); } }; sticky_navigation(); $(window).scroll(function() { sticky_navigation(); }); }); </code></pre> <p><strong>UPDATE:</strong> Now I've got the it to work ONLY when the #panel is open. If it is closed, the #sticky_navigation does not initiate.</p> <pre><code>//sticky top nav PANEL OPEN ONLY $(document).ready(function(){ // grab the initial top offset of the navigation var sticky_navigation_offset_top = $('#sticky_navigation').offset().top; // our function that decides weather the navigation bar should have "fixed" css position or not. var sticky_navigation2 = function(){ var scroll_top1 = $(window).scrollTop(); // our current vertical position from the top scroll_top1 = $(window).scrollTop() - $('#panel').height(); // if we've scrolled more than the navigation, change its position to fixed to stick to top, // otherwise change it back to relative if (scroll_top1 &gt; sticky_navigation_offset_top ) { $('#sticky_navigation').css({ 'position': 'fixed', 'top':0, 'left':0 }) } else { $('#sticky_navigation').css({ 'position': 'relative' }); } }; // run our function on load sticky_navigation2(); // and run it again every time you scroll $(window).scroll(function() { sticky_navigation2(); }); }); </code></pre>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload