Skip to content

Instantly share code, notes, and snippets.

@SamWM
Created July 30, 2012 16:27
Show Gist options
  • Save SamWM/3208209 to your computer and use it in GitHub Desktop.
Save SamWM/3208209 to your computer and use it in GitHub Desktop.
Alternating background colours for table cells, returning to start when last one is reached (using jQuery)
<table style="padding: 3px; background: #111; border-radius: 8px" cellspacing="4">
<tr>
<td class="light" width="50" height="50" data-bg="#f8696b" style="background: #fff; border-radius: 25px;">
</td>
</tr>
<tr>
<td class="light" width="50" height="50" data-bg="#f9e983" style="background: #fff; border-radius: 25px;">
</td>
</tr>
<tr>
<td class="light" width="50" height="50" data-bg="#63be7b" style="background: #fff; border-radius: 25px;">
</td>
</tr>
</table>
<table style="margin-left: 20px;">
<tr>
<td width="20" height="150" style="background: #333; border-bottom-right-radius: 8px; border-bottom-left-radius: 8px;">
</td>
</tr>
</table>
<script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/jquery/1.7.2/jquery.min.js"></script>
<script type="text/javascript">
var index = 0;
setInterval( function()
{
$("td.light").each(
function()
{
$(this).css("background", $(this).data("bg"));
}).filter(":not(:eq(" + index + "))").css("background", "#fff");
index = (index + 1) % 3
}, 500);
</script>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment