我目前正在使用此代码,我发现自动滚动到锚点:
$(function() {
$('nav a[href*=#]').bind('click',function(event){
var $anchor = $(this);
$('html, body').stop().animate({
scrollTop: $($anchor.attr('href')).offset().top - 100
}, 1000);
event.preventDefault();
});
});
我可以添加哪些内容以确保点击后将#anchor添加到网址?
我已经看过这个,但我不确定如何添加..
function() {
if(history.pushState) {
history.pushState(null, null, target);
}
else {
location.hash = target;
}
});
非常感谢!!!
答案 0 :(得分:2)
运行when the animation is done :( nice demo here)
$(function () {
$('nav a[href*=#]').on('click', function (event) {
var $anchor = $(this),
name = $anchor.attr("href").match(/#(.+)/i)[1];
$('html, body').stop().animate({
scrollTop: $($anchor.attr('href')).offset().top - 100
}, 1000, function () {
if (history.pushState) {
history.pushState(null, null, "#" + name);
} else {
location.hash = name;
}
});
event.preventDefault();
});
});