I thought for myself! In case anyone else has the same problem and comes across this, here is my revised code and the best (albeit amateurish) explanation of what I did:
JS:
$(document).ready(function() { $('#drawercontents').hide(); var top =$('#nav').offset().top - parseFloat($('#nav').css('marginTop').replace(/auto/, 0)); $('#drawer a').click(function(e){ e.preventDefault(); $('#drawercontents').toggle('fast', function() { top =$('#nav').offset().top - parseFloat($('#nav').css('marginTop').replace(/auto/, 0)); }); }); $(window).scroll(function () {
HTML:
<!DOCTYPE html> <html> <head> <script class="jsbin" src="http://ajax.googleapis.com/ajax/libs/jquery/1/jquery.min.js"></script> <meta charset=utf-8 /> <title>JS Bin</title> <style> article, aside, figure, footer, header, hgroup, menu, nav, section { display: block; } body { margin: 0; padding: 0; } #article { width: 400px; height: 1000px; float: left; margin-right: 20px; padding: 20px; background: #eee; } #nav { width: 200px; float: left; background: #ff0; padding: 20px; } #drawer { width: 660px; padding: 20px; color:#fff; background: #000; margin-bottom: 10px; } .fixed { position: fixed; left: 460px; top: 0px; } </style> </head> <body> <div id="drawer"> <a href="#">Click here to toggle drawer</a> <p id="drawercontents">Here is the stuff in the drawer. It is hidden by default. When it is shown there is a problem with the nav jumping to the top of the page when we scroll.</p> </div> <div id="article"> <h2>This is a long article!</h2> <p>Lorem ipsum dolor sit amet, consectetur adipiscing elit pellentesque sed egestas id, iaculis ut erat. Praesent ut neque vel dolor lacinia eleifend at sit amet sem, etc etc</p> <p>Div height has been set to 1000px to force scrolling without adding too much filler text</p> </div> <div id="nav"> This should follow down the page once we scroll past the start of the article, and 'stick' back in place when we are back at the top. </div> </body> </html>
Working example on JS Bin - http://jsbin.com/alibi3/9
top set as a global variable, so it can be used between functions. First, it is set to load the document, then it is redefined whenever the cursor switch function $('#drawer a').click(function(e) launched.
So, whenever $(window).scroll , it has the correct top value to work and behaves the way I want.
source share