我需要针对多个div元素。他们都需要同样的行动。他们基本上需要在悬停和下潜时进行放大。
如何将变量从循环传递到jquery和gsap?
for (var sca = 1; sca<=12; sca++) {
$( "#sc"+sca ).mouseover(function() {
TweenLite.to("sc"+sca, .5, {css:{ scale:1.3 }, ease:Back.easeOut});
$( "#sc"+sca ).css("z-index","100");
})
.mouseout(function() {
TweenLite.to(sc1, .5, {css:{ scale:1 }, ease:Back.easeIn});
$( "#sc"+sca ).css("z-index","1");
});
}发布于 2013-10-03 09:22:36
将所有div元素都赋予相同的类,然后可以使用该类选择器同时将相同的事件处理程序附加到所有这些元素,而无需循环。就像这样:
$('.myClass')
.mouseover(function() {
TweenLite.to(this, .5, { css: { scale: 1.3 }, ease: Back.easeOut });
$(this).css("z-index", "100");
})
.mouseout(function() {
TweenLite.to(this, .5, { css: { scale: 1 }, ease: Back.easeIn });
$(this).css("z-index","1");
});另外,我不知道为什么要使用TweenLite,而jQuery有内置的动画吗?
发布于 2013-10-03 10:08:00
$(document).ready(function () {
$('.fooDiv').mouseover(function () {
TweenLite.to(this, .5, { css: { scale: 1.3 }, ease: Back.easeOut });
$(this).css("z-index", "100");
})
.mouseout(function () {
TweenLite.to('sc1', .5, { css: { scale: 1 }, ease: Back.easeIn });
$(this).css("z-index", "1");
});
});发布于 2013-10-03 09:23:03
不要使用循环,而是使用以"sc“开头的id将事件处理程序绑定到所有元素,然后使用对函数中实际元素的引用:
$('[id^="sc"]').mouseover(function() {
TweenLite.to(this.id, .5, {css:{scale:1.3}, ease:Back.easeOut});
$(this).css("z-index","100");
})
.mouseout(function() {
TweenLite.to(this.id, .5, {css:{scale:1}, ease:Back.easeIn});
$(this).css("z-index","1");
});或者,就像罗里建议的那样,用类代替(类选择器会稍微好一些)。
如果您必须使用一个循环(如上面所演示的,不使用循环),那么您将需要创建一个包含该迭代的sca值的闭包:
for(var sca = 1; sca <= 12; sca++) {
(function(sca) {
$( "#sc"+sca).mouseover(function() {
TweenLite.to("sc"+sca, .5, {css:{scale:1.3}, ease:Back.easeOut});
$( "#sc"+sca ).css("z-index","100");
})
.mouseout(function() {
TweenLite.to("sc" + sca, .5, {css:{scale:1}, ease:Back.easeIn});
$( "#sc"+sca ).css("z-index","1");
});
})(sca);
}https://stackoverflow.com/questions/19155338
复制相似问题