We have a scrollable div that has CSS hieght:40px;
. Inside it are multiple LI height:20px
<div id="#scroller">
<li title="I1">item1</li>
<li title="I2">item2</li>
<li title="I3">item3</li>
<li title="I4">item4</li>
<li title="I5">item5</li>
<li title="I6">item6</li>
<li title="I7">item7</li>
<li title="I8">item8</li>
<li title="I9">item9</li>
</div>
When the user scrolls I want to trigger a script that determines the first of the two elements which is visible. The div scroll is designed to snap to elements. So if you scroll down and item3 and item 4 are visible how do I fund out that item 3 is the top visible element.
I tried $('#scroller li:visible')
but this does not work as far as the div is concerned they are all visible just hidden behind their container.
Any ideas,
Marvellous
Summary. Use the getBoundingClientRect() method to get the size of the element and its relative position to the viewport. Compare the position of the element with the viewport height and width to check if the element is visible in the viewport or not.
The scroll event is fired whenever a particular element is being scrolled. Thus, we can easily find out which element is being scrolled with the help of it. Example: In this example, we've created two scrollable elements and added the 'scroll' event listener to both of them.
Indicate content overflow by cutting off grid tiles. Or alternatively you can directly ask users to scroll. A subtle cue, such as an arrow pointing off-screen or a text “scroll down,” can inform users that most of the content will be laid out linearly.
Making a div vertically scrollable is easy by using CSS overflow property. There are different values in overflow property. For example: overflow:auto; and the axis hiding procedure like overflow-x:hidden; and overflow-y:auto;.
Updated with a working example http://jsfiddle.net/U4qyp/32/
I think .position()
should do the job. It gives you the position of the element relative to its parent element. After you called .position() you can access the element coordinates using the properties top
and left
.
http://api.jquery.com/position/
The element whose top position plus its height is major than zero, is visible.
Here is an example of what I mean.
http://jsfiddle.net/U4qyp/10/
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With