7

I'm building something where I show users items that they haven't seen.

Each item is in a <div>, so when the user scrolls past a div, or views the div, I want that item to be marked as having been seen.

Google reader does this, if you scroll past an item in your feed there it automatically marks it as read.

How can this be tracked? Advice please.

Note: It shouldn't be restricted to using the mouse to scroll, hitting page down/up, using arrow keys, etc should also be counted. The main criteria is that the user has seen a div.

Ali
  • 261,656
  • 265
  • 575
  • 769
  • 3
    As far as I know, they calculate each relevant div's offset from the top of the page and compare to the scroll height. Once scollHeight >= divHeight, then it's considered in-view. – Marc B May 24 '11 at 14:43

4 Answers4

6

You need jQuery's scrollTop.

Something like:

$(window).scrollTop() > $('#mydiv').offset().top;

for when it first comes into view, or add $('#mydiv').height() to the top offset if you want it to be marked when it's fully in view.

tjm
  • 7,500
  • 2
  • 32
  • 53
3

You could use a solution like this, http://www.appelsiini.net/projects/viewport, which I used in the past.

Or see this for other solutions: Detecting divs as rendered in the window to implement Google-Reader-like auto-mark-as-read?

Community
  • 1
  • 1
Justin
  • 1,956
  • 3
  • 23
  • 34
1

Have a look at $("#divid").scrollTop().

Praveen Lobo
  • 6,956
  • 2
  • 28
  • 40
1

I think you'll need something like this...

$(window).scroll(function(){
    var scroll = $(this).scrollTop();
    var vieweditems = $('div').filter(function(){
       return scroll> $(this).offset().top;
       //compare the div's offset top with the window scroll position 
       // this returns the collection of viewed divs
    })// this object is colleection of viewd divs
      .removeClass('hilight')
      //Remove the class which distinguishes the unread and read items
      .map(function(){
          return this.id.split('_')[1];
      }).get();
      //This is the collection of ids of viewd divs
      //vieweditems  now holds the ids of viewed divs

});
Jishnu A P
  • 14,202
  • 8
  • 40
  • 50
  • How can I identify in this case which div was viewed so I could send an ajax request to mark that div as 'viewed' in the database? Divs will have ids e.g `item_1`, `item_2` etc. – Ali May 24 '11 at 15:02