I need to check wether the user has seen the post and change the document title based on how many posts he hasn't seen.
For example title with 2 unseen posts: "(2) - Post#123", or with no unseen posts: "Post #123".
I've looked into a couple of "scrolled into view" jQuery plugins, but I haven't found a good and quick solution to do this with ajaxed content (the posts are appended to a div, it checks for new every 5 seconds).
So for example this as markdown:
<div id="posts">
<div class="reply" data-postid="1" data-seen="no">
Post 1
</div>
<div class="reply" data-postid="2" data-seen="no">
Post 2
</div>
<div class="reply" data-postid="3" data-seen="no">
Post 3
</div>
</div>
I'm guessing using an attribute for "seen" will be useful. Now how do I check if a user has seen (the posts have been scrolled by/on the screen) and store the unseen in a string to use in the title?
Useful info: The divs appended are gotten via xhr, they are echoed out as the body of another file "imported" or appended to #posts
with xhr. The name of the function that is repeated every 5 seconds to get new posts is PostData()
.
EDIT: I'm trying this, but it's not working:
function isScrolledIntoView(elem){
var docViewTop = $(window).scrollTop();
var docViewBottom = docViewTop + $(window).height();
var elemTop = $(elem).offset().top;
var elemBottom = elemTop + $(elem).height();
return ((elemBottom <= docViewBottom) && (elemTop >= docViewTop));
}
$(window).scroll(function(){
isScrolledIntoView(".reply[data-seen='no']", function(){
if ($(this) == true){
$(this).attr("data-seen", "yes");
}
});
});
UPDATED WORKING CODE (for the bypassers):
function lescroll(){
var amount = "0";
$('.reply[data-seen="no"]').each(function(){
amount++;
if ( isScrolledIntoView(this) ) {
$(this).attr("data-seen", "yes");
amount--;
};
});
document.title = "(" + amount + ") pagetitle";
if (amount == 0){
document.title = ">><?php echo($pageid); ?> - <?php echo($leboardname); ?> vikingchan";
}
setTimeout(lescroll, 5000); /* make it check while you are in another tab */
}
$(window).scroll(function(){
lescroll();
});
$(document).ready(function(){
lescroll();
});