There are a couple of steps to solve your problem:
An ID in HTML cannot start with a number, so rename them to something like 'hair1', 'hair2', etc.
The main problem is that because of the setInterval, by the time the function runs, i
will not be the i
that you were expecting. This is because of variable scope. You can get around this with an anonymous function.
Combining both of the above gives this code:
<div class="hair" onclick="rotate()">
<img src="single.png" id="hair1" width="10" height="40">
<img src="single.png" id="hair2" width="10" height="40">
<!-- etc -->
</div>
// NOTE: This is not the final code listing, see below for a final answer.
for (var i = i; i < 11; i++) {
(function(local_i) {
setInterval(function () {
// use local_i instead of i inside this function for the results you expect
document.getElementById('hair' + local_i).style.WebkitTransitionDuration='1s';
document.getElementById('hair' + local_i).style.webkitTransform = 'rotate(40deg)';
}, 100);
})(i);
}
I would also recommend putting the styles into a stylesheet, then apply them using a class:
.rotate
{
-webkit-transform: rotate(40deg);
-webkit-transition: -webkit-transform 1s;
}
Finally, to get the elements to rotate in a row rather than all together, you need to multiply the interval by the value of i
. I think that you probably mean to use setTimeout rather than setInterval (setInterval will keep running over and over again).
function rotate(){
for(var i=1;i<11;i++) {
(function (local_i) {
setTimeout(function(){
document.getElementById('hair' + local_i).classList.add('rotate');
}, 100 * local_i);
})(i);
}
}
I've put together a demo here