我对我的功能功能的否定感到非常困惑。原始函数从第n个索引开始采用9个元素,并减少它们的变换位置。
function pushIt(max, target, index, count) {
if (count == max || count == img.children ) {
running = false;
return;
}
var tmp = target[index];
var matrix = window.getComputedStyle(tmp).getPropertyValue("transform");
var translate_left = matrix.split(",")[4];
var translate_top = matrix.split(",")[5].split(")")[0]-215;
tmp.style.transform = "translate3d(" + translate_left + "px," + translate_top + "px,0)";
setTimeout(function(){
pushIt( max, target, index + 1, count + 1 );
},50)
}
我想做的是否定它的功能,例如它不会减少但增加(第n * 3)-1个元素的变换位置(倒数9个元素)
function pushItDOWN(max, target, index , count) {
if ( count == max || index < 0 ) {
running = false;
return;
}
console.log("down");
var tmp = target[index];
var matrix = window.getComputedStyle(tmp).getPropertyValue("transform");
var translate_left = matrix.split(",")[4];
var translate_top = matrix.split(",")[5].split(")")[0]+215;
tmp.style.transform = "translate3d(" + translate_left + "px," + translate_top + "px,0)";
setTimeout(function(){
pushItDOWN(max, target, index - 1, count + 1 );
},50)
}
}
第二个函数做的是获取元素并将它们的变换设置为遗忘(例如,在视口之外)并以某种方式破坏第一个函数的功能。
我是否忽略了导致问题的一些关键事实,我似乎找不到问题的根源。 实时demo以便更好地理解
答案 0 :(得分:1)
我不是百分百肯定,但很可能这是你的错误:
这将产生一个字符串:
matrix.split(",")[5].split(")")[0]
让我们说它是"500"
,然后是
matrix.split(",")[5].split(")")[0] + 215
// equals
"500" + 215
// results in (because + is both used as string concatenation as addition)
"500215"
// - will work, because it only has one meaning
"500" - 215 // results in 285
在添加215之前将值解析为int(或必要时浮动):
parseInt(matrix.split(",")[5].split(")")[0]) + 215