variable.style.backgroundColor未定义

时间:2014-08-31 01:39:14

标签: javascript

好吧,我已经尝试了很多不同的方法,似乎无法让它发挥作用。我一直收到ObjectID未定义的错误,我不知道在哪里看。

    // This is my attempt at parallax scrolling

window.onscroll = moveObj( 'box', 300, 3, 4) || moveObj();

function moveObj(elem, endpt, hrspd, vrspd) {
    elem = elem || "";  endpt = endpt || 0; hrspd = hrspd || 0; vrspd = vrspd || 1;

    //Get elements
    var ObjectID = (document.getElementById(elem) || "box2");

    ObjectID.style.backgroundColor = "Purple";

    //Set StopPoint and Speed
    ObjectID.style.left=((window.pageYOffset<endpt)?(window.pageYOffset):(-window.pageYOffset+(endpt*2))*vrspd);

    //Eliminate vertical movement
    ObjectID.style.top = (window.pageYOffset)*hrspd;
};

提前感谢您的帮助!

1 个答案:

答案 0 :(得分:0)

试试这个

window.onscroll = function () {
    moveObj( 'box', 300, 3, 4);
}

function moveObj(elem, endpt, hrspd, vrspd) {
    elem = elem || "";  endpt = endpt || 0; hrspd = hrspd || 0; vrspd = vrspd || 1;

    //Get elements
    var ObjectID = (elem);
    var Object = document.getElementById(ObjectID)
    Object.style.backgroundColor = "Purple";

    //Set StopPoint and Speed
    Object.style.left=((window.pageYOffset<endpt)?(window.pageYOffset):(-window.pageYOffset+(endpt*2))*vrspd);

    //Eliminate vertical movement
    Object.style.top = (window.pageYOffset)*hrspd;
};
编辑:在这种情况下,avril alejandro对你的问题的评论是正确的。对moveObj()进行了不必要的调用。我已编辑代码以删除此内容和&#34; box2&#34;参考