Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Scrollable Div, which elements can be seen

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

like image 397
RIK Avatar asked Jul 06 '11 14:07

RIK


People also ask

How do you check if an element is visible after scrolling?

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.

How do you know which element is scrolled?

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.

How do you display scrollable content?

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.

What makes a div scrollable?

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;.


1 Answers

Update

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/

like image 140
Jose Faeti Avatar answered Nov 27 '22 17:11

Jose Faeti