我正在使用一段简单的代码来让内部链接滚动到特定的 div。它按照我的预期工作,但它也会重新加载页面。滚动效果很好,只是我需要它来不重新加载页面。另外,如果这很重要,我会在我创建的 Wordpress 主题中使用它。
HTML:
<a href="#hiremenow">This may sound crazy, So hire me maybe?</a>
<div id="hiremenow"></div>
SCRIPT:
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script>
<script>
$(document).ready(function(){
$('a[href^="#"]').on('click',function (e) {
e.preventDefault();
var target = this.hash,
$target = $(target);
$('html, body').stop().animate({
'scrollTop': $target.offset().top
}, 900, 'swing', function () {
window.location.hash = target;
void(0);
});
});
});