javascript - jquery and event scroll -


i have 1 class html various elements want add class new class in first element scroll top reach top ofos elements, when scroll reach second add in second , on. tried this

var element = $(".element");          $(window).scroll(function () {             var scroll = $(window).scrolltop();             for(var = 0; < element.length;i++){                 if(scroll > element.eq(i)){                     element.eq(i).addclass("newclass");                 }    } }) 

html piece

<div>     <div class="element">         <img src="img/image1" />     </div>     <div class="element">         <img src="img/image2" />     </div>     <div class="element">         <img src="img/image3" />     </div> </div> 

but line element.eq(i).addclass("newclass") dont work :) how should do

try use

if(scroll > element.eq(i).offset().top - ($(window).height() / 2) ){     element.eq(i).addclass("newclass");  } 

demo


Comments

Popular posts from this blog

c# - Validate object ID from GET to POST -

node.js - Custom Model Validator SailsJS -

php - Find a regex to take part of Email -