Scroll parent to top of child element

danielb

I am trying to scroll a div so that a link inside of it appears on top.

$(document).ready(function(){

    scrollit();
  
  
});

function scrollit(){
  
  	var elem= $("#link10");
							  
	if(elem.length){
		console.log(elem.offset().top);
		$("#sidebar").animate({ scrollTop: elem.offset().top }, { duration: 'medium', easing: 'swing' });
	}
  
  
}
#sidebar{
  
  height:80px;
  width:200px;
  overflow:scroll;
  
  
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div id='sidebar'>
         <a href='#' id='link1'>1</a><br/>
         <a href='#' id='link2'>2</a><br/>
         <a href='#' id='link3'>3</a><br/>
         <a href='#' id='link4'>4</a><br/>
         <a href='#' id='link5'>5</a><br/>
         <a href='#' id='link6'>6</a><br/>
         <a href='#' id='link7'>7</a><br/>
         <a href='#' id='link8'>8</a><br/>
         <a href='#' id='link9'>9</a><br/>
         <a href='#' id='link10'>10</a><br/>
         <a href='#' id='link11'>11</a><br/>
         <a href='#' id='link12'>12</a><br/>
         <a href='#' id='link13'>13</a><br/>
         <a href='#' id='link14'>14</a><br/>
         <a href='#' id='link15'>15</a><br/>
         <a href='#' id='link16'>16</a><br/>
         <a href='#' id='link17'>17</a><br/>
         <a href='#' id='link18'>18</a>
    </div>

<br/><br/>
<a href='#' onclick='scrollit();return false'>again</a>
At first, it will log the offset as being a certain distance (say 45), and will scroll to there correctly. Then, if it runs again, the logged distance is 0 and will then scroll to the top of the sidebar. It should say at that element.

rafaelcastrocouto

Your code works fine as you can see in this jsbin

The problem is that the elem.offset().top value is relative to the current scroll position so you have to take that in account and either reset the scroll or discount the scroll position (with something like window.pageYOffset).

UPDATE:

Solution by danielb:

$("#sidebar").animate({ scrollTop: $("#sidebar").scrollTop()+elem.offset().top }, { duration: 'medium', easing: 'swing' }); 

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Scroll element to the top of an overflowed parent container

From Dev

Get element to scroll back to top when child element clicked

From Dev

scroll property in a child element of a parent div whose position is fixed

From Dev

Child element 100% width of it's parent with overflow: scroll

From Dev

Why a child element with a top margin doesn't stretch the parent container

From Dev

stacking with z-index, child element on top over parent sibling

From Dev

How to get the left and top position of a child element of its parent

From Dev

Horizontal scroll only if child element's width property exceeds parent element's max-width property

From Dev

Positioning child at bottom of parent with scroll

From Dev

Child element's text is not aligned with parent's element text in Chrome when parent element has vertical-align: top

From Dev

Mouseover on child element, not on parent

From Dev

Child element overflows parent

From Dev

Child form always on top of parent

From Dev

parent element not following child element

From Dev

Remove Parent Element with Child Element

From Dev

Getting child element of a child element of a parent with ID

From Dev

Is it possible to hide a child element with parent of parent element?

From Dev

Scroll To Top Button not working in Child templates

From Dev

element on top of parent's sibling

From Dev

Stick div to top of parent element

From Dev

element on top of parent's sibling

From Dev

updateLayout() causing parent container to scroll to top

From Dev

Scroll to the top of the screen without refencing to the parent window

From Dev

Child Div overflow x - y and scroll on parent

From Dev

Prevent parent scroll when in child div

From Dev

Child Div overflow x - y and scroll on parent

From Dev

Child element go out of parent

From Dev

jQuery index of child in parent element

From Dev

Child element hover, effect Parent

Related Related

  1. 1

    Scroll element to the top of an overflowed parent container

  2. 2

    Get element to scroll back to top when child element clicked

  3. 3

    scroll property in a child element of a parent div whose position is fixed

  4. 4

    Child element 100% width of it's parent with overflow: scroll

  5. 5

    Why a child element with a top margin doesn't stretch the parent container

  6. 6

    stacking with z-index, child element on top over parent sibling

  7. 7

    How to get the left and top position of a child element of its parent

  8. 8

    Horizontal scroll only if child element's width property exceeds parent element's max-width property

  9. 9

    Positioning child at bottom of parent with scroll

  10. 10

    Child element's text is not aligned with parent's element text in Chrome when parent element has vertical-align: top

  11. 11

    Mouseover on child element, not on parent

  12. 12

    Child element overflows parent

  13. 13

    Child form always on top of parent

  14. 14

    parent element not following child element

  15. 15

    Remove Parent Element with Child Element

  16. 16

    Getting child element of a child element of a parent with ID

  17. 17

    Is it possible to hide a child element with parent of parent element?

  18. 18

    Scroll To Top Button not working in Child templates

  19. 19

    element on top of parent's sibling

  20. 20

    Stick div to top of parent element

  21. 21

    element on top of parent's sibling

  22. 22

    updateLayout() causing parent container to scroll to top

  23. 23

    Scroll to the top of the screen without refencing to the parent window

  24. 24

    Child Div overflow x - y and scroll on parent

  25. 25

    Prevent parent scroll when in child div

  26. 26

    Child Div overflow x - y and scroll on parent

  27. 27

    Child element go out of parent

  28. 28

    jQuery index of child in parent element

  29. 29

    Child element hover, effect Parent

HotTag

Archive