Sunday, May 12, 2024
 Popular · Latest · Hot · Upcoming
-4
rated 0 times [  2] [ 6]  / answers: 1 / hits: 16197  / 12 Years ago, fri, may 25, 2012, 12:00:00

I have a document with several sections like this:



<div class='section' id='sec1'>
lalala
lalala
lalala
</div>

<div class='section' id='sec2'>
lalala
lalala
lalala
</div>

<div class='section' id='sec3'>
lalala
lalala
lalala
</div>

<div class='section' id='sec4'>
lalala
lalala
lalala
</div>


How do I grab the closest <div.section> to the current scroll position (presumably, this would equate to the section that the reader is currently looking at)?


More From » jquery

 Answers
55

You can use $(window).scrollTop() and $(el).postion().top to figure out how far the element is from the top of the screen after scrolling.



You can then use this information to manipulate the element as desired.



Here is a working jsfiddle example:
http://jsfiddle.net/gizmovation/x8FDU/


[#85344] Thursday, May 24, 2012, 12 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
jaden

Total Points: 709
Total Questions: 91
Total Answers: 91

Location: Maldives
Member since Sat, Jan 29, 2022
2 Years ago
;