シンプルなコードで作れるページ内スムーススクローラー

Ads

Result

jQuery

//クリックイベント
$('a[href*="#"]').click(function(event){
        event.preventDefault();
        var f = this.href;
        //文字列を区切る
        var p = f.split("#");
        var t = p[1];
        var to = $("#"+t).offset();
        var tt = to.top;
        //取得した位置にスクロール
        $('html, body').animate({scrollTop:tt},500);
    });

html

<div id="top">HOME or go <a href="#bottom">bottom</a></div>
<a href="#top" id="bottom">top</a>

via

Scroll effect with local anchors – Jquery | Beschi’s Works

タイトルとURLをコピーしました