【发布时间】:2016-01-23 11:44:56
【问题描述】:
我已经设法使用以下代码在单个页面上平滑滚动。
请注意,HTML 链接存储在 header.php 中并在多个页面中使用,下面是代码 sn-p:
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,并向下滚动到联系人锚点。
非常感谢您提供任何帮助或建议,我相信这是对某处代码的简单调整。
【问题讨论】:
标签: javascript jquery scroll jquery-animate smooth-scrolling