Jquery add class on page view -
i have added
function isscrolledintoview(elem) { var docviewtop = jquery(window).scrolltop(); var docviewbottom = docviewtop + jquery(window).height(); var elemtop = jquery(elem).offset().top; var elembottom = elemtop + jquery(elem).height(); return ((elembottom <= docviewbottom) && (elemtop >= docviewtop)); }
window.scroll
jquery(window).scroll(function(){ if (isscrolledintoview('h2') === true) { jquery('h2').addclass('in-view') } else {jquery('h2').removeclass('in-view') } });
the problem classes added , removed when top h2 on page visible. each h2 triggered when each individual 1 enters browser window.
please forgive me, new javascript.
since it's working first h2
, can use .each
repeat other h2
elements:
jquery(window).scroll(function(){ $("h2").each(function() { if (isscrolledintoview(this) === true) { jquery(this).addclass('in-view') } else { jquery(this).removeclass('in-view') } }); });
Comments
Post a Comment