admin管理员组

文章数量:1289633

When the user clicks a button, I want his browser to automatically scroll to an <a> with a certain href (let's call it "abc"). Ideally the scrolling would be nicely animated in some way.

When the user clicks a button, I want his browser to automatically scroll to an <a> with a certain href (let's call it "abc"). Ideally the scrolling would be nicely animated in some way.

Share Improve this question asked Sep 9, 2009 at 4:57 Tom LehmanTom Lehman 89.4k72 gold badges205 silver badges279 bronze badges 2
  • 1 Animations are distracting and get in the way of using the app. Unless your app is an animation tool of some kind. – tsilb Commented Sep 9, 2009 at 7:05
  • What if the element he's scrolling to isn't visible without scrolling? A ScrollTo is good UX in that case. – Dave Ward Commented Jul 3, 2010 at 1:40
Add a ment  | 

3 Answers 3

Reset to default 5

Give a look to the jQuery.scrollTo plugin.

With that plugin you could simply:

$.scrollTo('a[href=abc]');

Way simpler:

element_to_scroll_to = document.getElementById('anchorName2');
element_to_scroll_to.scrollIntoView();

Even no need for jQuery ;)

You don't need any plugin.

$(document.documentElement).animate({
    scrollTop: $('a#abc').offset().top
});

本文标签: javascriptScroll the page to an ltagt with a particular href with jQueryStack Overflow