这就是我想要做的:
我无法获得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)函数 -编写整个动画过程等。并应用新的随机值。