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
Post a Comment