I have the following jQuery, if the cookie url
is set, the scroll_to_href
function is called and the URL
variable is passed in to it.
My problem is with the scroll_to_href
funcion, I'm attempting to scroll to an anchor where the href equals the value of my url
var (it's re-declared as href
in the scroll_to_href
function). Currently this does not work, I'm not pulling in errors, could anyone suggest what the issue might be?
Thanks
$(document).ready(function(){
if($.cookie('url')){
var url = $.cookie('url');
scroll_to_href(url);
}
function scroll_to_href(url){
var href = $("a[href='"+ url +"']");
$('html,body').animate({scrollTop: href.offset().top},'slow');
}
});
I made a fiddle for testing your code: https://jsfiddle.net/dsbnw50r/
$(document).ready(function(){
// Static url
scroll_to_href('http://google.com');
function scroll_to_href(url){
var href = $("a[href='"+ url +"']");
$('html,body').animate({scrollTop: href.offset().top},'slow');
}
});
Either you cookie isn't set or the URL is different.
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.