2012-12-08 111 views
1

我有一套div-s,我需要应用一些jQuery。为什么变量不会传递?

<div id="anim1" class="animation"></div> 
<div id="anim2" class="animation"></div> 

它看起来很直接,但我想让它更灵活一点。虽然... ...可能是不可能的,但是我没有复制和粘贴jQuery函数多少次,我想知道是否有任何方式从mouseover动作中获取图层名称并将其放入一个变量,我可以在以下脚本中使用:

$(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); 
}); 

我想知道我在做什么错在这里。任何想法都非常感谢...

更新。试图在$(document).ready(function()中声明变量。我不确定是否可以这样做,但至少动画现在正在移动。唯一的问题是,当我

$(document).ready(function() { 
layer = $('.animation').mouseover(function() { 
    '#'+this.id; 
}); 
+0

对于其中一个,'*'不是变量名的有效字符。 –

+1

@Asad看起来像他想要加粗变量名称 – VIDesignz

+0

是的,我想要粗体显示变量名称以便能够更好地看到 – Perren

回答

0

在这里你去的人...

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

}); 
+0

非常感谢VIDesignz。完美解决方案这正是我所期待的。 没有想过usung.each(函数() – Perren

+0

欢迎@Perren ...'.each()'这样的时代很棒! – VIDesignz

+0

永远不要停止学习...... :)再次感谢@VIDesignz – Perren

0

你可以只设立一个变量了鼠标功能的范围,一边将鼠标悬停在任何一个:,然后分配$(本),该变量的函数

瓦尔那里面;

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

然后使用它来指代我之外的元素。 e你在这里想要做的,但这是“传递元素”的一种方式。

0

当你不惯于复制您为您的每个div的功能,我会用jQuery each

我想给每个div的类名:

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

,然后指定你的效果在每个元素:

$('.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); 
}); 
+0

谢谢muffls。这也是一个很好的解决方案。效果很好。我应该尝试'jQuery每个'...也像一种享受......再次感谢 – Perren

相关问题