Javascript 滚动链接到另一页';s剖面平滑
我有Javascript 滚动链接到另一页';s剖面平滑,javascript,jquery,html,dom,Javascript,Jquery,Html,Dom,我有index.php,它有不同的部分,如top、service、aboutus和contactus。我有头文件。php在includes文件夹中,我将它分别包含到每个文件中。我为部分添加了平滑滚动的jquery 现在我有了具有相同标题的gallery.php。现在我想调用gallery到index/section的每个部分。因此,我将href=“#服务”更改为href=index.php#服务。现在它可以从索引和图库页面工作 问题: 如果我从index.php调用该部分,平滑滚动就可以工作。当
index.php
,它有不同的部分,如top
、service
、aboutus
和contactus
。我有头文件。php
在includes文件夹中,我将它分别包含到每个文件中。我为部分添加了平滑滚动的jquery
现在我有了具有相同标题的gallery.php
。现在我想调用gallery到index/section的每个部分。因此,我将href=“#服务”
更改为href=index.php#服务
。现在它可以从索引和图库页面工作
问题:
如果我从index.php
调用该部分,平滑滚动就可以工作。当我从gallery.php
调用一个部分时,它只加载index.php
的上部,然后突然移动到被调用的部分。我希望它能够平稳地移动。我如何才能做到这一点
$(document).ready(function(){
$("a").on('click', function(event) {
if (this.hash !== "") {
// Prevent default anchor click behavior
event.preventDefault();
// Store hash
var hash = this.hash;
// Using jQuery's animate() method to add smooth page scroll
// The optional number (800) specifies the number of milliseconds it takes to scroll to the specified area
$('html, body').animate({
scrollTop: $(hash).offset().top
}, 800, function(){
// Add hash (#) to URL when done scrolling (default click behavior)
window.location.hash = hash;
});
} // End if
});
});
您的代码看起来不错,但是您必须使footer.php并从footer.php调用函数,这样您的问题才会重新出现
在我使用的时候,我们必须从footer调用所有与jquery相关的东西,但这取决于您的需求,所以在这种情况下,您可以从footer.php调用,您会遇到一些javascript代码冲突。你必须在代码中进行调试。我在这里添加了jquery代码。请检查。你的意思是我必须在页脚或超链接中添加脚本文件吗?我在页脚中有相同的链接。它也像页眉一样工作。