在寻找解决的办法,通过谷歌和stackoverflow,以处理平滑滚动到锚点很显然,许多相同页面的解决方案存在。处理跨多个页面的多个锚点之间的平滑滚动似乎仅限于在stackoverflow上的一个QA。然而,这个解决方案并不适合我。
虽然我刚刚处理初学者处理java和CSS,但我想通过将几种解决方案合并到一个至少适用于Firefox和Chrome(未在其他浏览器上测试)的解决方案来传递解决方案。
我希望有人可以看看这一点,并提出一些建议: 1)使其更跨浏览器兼容 2)把它清理干净
最后,这里有一些条件我的有它的工作没有问题:
多页 - 多个锚 引导3个 多个jQuery函数
$(function() {
$('[data-toggle="elementscroll"]').click(function() {
if (location.pathname.replace(/^\//,'') == this.pathname.replace(/^\//,'')
&& location.hostname == this.hostname) {
var target = $(this.hash);
target = target.length ? target : $('[name=' + this.hash.slice(1) +']');
if (target.length) {
$('html,body').animate({
scrollTop: target.offset().top -57 //head space
}, 1000); //scroll speed
return false;
}
}
});
});
$(function() {
$('html, body').hide();
if (window.location.hash) {
setTimeout(function() {
$('html, body').scrollTop(0).show();
$('html, body').animate({
scrollTop: $(window.location.hash).offset().top -57 // head space
}, 1000) //scrollspeed
}, 0);
}
else {
$('html, body').show();
}
});