0

我正在使用一段简单的代码来让内部链接滚动到特定的 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);
        });
    });

});
4

1 回答 1

1

返回一个假值。

$('a[href^="#"]').on('click',function (e) {
   //whatever...
   return false;
});

这意味着“点击事件已被处理,不执行标准动作”。

于 2013-06-28T06:48:29.390 回答