jQuery randomly add class to list items with limit -
i have bit fo code working fine, either doesn't add class or ads classe of wide
items within list.
i trying find solution limit how many of list items can have class of wide.
i using more masonry wall , limit number of wide items within wall.
my current code below, there way limit this?
// random masonry item class on homepage jquery(document).ready(function(){ var classes = [" ", "wide"]; jquery(".mix-target").each(function(){ jquery(this).addclass(classes[~~(math.random()*classes.length)]); }); });
something this?
jquery(document).ready(function(){ var classes = [" ", "wide"]; var limit = 3; jquery(".mix-target").each(function(){ if (limit == 0) return false; var index = ~~(math.random()*classes.length); if (index == 1) limit--; jquery(this).addclass(classes[index]); }); });
Comments
Post a Comment