I have a set of div-s that I need to apply some jQuery to.
<div id="anim1" class="animation"></div>
<div id="anim2" class="animation"></div>
It looks straight forward, but I wanted to make it a bit more flexible. It might not be possible though... ...but instead of copying and pasting the jQuery function as many times as many layers I have I wondered if there was any way to acquire the layer name from a mouseover action and place it into a variable that I can use in the following script:
$(document).ready(function() {
$('.animation').mouseover(function() {
layer = '#'+this.id;
});
var steps = [0, 50, 100, 150, 200, 250, 300, 350, 400, 450, 500, 550, 600, 650, 700, 750, 800, 850, 900, 950, 1000, 1050, 1100, 1150, 1200, 1250, 1300, 1350, 1400, 1450, 1500, 1550, 1600, 1650, 1700, 1750, 1800, 1850, 1900, 1950, 2000, 2050, 2100, 2150, 2200, 2250, 2300, 2350, 2400, 2450, 2500, 2550, 2600, 2650, 2700, 2750, 2800, 2850, 2900, 2950,];
var index = -1;
setTimeout(function() {
index++;
if(index == 57) {
index = 0;
}
$(layer).hover(function(){
index ++;
}, function(){
index -=1;
});
$(layer).css('backgroundPosition', '-' + steps[index] + 'px 0px');
setTimeout(arguments.callee, 50);
}, 25);
});
I was wondering what am I doing wrong here. Any thoughts are much appreciated...
update. Tried to declare the variable in the $(document).ready(function(). I am not sure to be honest if I can do this way, but at least the animations are moving now. The only problem is that both stops when I hover over any of them.:
$(document).ready(function() {
layer = $('.animation').mouseover(function() {
'#'+this.id;
});