I want to navigate to an anchor point on a new page, but I want the page to load at the top then immediately smooth scroll to the relevant anchor point. Can this be done?
I am a complete newbie with Javascript.
This is the js I currently use for smooth scrolling within the current page. I just apply a class of 'scroll' on the link.
Thanks very much!
<script>
$(function(){
$('.scroll').on('click',function(e) {
e.preventDefault();
$('html, body').animate({ scrollTop: $($(this).attr('href')).offset().top + 'px' }, 1000, 'swing');
});
});
</script>
As browsers automatically detect the
hash
and take you to that position...It occurs to me that you could first reset the scroll position to 0 and then made the smooth scrolling.
Something like...
Edit: Move the
scroll(0,0)
outside$(function(){...});
to prevent flickering.Also, Snippet with working example was added.
The effect is best appreciated when viewed in full screen