简体   繁体   中英

Make Page Jump to Anchor on Reload

Greetings I'm working on a gallery script and would like to have it so when the page is loaded it is automatically positioned to the top of an anchor I have set. I've tried this code:

<script>location.href = "#trendnav";</script>


<a name="trendnav"></a>

And it doesn't seem to do anything. The more instantaneous this seems to the user, the better.

Try

  window.location.hash = "#VALUE";

Fiddle Demo 1

Fiddle Demo 2

or

window.scrollTo

Or, using your original approach:

$('span#godown').click(function(){location='#anchor';});

See here http://jsfiddle.net/dxNKG/1/ . I assigned a clickable span with the task of being the button that triggers it.

Actually, your original syntax location.href='#anchor' is correct, but you should fire it only after the DOM has loaded completely. So, either put the <script> section at the end of your page or do something like

window.onload=function(){window.location.href='#anchor';};

see here http://jsfiddle.net/bUaTT/ (without jQuery) or, since you are using jQuery:

$(function(){window.location.href='#anchor';})

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM