I have a few div
elements with data-msid
('1', '2', '3'), and each one has a corresponding li
in footer with data-msidatrr
('1', '2', '3').
PROBLEM:
I want to add active class on footer li
when the user scrolls and the corresponding div
becomes visible in viewport
.
EXAMPLE:
When id='first' data-msid="1"
appears in the viewport, li data-msidatrr="1"
in footer should have an active
class, and so on for the rest of the div
and footer li
.
Here's my jQuery code:
$(function(){
$.fn.isOnScreen = function(){
var win = $(window);
var viewport = {
top : win.scrollTop(),
left : win.scrollLeft()
};
viewport.right = viewport.left + win.width();
viewport.bottom = viewport.top + win.height();
var bounds = this.offset();
bounds.right = bounds.left + this.outerWidth();
bounds.bottom = bounds.top + this.outerHeight();
return ( ! (
viewport.right < bounds.left ||
viewport.left > bounds.right ||
viewport.bottom < bounds.top ||
viewport.top > bounds.bottom
));
}; /* END $.fn.isOnScreen */
$( "#container" ).scroll(function() {
console.log('scrlling');
$.each( $('#container>div'), function(i, left) {
console.log(i);
var msid = $(left).attr('data-msid');
console.log($(left).attr('data-msid'));
console.log($('#first').isOnScreen());
$('.fC[data-msidatrr=]+msid').addClass('active');
})
});
}) /* END $(function() */