1

だから私は自分のページでjQueryスクロールがうまく機能するようになりました。でも、ひとつだけ変えたい。URLにアンカーテキストを表示したくありません。すなわち。#products または #contactinfo

HTML コード:

<div>
<a href="#products">Products</a>
<a href="#contactinfo">Contact info</a>
</div>

<div id="products"></div>

<div id="contactinfo"></div>

jQuery コード:

    $(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;
        });
    });
});
4

2 に答える 2

5

これを試して:

HTML:

<div>
    <span class="link" rel="#products">Products</span>
    <span class="link" rel="#contactinfo">Contact info</span>
</div>

<div style="height:800px;"></div>
<div id="products">products</div>
<div style="height:800px;"></div>
<div id="contactinfo">contact</div>
<div style="height:800px;"></div>

脚本:

$(document).ready(function(){
    $('.link').on('click',function (e) {
        $('html, body').stop().animate({
            'scrollTop': $($(this).attr('rel')).offset().top
        }, 900, 'swing', function () {});
    });
});

フィドル

于 2013-08-19T14:50:38.063 に答える
1

return false;関数の最後にa を追加するだけclickです。編集:そしておそらくこの行を削除してください..それはあなたが望んでいないことを正確に行いますか?どうやら?

window.location.hash = target;
于 2013-08-19T14:39:25.320 に答える