Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>There are two events you need to handle to get this to work correctly. First is the scroll event which you are pretty close on. The only thing you might want to do is to use <a href="http://api.jquery.com/offset/" rel="nofollow">offset</a> to get the current left position value based on the document.</p> <p>The other event which is not yet handled is the <a href="http://api.jquery.com/resize/" rel="nofollow">resize</a> event. If you don't handle this then once a left position is defined your element (header) will be there always regardless of whether or not the user resizes the window.</p> <p>Basically something like this should work:</p> <pre><code>var headeroffset; var header = $('#homeheader'); // handle scroll $(window).scroll(function() { // auto when not defined if (headeroffset === undefined) { // capture offset for current screen size headeroffset = header.offset(); } // calculate offset var leftOffset = headeroffset.left - $(this).scrollLeft(); // set left offset header.css('left', leftOffset); }); // handle resize $(window).resize(function() { // remove left setting // (this stops the element from being stuck after a resize event if (header.css('left') !== 'auto') { header.css('left', ''); headeroffset = undefined; } }); </code></pre> <p>JSFiddle example: <a href="http://jsfiddle.net/infiniteloops/ELCq7/6/" rel="nofollow">http://jsfiddle.net/infiniteloops/ELCq7/6/</a></p> <p><a href="http://jsfiddle.net/infiniteloops/ELCq7/6/show" rel="nofollow">http://jsfiddle.net/infiniteloops/ELCq7/6/show</a></p> <p>This type of effect can be done purely in css however, i would suggest taking a look at the full page app series Steve Sanderson did recently.</p> <p><a href="http://blog.stevensanderson.com/2011/10/05/full-height-app-layouts-a-css-trick-to-make-it-easier/" rel="nofollow">http://blog.stevensanderson.com/2011/10/05/full-height-app-layouts-a-css-trick-to-make-it-easier/</a></p> <p>As an example you could do something like this:</p> <p><a href="http://jsfiddle.net/infiniteloops/ELCq7/18/" rel="nofollow">http://jsfiddle.net/infiniteloops/ELCq7/18/</a></p>
    singulars
    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.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      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