Note that there are some explanatory texts on larger screens.

plurals
  1. POLinking to a page, loading the page, and scrolling to a div
    primarykey
    data
    text
    <p>I have a site that is made up of a long homepage with a series of sections with separate IDs, and a blog with single posts as separate pages.</p> <p>In the header there are menu items which, if a user is on the homepage, scroll down when clicked to the various panels using the IDs (#section-one, #section-two etc). This part is working nicely.</p> <p>I've made a second menu for the single blog posts, which is basically the same but links to the various panels on the homepage (www.website.com/home/#section-one etc). My hope is to get these to scroll nicely too, so that when a user clicks, the homepage loads and then scrolls down to the right section.</p> <p>I'm using the jQuery below to create the scroll effect. At the moment it uses <code>setTimeout</code> with a value of 0, and this is causing a problem - the scroll runs before the page is fully loaded, and stops in the wrong place. If I set the value of 'setTimeout' to a higher value it creates an ugly flash/pause when the homepage loads before the scroll runs.</p> <p>Is there an alternative way of ordering things, so that the page loads, and the script waits until it has loaded completely before the animation fires? </p> <p>Here is what I have so far:</p> <pre><code>var jump = function (e) { if (e) { e.preventDefault(); var target = $(this).attr("href"); } else { var target = location.hash; } $('html,body').animate({ scrollTop: $(target).offset().top - 70 }, 1500, 'easeInOutCubic'); }; $('html, body').hide(); $(document).ready(function () { $('a[href^=#]').bind("click", jump); if (location.hash) { setTimeout(function () { $('html, body').scrollTop(0).show(); jump(); }, 0); } else { $('html, body').show(); } }); </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.
    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