How to scroll to an element in jQuery?

后端 未结 9 1720
无人及你
无人及你 2020-11-30 19:23

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

document.location.href=\"#branch1\";
         


        
9条回答
  •  旧时难觅i
    2020-11-30 20:09

    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;

提交回复
热议问题