如何在translate3d函数上使用变量?

时间:2017-05-28 04:00:49

标签: javascript html css

我目前正在研究我的投资组合,我在javascript中有一个名为translate3d(0,10px,0)的函数。我的问题是,如果我想输出变量而不是10px以避免硬编码,我该怎么做?我尝试过translate3d(0,a,0)假设一个变量值为10px(就像这样a =& #34; 10px";)但它没有对页面进行任何更改,也没有在控制台中看到错误。提前谢谢。

// This is how the function looks like
function levelBox(){
    setTimeout(function (){
    e1.style.webkitTransitionDuration = "1s";
    e1.style.webkitTransitionTimingFunction = "ease-out";
    e1.style.webkitTransform = "translate3d(0, 'a', 0)";
}, 0);
}

2 个答案:

答案 0 :(得分:0)

您将translate3d函数作为字符串传递给webkitTransform甚至a

试试这个

function levelBox(){
    setTimeout(function () {
    var a = "10px";
    var value = translate3d(0, a, 0);
    e1.style.webkitTransitionDuration = "1s";
    e1.style.webkitTransitionTimingFunction = "ease-out";
    e1.style.webkitTransform = value;
    }, 0);
}

答案 1 :(得分:0)

按如下方式进行:

function levelBox(_x, _y, _z){
    setTimeout(function (){
    e1.style.webkitTransitionDuration = "1s";
    e1.style.webkitTransitionTimingFunction = "ease-out";
    e1.style.webkitTransform = "translate3d("+_x+"px, "+_y+"px, "+_z+"px)";
}, 0);
}