Timeline for How to scroll an HTML page to a given anchor
Current License: CC BY-SA 4.0
8 events
when toggle format | what | by | license | comment | |
---|---|---|---|---|---|
Jul 28, 2021 at 21:11 | history | edited | Peter Mortensen | CC BY-SA 4.0 |
Active reading [<https://en.wikipedia.org/wiki/JavaScript>].
|
Jan 5, 2021 at 14:32 | comment | added | Nathan G | sometimes this is not accurate... when the page has dynamic parts with lazy load | |
Aug 25, 2013 at 18:32 | comment | added | Jazzy | I had some luck with this approach: <a data-hash="about">About</a> <script> $("[data-hash]").click(function() { var data = $(this).attr("data-hash"); $(document.body).animate({ 'scrollTop': $("#"+data).offset().top }, 500); }); </script> | |
Jan 23, 2013 at 21:13 | comment | added | gnarf |
@CodeJoust - I'm on the jQuery team, I've read it many times, and yes $("#selector") is optimized but $("#selector,a[name='selector']") won't go through the same optimizations as quickly. I suppose my 2.5 year old comment is a little strange sounding. The "optimization" is avoiding the a[name='selector'] search if it finds the id, not optimizing searching for the id.
|
|
Jan 23, 2013 at 7:04 | comment | added | CodeJoust | @gnarf - There isn't any need to optimize the '#' selectors in jQuery - they're already optimized for you. It's fairly easy to see if you've read the jQuery source code. | |
Jul 2, 2010 at 7:36 | comment | added | gnarf |
As far as creating a selector to find the <h1 id="anchorName"> or an <a name="anchorName"> , use $('#'+hash+',a[name='+hash+']') or slightly optimized $(document.getElementById(hash) || 'a[name='+hash+']') which will search for the element by id first, and resort to searching the a's only if one isn't found.
|
|
Jul 2, 2010 at 6:28 | history | edited | jAndy | CC BY-SA 2.5 |
added 7 characters in body; added 44 characters in body; added 194 characters in body; added 60 characters in body
|
Jul 2, 2010 at 6:22 | history | answered | jAndy | CC BY-SA 2.5 |