最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - How to navigate to a section of a page using animation? - Stack Overflow

programmeradmin2浏览0评论

in one page, if we have html like this

and we click on tag it will navigate to particular section, that's fine

<a href="#shushi">Sushi</a>
<a href="#bbq">BBQ</a>

Sample Page:
<div id='sushi'></div>
<div id='bbq'></div>

but is it possible to add some animation effect when we click on it shushi and bbq and navigate to that section?

something like this :

$([some id]).animate({scrollTop: $elem.height()}, 800);

in one page, if we have html like this

and we click on tag it will navigate to particular section, that's fine

<a href="#shushi">Sushi</a>
<a href="#bbq">BBQ</a>

Sample Page:
<div id='sushi'></div>
<div id='bbq'></div>

but is it possible to add some animation effect when we click on it shushi and bbq and navigate to that section?

something like this :

$([some id]).animate({scrollTop: $elem.height()}, 800);
Share Improve this question edited Jun 3, 2014 at 17:13 vir asked Jun 3, 2014 at 17:07 virvir 551 silver badge6 bronze badges
Add a ment  | 

3 Answers 3

Reset to default 2

Working DEMO

$(document).on("click","a",function(e){
        e.preventDefault();
        var id = $(this).attr("href"),
            topSpace = 30;
        $('html, body').animate({
          scrollTop: $(id).offset().top - topSpace
        }, 800);
    });

This should work:

$('html, body').animate({scrollTop: $elem.scrollTop()}, 800);

http://api.jquery./scrolltop/

In your stylesheet add this code it will work fine

html{scroll-behavior: smooth;}
发布评论

评论列表(0)

  1. 暂无评论