正确的方法来获取从另一个函数创建的css属性值

时间:2018-12-18 13:45:43

标签: javascript html css

这就是我想要做的:

  • 功能:创建一个具有上下左右随机值的div
  • 功能:获取该对象的css值(我在那)并移动此div:

我无法获得css值。返回:

TypeError: undefined is not an object (evaluating 'elem1.style.top')

有一个javascript文件:

var r = 50;
var rc = r /2;
var q = 0.2;
var wq = window.innerWidth/q;

function getRandomIntInclusive(min, max) 
{
  min = Math.ceil(min);
  max = Math.floor(max);
  return Math.floor(Math.random() * (max - min +1)) + min;
}
function getPosition()
{
    var elem1 = document.getElementsByClassName('circle');
    var value = elem1.style.top;
    alert(value);
}
function createObject(wq,r,rc)
{
    var square = document.createElement('div');
    square.className = 'square';
    square.style.width = "" + r + "px";
    square.style.height = "" + r + "px";

    var circle = document.createElement('div');
    circle.className = 'circle';
    circle.style.width = "" + rc + "px";
    circle.style.height = "" + rc + "px";
    circle.style.top = "" + getRandomIntInclusive(0, rc) + "px";
    circle.style.left = "" + getRandomIntInclusive(0,rc) + "px";
    square.appendChild(circle);

    document.body.appendChild(square);
    getPosition()
}
createObject(wq,r,rc);

答案: -将我创建的div发送到getPosition(circle)函数 -编写整个动画过程等。并应用新的随机值。

0 个答案:

没有答案