1

Hello using Jquery how can i scroll an element (with overflow) to children element ?

<span id="gran">
 <span id="parent">
  <div class="a">
   Casa
  </div>
  <div class="a">
   Casa
  </div>
  <div class="a">
   Casa
  </div>
  <div class="a">
   Casa
  </div>
  <div class="a">
   Casa
  </div>
 </span>
</span>

#gran {
overflow: auto;
}

I want to scroll #gran to third element with class a so i tried this but doesn't work:

 $("#gran").scrollTo('#parent > div:nth-child(3)'); 

I hope in your help :) Thanks a lot and sorry for my english

Borja
  • 3,359
  • 7
  • 33
  • 66
  • [nesting-div-within-span-problem??](https://stackoverflow.com/questions/2919909/nesting-div-within-span-problem) .. also `span:nth-child(3)` Is it a span or div?? in html its a div in jquery code its a span?? – Mohamed-Yousef Jan 09 '21 at 21:47
  • @Mohamed-Yousef sorry i edit jquery....but doens't work yet – Borja Jan 09 '21 at 21:49

1 Answers1

2

This is how it can be done

  • You need to set max-height with overflow : auto to let the element scrollable without that it will keep extending without scrollbar .. if you need to use the document/body scrollbar use $('body , html') Smooth scroll to div id jQuery

  • You need to use animate() function with scrollTop and offset()/position()

//$("#gran").scrollTo('#parent > div:nth-child(3)');

$(document).ready(function(){
  $('#gran').animate({
      scrollTop: - $('#gran').offset().top + $("#parent > div:nth-child(3)").offset().top
  }, 2000);
});
#to_remove{
  background : red;
  height : 100px;

}

#gran {
overflow: auto;
max-height : 500px;
}
.a{
  height : 300px;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<div id="to_remove"></div>

<div id="gran">
 <div id="parent">
  <div class="a">
   Casa 1
  </div>
  <div class="a">
   Casa 2
  </div>
  <div class="a">
   Casa 3
  </div>
  <div class="a">
   Casa 4
  </div>
  <div class="a">
   Casa 5
  </div>
 </div>
</div>
Mohamed-Yousef
  • 23,946
  • 3
  • 19
  • 28
  • Your code works well but i don't understand why in my case (where html is a little bit more diffucult with 3 div inside a parent overflow) nth-child works only for "even" – Borja Jan 09 '21 at 22:20
  • @Borja If you've only 3 divs in a prarent and your code try to scroll to the third/last one it will scroll fine but up to the 3rd div height .. if its height smaller then the `#gran` height it will not scroll to the top of the `#gran` for sure .. because its no more height/content for scrollbar to scroll – Mohamed-Yousef Jan 09 '21 at 22:25
  • Nooo is really strange problem... for odd nth-child i have error "can't access property "top"" but if is even nth-child (2-4-6 ecc) works well. Whattttt !?!? :D – Borja Jan 09 '21 at 22:28
  • @Borja If you've a live website let me check it for you – Mohamed-Yousef Jan 09 '21 at 22:32
  • no is a file...maybe the problem is about some properties of each div (child). Maybe it can has display: inline ? – Borja Jan 09 '21 at 22:33
  • @Borja I don't think its about `display` ok see updated answer and see .. i added `$(document).ready(` and `setTimeout` to let the website take more few seconds to run the scroll action .. lets try an see – Mohamed-Yousef Jan 09 '21 at 22:40
  • if #parent has 8 div but nth-child works only for even... – Borja Jan 09 '21 at 22:45
  • @Borja Yes its strange .. I can't tell what is wrong specially while I can't see the code live.. keep trying and if you find a solution please let me know – Mohamed-Yousef Jan 09 '21 at 22:54
  • Yes you are right...and i thank your for the help :) – Borja Jan 09 '21 at 23:07