Reputation: 261
I have the following problem:
$('.gotoservices').click(function(){
$('html,body').scrollTo($("#services"),1000);
window.location.hash = "services";
return false;
});
This code works but for some reason the page flashes before the scrollTo
.
If I remove the window.location.hash
line the return false;
works and the page doesn't flash/flicker.
I have tried e.preventDefault
- doesn't work
I'm struggling to find any work around.
Cheers
Upvotes: 4
Views: 3157
Reputation: 966
In a generic sense, you can update the URL with HTML5 history
without the browser re-rendering anything:
history.pushState(null, null, '#myhash');
Of course, you will likely want to have fallbacks for older browsers, and you might want to do this only after your animation is done.
So, integrated with your code:
$('.gotoservices').click(function(e){
//Prevent default so the browser doesn't try to do anything
e.preventDefault();
var $hash = "#services";
$('html,body').scrollTo($($hash), 1000, function () {
//If the browser supports HTML5 history,
//use that to update the hash in the URL
if (history.pushState) {
history.pushState(null, null, $hash);
}
//Else use the old-fashioned method to do the same thing,
//albeit with a flicker of content
else {
location.hash = $hash;
}
});
});
Upvotes: 2
Reputation: 1990
Why don't you just make a function that specifically scrolls to #services? You're telling your web browser to go to #services and then to scroll to #services. Probably has a conflict with that. If you're trying to update the url to have the hash, then you probably need
$(window).bind('hashchange', function(e) {
e.preventDefault();
});
So that it doesn't try to "go to the hash".
Upvotes: 0