views:

110

answers:

3

I have a scrollable div with a fixed height. While scrolling I need to get the element at the top of the visible viewport and the one at the bottom of the viewport. I can get the position of the scrollbar using scrollTop, however I am not sure how to get the element.

Update: Formatted question for clarity.

A: 

You should loop through the div using DOM elements. Take a look here, I think it might be a usefull resource: http://www.howtocreate.co.uk/tutorials/javascript/dombasics

With some arrays and counters you should be able to get the last element in your div.

Ben Fransen
I know how to loop, however how do I know which element has is now at the top of the div - where the scrollbar currently is?
vikasde
Ah, your wording is a bit hard to understand. The stuff at the top of the div will always be there :) What you're asking for is the element at the top of the visible window/viewport/view, something like that.
Carl Smotricz
Thats exactly what I meant :)
vikasde
Great! Might I suggest you edit your question to reflect this? Quality of answers should improve if people are better able to understand you. Also please include a small remark that wording was changed for clarity.
Carl Smotricz
Done. Thanks. I will try your suggestion.
vikasde
+1  A: 

Get your div's scroll position

var scrollY = document.getElementById("yourDiv").scrollTop;

Loop through the elements inside and look at their position

var divs = document.getElementById("yourDiv").getElementsByTagName("div");
alert(divs[0].offsetTop)

Figure out which one is at the scrollTop position and the other one is at scrollTop + height position

epascarello
Thanks. I will try that. Would there be a way to get the divs without looping? Maybe if my Divs had an incremental Id or something?
vikasde
The reason why I want to avoid looping is because I have around 2000 elements on my page and I not want to loop through all of them always I scroll. Thanks
vikasde
If they have a set height, it is a simple division problem.
epascarello
Yes, they all have a fixed height. Let me try to play with this.
vikasde
A: 

I assume that your DIV is a long list of text lines, and all your lines will be of uniform height. If this is true, then you don't need to ask which one is at the top of the viewport, as you will be able to calculate it.

You'll want to be displaying the first page of this stuff in full anyway, so you create one complete and displayable line, ask it for its height and remember that. Then you add some more text-containing lines until the total height of them is greater than the height of the window. Then, add 1990 or so empty lines so that the thumb's size and position accurately reflect the size of the list.

Then, when someone scrolls your window, you can calculate which lines should be visible; zap those that have moved out of view and build up the ones that should be visible. Done!

(I think - I've never tried this!)

Carl Smotricz
You'll want to make sure that "empty" lines are EXACTLY as high as filled lines. Since you're doing all this fiddling in JS anyway, I guess it should be as simple as setting a CSS attribute that applies to all those lines.
Carl Smotricz
Yeah, I already used CSS for that. I got it almost working, however I notice that when I start new items, then the browser becomes slower and slower. There must be memory leak somewhere.
vikasde