jQuery在其他网页上平滑滚动
问题描述:
我已经设法使用以下代码在单个页面上实现平滑滚动。jQuery在其他网页上平滑滚动
注意的HTML链接存储在header.php文件,下面跨越多个页面中使用的代码片段:
HTML脚本:
<a class="item" href="index.php#contact">
<a name="contact"></a>
JS脚本:
$('a[href="index.php#contact"]').click(function (e) { // user clicks on html link
e.preventDefault(); // prevent the default behaviour that occurs in the browser
var targetOffset = $('a[name="contact"]').offset().top; // define a variable to point at a particular section & offset from the top of browser
$('body').animate(// create animation
{ scrollTop: targetOffset }, 1000); // scrollTop property = target variable
});
问题:
当我转到其他网页并单击联系人链接时,它不会链接回index.php#contact
并向下滚动到联系人定位点。
任何帮助或建议非常感谢我确信它在代码中的一个简单的调整。
答
检查你的href它应该是这样的:index.php /#contact
我已经尝试过,并且执行相同的问题。 – Harry