Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Detecting divs as rendered in the window to implement Google-Reader-like auto-mark-as-read?

When using Google Reader and browsing RSS entries in the "Expanded" view, entries will automatically be marked as 'read' once a certain percentage of the div is visible on the screen (difficult to tell what percentage has to be visible in the case of Google Reader). So, as I scroll down line-by-line, the javascript code can determine that a) the entry is being rendered in the visible window and b) a certain amount is visible and when those conditions are met, the state is toggled to read.

Does anyone have any idea how that feature is implemented? Specifically, does anyone here know how to tell if a div has scrolled into view an how much of the div is visible?

As an aside, I'm using jQuery, so if anyone has any jQuery-specific examples, they would be much appreciated.

like image 385
smoody Avatar asked Dec 09 '08 20:12

smoody


2 Answers

The real trick is to keep track of where the scrollbar is in the element containing your items. Here's some code I once whipped up to do it: http://pastebin.com/f4a329cd9

You can see that as you scroll it changes focus. You just need to add more handler code to the function that handles each focus change. It works scrolling in both direction, and also by clicking right on the scrollbar, which simple mouse tracking won't give you (though in this case since the example elements are all the same size, with the same text, it's hard to tell that it has indeed scrolled). The other issue is what to do when the container bottoms out. The solution I have right now only works in FF. If you want to have it look nice in IE, you'll have to use a dummy element that blends into the background, like the one I have commented out in the code.

like image 57
Cthulhon Avatar answered Nov 11 '22 00:11

Cthulhon


I just came across this as I need the same thing, and it looks super useful:

http://www.appelsiini.net/projects/viewport

like image 2
Cameron Booth Avatar answered Nov 11 '22 00:11

Cameron Booth