Scrolling to an Anchor using Transition/CSS3

前端 未结 10 909
-上瘾入骨i
-上瘾入骨i 2020-11-30 20:07

I have a series of links which are using an anchor mechanism:


      
10条回答
  •  一个人的身影
    2020-11-30 21:08

    Only mozilla implements a simple property in css : http://caniuse.com/#search=scroll-behavior

    you will have to use JS at least.

    I personally use this because its easy to use (I use JQ but you can adapt it I guess):

    /*Scroll transition to anchor*/
    $("a.toscroll").on('click',function(e) {
        var url = e.target.href;
        var hash = url.substring(url.indexOf("#")+1);
        $('html, body').animate({
            scrollTop: $('#'+hash).offset().top
        }, 500);
        return false;
    });
    

    just add class toscroll to your a tag

提交回复
热议问题