javascript - Simplify functions so the code does not get too big -


i have code each element make scroll effect when done: keep repeating function each element, problem 30 elements different classes add, code large.

jquery:

  $(window).scroll(function () {         $('.regalos').each(function () {             var imagepos = $(this).offset().top;              var topofwindow = $(window).scrolltop();             if (imagepos < topofwindow + 400) {                 $(this).addclass("stretchleft");             }         });          $('.sprite-layer-2').each(function () {             var imagepos = $(this).offset().top;              var topofwindow = $(window).scrolltop();             if (imagepos < topofwindow + 400) {                 $(this).addclass("slideleft");             }         });        // ... must 28     }); 

i use:

$(window).scroll(function () {     var topofwindow = $(window).scrolltop();      function _checkoffset(classname) {         return function () {             var $this = $(this),                 imagepos = $this.offset().top;              $this.toggleclass(classname, (imagepos < topofwindow + 400));         };     }      $('.regalos').each(_checkoffset('stretchleft'));     $('.sprite-layer-2').each(_checkoffset('slideleft')); }); 

however, might better off re-thinking structure avoid adding different class names based on offset.


Comments

Popular posts from this blog

How to remove text and logo OR add Overflow on Android ActionBar using AppCompat on API 8? -

html - How to style widget with post count different than without post count -

url rewriting - How to redirect a http POST with urlrewritefilter -