【发布时间】:2012-07-20 14:51:52
【问题描述】:
我有一个看起来像这样的 javascript 对象,
var telephones = {
/*
"phone" : {
duration : time it takes to animate element in,
leftIn : position of element once on the incoming animation
leftOut : position of element on the outgoing animation
delay : delay between two animations
}
*/
"phone1": {
duration : 850,
leftIn : "408px",
leftOut : "9999px",
delay : 0,
},
"phone2" : {
duration : 600,
leftIn : "962px",
leftOut : "999px",
delay : setDelay(),
},
"phone3" : {
duration : 657,
leftIn : "753px",
leftOut : "9999px",
delay : 0,
},
"phone4" : {
duration : 900,
leftIn : "1000px",
leftOut : "9999px",
delay : setDelay(),
},
"phone5" : {
duration : 1200,
leftIn : "800px",
leftOut : "9999px",
delay : 0,
},
"phone6" : {
duration : 792,
leftIn : "900px",
leftOut : "9999px",
delay : setDelay(),
},
};
我正在使用上述对象来尝试为幻灯片中的单个元素设置动画,该幻灯片已经通过 jquery 循环插件进行了动画处理。我正在通过以下方式使用代码,
$('#slideshow').cycle({
fx: 'scrollHorz',
before : triggerParralex,
after : removeParralex,
easing : 'easeOutCubic',
speed : 2000
});
所以上面的代码启动了循环插件。然后我使用 before 和 after 回调来运行另外 2 个函数,这些函数看起来像这样,
function bgChange(curr, next, opts) {
var background = $(".current").attr('data-background');
$.backstretch(background, {target: "#backstrectch", centeredY: true, speed: 800});
}
function triggerParralex(curr, next, opts) {
//move any phones that are in the viewport
for (var key in telephones) {
var obj = telephones[key];
for (var prop in obj) {
if($(".current ." + key).length) { //does .custom .phone1/2/3/4/5/6 exist if it does we can carry on
setTimeout(function() {
$(".current ." + key).animate({
"left" : obj["leftOut"],
"opacity" : 0
}, obj["duration"]);
}, obj["delay"]);
}
}
}
//change the background
bgChange();
//remove the current class from the DIV
$(this).parent().find('section.current').removeClass('current');
}
function removeParralex(curr, next, opts) {
//give the slide a current class so that we can identify it.
$(this).addClass('current');
//animate in any phones that belong to the current slide
for (var key in telephones) {
var obj = telephones[key];
for (var prop in obj) {
console.log(obj["leftIn"])
if($(".current ." + key).length) { //does .custom .phone1/2/3/4/5/6 exist if it does we can carry on
setTimeout(function() {
$(".current .phone1").animate({
"left" : obj["leftIn"],
"opacity" : 1
}, obj["duration"]);
}, obj["delay"]);
}
}
}
}
我的问题如下,
我正在尝试为我的部分元素中的图像设置动画,部分元素已经通过循环插件滑动,这对我来说感觉就像它正在阻止我的图像在稍后阶段被动画化?
第二个问题似乎是,虽然我的脚本会很高兴地找到$(".current .phone1") 似乎只是从对象中添加了phone6 的属性,但我已经做了一个fiddle。
正如您从小提琴中看到的那样,带有#slideshow 的部分正在循环,但是其中的图像没有动画...为什么?
【问题讨论】:
-
您正在循环内创建一个函数。请参阅Javascript closure inside loops - simple practical example 了解此问题的解释和解决方案。
-
谢谢,但你能指出我在哪里创建函数吗?
-
setTimeout(function() {....})... 执行回调时,obj将具有循环最后一次迭代的值。另外,我认为您不想遍历似乎没有意义的“动画对象”的每个属性。 -
谢谢,我已经阅读了您留给我的链接,但我真的不明白如何修复我的代码...任何线索?
标签: javascript jquery object jquery-animate