html - How to call a function for each element with a particular class, every time the window scrolls (jquery)) -
i trying make scroll-triggered function slides elements in 1 side based on scroll position. run on elements 'my-class', code seems think 'this' window, not each 'my-class' element. there wrong way calling function? how can each element run function?
here basic html structure:
<div class="wrapper"> <div class="my-class"></div> <div class="not-my-class"></div> <div class="my-class"></div> <div class="not-my-class"></div> <div class="my-class"></div> </div>
some css:
.wrapper { width: 100%; height: 100%; } .my-class, .not-my-class { width: 100%; height: 350px; margin-top: 10px; background-color: #888888; }
and jquery:
function fadein($element) { $scrolltop = $(window).scrolltop(); $windowheight = $(window).height(); $pageheight = $('body').height(); $elementheight = $($element).height(); $baseoffset = $($element).offset().top; $length = 100; $finaloffset = $baseoffset + ( $elementheight / 2 ); $current = ( $scrolltop - ( $finaloffset - ($windowheight / 2 ) - ( $length / 2 ) ) ) / $length; if ($current > 1) { $current = 1; } else { if ($current < 0) { $current = 0; } else {} } $opacity = $current; $slide = ( $current * 100 ) - 100; $(this).css({"opacity": $opacity, "left": $slide, "position": "relative"}); } $(window).on("load resize scroll",function(){ $('.my-class').each(fadein(this)); });
you need pass in function so:
$('.my-class').each(function(){ fadein(this) });
so this
referring .my-class element instead of window
Comments
Post a Comment