javascript - Check for unseen posts and update title -
i need check wether user has seen post , change document title based on how many posts hasn't seen.
for example title 2 unseen posts: "(2) - post#123", or no unseen posts: "post #123".
i've looked couple of "scrolled view" jquery plugins, haven't found , quick solution ajaxed content (the posts appended div, checks new every 5 seconds).
so example 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 attribute "seen" useful. how check if user has seen (the posts have been scrolled by/on screen) , store unseen in string use in title?
useful info: divs appended gotten via xhr, echoed out body of file "imported" or appended #posts
xhr. name of function repeated every 5 seconds new posts postdata()
.
edit: i'm trying this, 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 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 check while in tab */ } $(window).scroll(function(){ lescroll(); }); $(document).ready(function(){ lescroll(); });
to make code run elements change scroll callback
$(window).scroll(function(){ $('.reply[data-seen="no"]').each(function(){ if ( isscrolledintoview(this) ) { $(this).attr("data-seen", "yes"); }; }); });
but keep in mind might bit heavy since scroll fired multiple times while scrolling. might want debounce scroll
event.
Comments
Post a Comment