我调用了函数
animate(0,"mainNav",0.02,0.04);
宣言
function animate(num, element, transitionUnit, delayUnit) {
var delay = 0;
transition = 0;
//var delayUnit = 0.02; transitionUnit = 0.04;
var x = document.getElementById(element).getElementsByTagName("LI");
for (i = 0; i <= x.length - 1; i++) {
x[i].style.WebkitTransform = "translate3d(" + num + "px,0,0)";
x[i].style.transition = transition + "s " + delay + "s ease-in-out";
delay += delayUnit;
transition += transitionUnit;
if (x[i].querySelectorAll('ul li').length > 0) {
x[i].style.background = "rgba(0,0,0,0.35)";
}
}
}
如果我取消注释......
//var delayUnit = 0.02; transitionUnit = 0.04;
......它完美无瑕。
但我想将它与其他发送不同delayUnits和transitionUnits的函数一起使用。我错过了什么吗?
答案 0 :(得分:1)
明确声明所有变量:
function animate(num, element, transitionUnit, delayUnit) {
var delay = 0;
var transition = 0;
var x = document.getElementById(element).getElementsByTagName("LI");
for (var i = 0; i <= x.length - 1; i++) {
x[i].style.WebkitTransform = "translate3d(" + num + "px,0,0)";
x[i].style.transition = transition + "s " + delay + "s ease-in-out";
delay += delayUnit;
transition += transitionUnit;
if (x[i].querySelectorAll('ul li').length > 0) {
x[i].style.background = "rgba(0,0,0,0.35)";
}
}
}
在for循环中使用隐式全局变量会导致问题。
正如史蒂科提到的,你可能会反思价值观:
animate(0,"mainNav",0.04,0.02);