$(window).scroll(function() { $('#block-new20, #block-new25, #block-new26').each(function(){ var imagePos = $(this).offset().top; var topOfWindow = $(window).scrollTop(); $(this).toggleClass("right",imagePos < topOfWindow+550) }); });