平滑滚动可以很好地处理同一页。但我希望平滑的滚动工作时,其他页面上点击。例如,当我单击主页上的服务链接时,我需要转到该服务页面并平滑地滚动到该div标记。你能帮我这个忙吗?
HTML代码
<ul class="secondmenu">
<li><a href="Home.html#main1">Home</a></li>
<li><a href="about.html#main2">About</a></li>
<li><a href="services.html#templatemo_main">Technology</a></li>
<li><a href="about.html#templatemo_main">Team</a></li>
</ul>Jquery
jQuery('.secondmenu a').click(function (e) {
e.preventDefault();
var target = this.hash;
if (this.href != '#') {
jQuery('html, body').animate({
scrollTop: jQuery(target).offset().top - 60
}, 1000);
} });发布于 2015-01-21 13:10:42
由于您必须转到页面加载的相应部分,因此您应该在ready函数中编写脚本。
<script>
$( document ).ready(function(e) {
var target = window.location.hash; //The hash property sets or returns the anchor part of a URL, including the hash sign
if (typeof target != "undefined") { //Checks if the variable is undefined incase no hash property exists
$('html, body').animate({
scrollTop: $(target).offset().top - 60
}, 1000);
e.preventDefault();
}
});
</script>希望这对你有帮助..:)
https://stackoverflow.com/questions/28059835
复制相似问题