I've got a container, with multiple <div>
s inside, representing pages. As a user scrolls through these pages, I want to update the current page number label. To achieve this, I found I can use the :in-viewport
selector. I tried following this example, and came up with this jsFiddle:
This seems to work, but I do have a minor problem with it. And that is as I scroll down, my current page number doesn't change to "2" until the 2nd "image" element is partially hidden (on the top), and page 3 is partially visible. I'd like it to change to 2 sooner. See this screenshot for what I mean, this is when it changes to "2":
Ideally, I'd like the current page number to change to "2" as soon as the 2nd "image" element is the element with a majority in view. For example, this is about the position I'd like the current page number to change to "2":
http://imgur.com/uKZRD4m (notice how #2 is is the majority in-view)
And it seems to get worse as I keep scrolling. In this next screenshot, the current page number still says 5, but the 5th image isn't in view at all anymore!
Is there anything different I can do to achieve this effect? Right now I'm just using the in:viewport
selector in combination with the :first
selector:
$('.item:in-viewport:first')