Add class of 'last' to every 3rd image


/ Published in: jQuery
Save to your folder(s)

Quick little clientside script to pick out every 3rd image and add a class of last to it.

I needed this because the images were layed out in 3 columns, adding a margin to the right hand side would leave a large gap on the right hand side which i didn't want. This now allows me to remove the margin for the last image.


Copy this code and paste it in your HTML
  1. //Save the context in a var
  2. var $main = $("#mainContent");
  3. //If the container exists
  4. if($(".ngg-albumoverview").length){
  5. //Loop through each image, including the index of the object(i)
  6. $(".ngg-album", $main).each(function(i){
  7. //Since 0 based add one, for every 3rd object remainder will equal 0
  8. var remainder = (i + 1) % 3;
  9. //Add a class of last when the remainder is 0
  10. if(remainder === 0){
  11. $(this).addClass("last");
  12. }
  13. });
  14. }

Report this snippet


Comments

RSS Icon Subscribe to comments

You need to login to post a comment.