How to scroll to an element in jQuery?

156,219

Solution 1

Check jQuery.ScrollTo, I think that's the behavior that you want, check the demo.

Solution 2

For my problem this code worked, I had to navigate to an anchor tag on page load :

$(window).scrollTop($('a#captchaAnchor').position().top);

For that matter you can use this on any element, not just an anchor tag.

Solution 3

Like @user293153 I only just discovered this question and it didn't seem to be answered correctly.

His answer was best. But you can also animate to the element as well.

$('html, body').animate({ scrollTop: $("#some_element").offset().top }, 500);

Solution 4

You can extend jQuery functionalities like this:

jQuery.fn.extend({
scrollToMe: function () {
    var x = jQuery(this).offset().top - 100;
    jQuery('html,body').animate({scrollTop: x}, 500);
}});

and then:

$('...').scrollToMe();

easy ;-)

Solution 5

Check out jquery-scrollintoview.

ScrollTo is fine, but oftentimes you just want to make sure a UI element is visible, not necessarily at the top. ScrollTo doesn't help you with this. From scrollintoview's README:

How does this plugin solve the user experience issue

This plugin scrolls a particular element into view similar to browser built-in functionality (DOM's scrollIntoView() function), but works differently (and arguably more user friendly):

  • it only scrolls to element when element is actually out of view; if element is in view (anywhere in visible document area), no scrolling will be performed;
  • it scrolls using animation effects; when scrolling is performed users know exactly they're not redirected anywhere, but actually see that they're simply moved somewhere else within the same page (as well as in which direction they moved);
  • there's always the smallest amount of scrolling being applied; when element is above the visible document area it will be scrolled to the top of visible area; when element is below the visible are it will be scrolled to the bottom of visible area; this is the most consistent way of scrolling - when scrolling would always be to top it sometimes couldn't scroll an element to top when it was close to the bottom of scrollable container (thus scrolling would be unpredictable);
  • when element's size exceeds the size of visible document area its top-left corner is the one that will be scrolled to;
Share:
156,219
djmzfKnm
Author by

djmzfKnm

WebDev

Updated on July 05, 2022

Comments

  • djmzfKnm
    djmzfKnm almost 2 years

    I have done the following code in JavaScript to put focus on the particular element (branch1 is a element),

    document.location.href="#branch1";
    

    But as I am also using jQuery in my web app, so I want to do the above code in jQuery. I have tried but don't know why its not working,

    $("#branch1").focus();
    

    The above jquery (focus()) code is not working for div, whereas If i am trying the same code with textbox, then its working,

    Please tell me, how can I put focus on a div elemnt using jQuery?

    Thanks!

  • djmzfKnm
    djmzfKnm over 15 years
    Ya I'm trying to achieve the same anchoring task. Ok, if the focus can't do it, is there any other method in jquery which can do this?
  • fresskoma
    fresskoma over 13 years
    Instead of "$(this).scrollTop" you should use "$(window).scrollTop". The first only workings when "this" happens to be the same as "window".
  • max4ever
    max4ever almost 13 years
    if you are using it inside a .live('click') and you notice the window jumps to the top of the page, add a "return false" as the last statement, it solves the problem for IE and Chrome :D
  • Lukas Eder
    Lukas Eder over 12 years
    That's the nicest solution, I think. Just add it once, and then forget about it... :-)
  • Just a guy
    Just a guy over 12 years
    Very nice solution. This type of solution will go beyond the OP question and abbreviate a bunch of my code.
  • dynamic
    dynamic almost 12 years
    awesome. it doesnt' need a plugin.
  • Zmart
    Zmart over 10 years
    This answer pops up first Google. As of the new jQuery renovations, the new, correct URL is plugins.jquery.com/scrollto
  • Mike Atlas
    Mike Atlas over 10 years
    @Oudin I know this is slow to respond, but replace 'html,body' with '.scrollable-container' and then add scrollable-container' to the list of classes on your div. You could also add a parameter to scrollToMe` which takes any specific selector you want to be the parent instead.
  • Maks
    Maks about 10 years
    thanks thats an excellent answer. I didnt need the animation, so I just did scrollToMe: function () { var x = jQuery(this).offset().top - 100; $('html').scrollTop(x); }});
  • pgee70
    pgee70 over 9 years
    Thanks, well done. I extended this to handle scrolling objects in a scrolling window. jQuery.fn.extend({ scrollToMe: function (e) { if (typeof(e)=='undefined'){ e='html,body'; } var y = jQuery(this).offset().top; jQuery(e).animate({scrollTop: y}, 500); } });
  • geekuality
    geekuality over 9 years
    Even that address is outdated now. Author's Github repo is at github.com/flesler/jquery.scrollTo
  • eon
    eon about 9 years
    Note that though it seems "html" would be unneeded in the selector, Firefox won't do the scroll without it. So $('html, body') works, $('body') does not (FF only, in IE and Chrome it works fine).
  • Piotr Kula
    Piotr Kula about 9 years
    Link out of date and not really jQuery, but a jQuery plugin.
  • Firas Nizam
    Firas Nizam over 5 years
    this is the best solution
  • Zoe stands with Ukraine
    Zoe stands with Ukraine about 5 years
    Please don't add the same answer to multiple questions. Answer the best one and flag the rest as duplicates, once you earn enough reputation. If it is not a duplicate, edit the answer and tailor the post to the question.
  • cleybertandre
    cleybertandre over 3 years
    Thank you! Completely solved my issue. I've also added {behavior: "smooth"} as argument to scroll smoothly.