Reputation: 2674
Someone posted this in the past so I'll re-post it considering I'd love to see if there's any custom scripts out there with this functionality. Dare I say, so I don't have to code it myself :P
Excerpt from this SO post: "I already know the jQuery plugin ScrollTo, but I didn't find any way up to now to realize the following:
The users gets to my site (by typing, NOT by clicking a link on my page) domain.com/bla.php#foo
and the anchor "#foo" exists. Now I want that the browser of the user does NOT automatically scroll to "#foo", instead I want to smoothly scroll so that the element '#foo' is about in the middle of the view and NOT on the absolute top position of the users view.
Thanks so far!"
Upvotes: 0
Views: 606
Reputation: 169471
You can hack the solution to this. Add a script right in the to grab the current location. Set the scrolltop to be the top of the page then add a jQuery.ready(scrollTo) block for when the page is loaded.
A pseudo-code implementation is as follows.
<head>
<script src="jquery.js">
<script>
var loc = window.location;
var anchor = getAnchor(loc);
if (anchor !== undefined) {
removeAnchor(loc, anchor);
scrollTo(0);
$.ready(function() {
$.scollTo(anchor);
});
}
</script>
...
I hope that this will trigger before the browser has a chance to snap to the anchor. It may or may not work. Feel free to flesh it out yourself.
Upvotes: 1