1

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;
});
Perren
  • 13
  • 3

3 Answers3

0

You could just set up a variable out side of the scope of the mouse over function and then assign $(this) to that variable inside the function

Var that;

$('.animation').mouseover(function() {
    that = $(this);
});

Then use that to refer to the element outside the. I'm not sure what you're trying to do here, but that's one way to "pass the element on".

itsmequinn
  • 1,054
  • 1
  • 8
  • 21
0

When you don't wont to copy your function for each of your divs, I'd use jQuery each

I would give each div a class name:

<div class="anim" class="animation"></div>
<div class="anim" class="animation"></div>

And then assign your effect on each of the elements:

$('.anim').each(function(index_each, element) {

  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;
    }

    $(element).hover(function(){
       index ++;
      }, function(){
            index -=1;
    });

    $(element).css('backgroundPosition', '-' + steps[index] + 'px 0px');
    setTimeout(arguments.callee, 50);
  }, 25);
});
Philipp Hofmann
  • 3,388
  • 26
  • 32
  • Thanks muffls. This is also a great solution. Works well. I should have tried 'jQuery each'... works like a treat too... Thanks again – Perren Dec 08 '12 at 14:27
0

Here you go man...

FIDDLE

$('.animation').each(function() {
    var anim = $(this);

    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;
        }
        anim.hover(function() {
            index++;
        }, function() {
            index -= 1;
        });
        anim.css('backgroundPosition', '-' + steps[index] + 'px 0px');
        setTimeout(arguments.callee, 50);
    }, 25);

});
VIDesignz
  • 4,703
  • 3
  • 25
  • 37