Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p><strong>EDITED:</strong> You just need to detect the height of the fixed header and subtract that from the <code>scrollToPosition</code> which you were doing correctly. The issue is the <code>window.location.hash = "" + target;</code> jumps the page to top of the element with that id. So if you animate there like you were doing and then change to that hash it will "bounce back" like you described. Here is the first way we can combat this:</p> <pre><code>// Get the height of the header var headerHeight = $("div#header").height(); // Attach the click event $('a[href*=#]').bind("click", function(e) { e.preventDefault(); var target = $(this).attr("href"); //Get the target var scrollToPosition = $(target).offset().top - headerHeight; $('html').animate({ 'scrollTop': scrollToPosition }, 600, function(){ window.location.hash = "" + target; // This hash change will jump the page to the top of the div with the same id // so we need to force the page to back to the end of the animation $('html').animate({ 'scrollTop': scrollToPosition }, 0); }); $('body').append("called"); }); </code></pre> <p>Here's a <strong>new</strong> jsfiddle for this first method: <a href="http://jsfiddle.net/yjcRv/1/">http://jsfiddle.net/yjcRv/1/</a></p> <p><strong>FURTHER EDIT:</strong> An even better way to control hash change events is to use a plugin like <a href="http://www.asual.com/jquery/address">jQuery Address</a>. With this you can utilise your hashchange events much more. Here's an example usage:</p> <pre><code>// Get the height of the header var headerHeight = $("div#header").height(); $.address.change(function(evt){ var target = "#" + evt["pathNames"][0]; //Get the target from the event data // If there's been some content requested go to it…else go to the top if(evt["pathNames"][0]){ var scrollToPosition = $(target).offset().top - headerHeight; $('html').animate({ 'scrollTop': scrollToPosition }, 600); }else{ $('html').animate({ 'scrollTop': '0' }, 600); } return false; }); // Attach the click event $('a').bind("click", function(e) { // Change the location $.address.value($(this).attr("href")); return false; }); </code></pre> <p>Live example here: <a href="http://www.vdotgood.com/stack/user3444.html">http://www.vdotgood.com/stack/user3444.html</a></p> <p><strong>NOTE: You don't need to add the hash to your links href attribute now.</strong> Here's a link that you could target with a jQuery selector:</p> <pre><code>&lt;!-- This is correct --&gt; &lt;a href="/target" class="myclass"&gt;Target&lt;/a&gt; &lt;!-- These are incorrect --&gt; &lt;a href="/#/target" class="myclass"&gt;Target&lt;/a&gt; &lt;a href="#/target" class="myclass"&gt;Target&lt;/a&gt; </code></pre> <p>To target this link you'd use a selector like:</p> <pre><code>$("a.myclass").click(function(){ $.address.value($(this).attr("href")); return false; }); </code></pre> <hr> <p>jQuery Address does in fact look for links that have the following attribute:</p> <pre><code>&lt;a href="/target" rel="address:/target"&gt;Target&lt;/a&gt; </code></pre> <p>The <code>rel</code> attribute here contains <code>address:</code> followed by a relative url defined by you in this case <code>/target</code>. If you use this, jQuery Address will detect the link and fire the hash change event automatically.</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