简体   繁体   English

Divs停留在一条线上吗?

[英]Divs are staying in one line?

I have this script and the problem with it is that I cannot find a way to make the divs that are created to float randomly on the page. 我有这个脚本,它的问题是我找不到一种使所创建的div随机浮动在页面上的方法。 They stay in the left corner. 他们留在左上角。 Do I need to change the body dimensions or change the position. 我是否需要更改车身尺寸或位置。 I guess something is blocking them not to. 我猜有什么阻止他们不这样做。
One more thing. 还有一件事。 If someone of you would revise the code and write re-write it following the best practices in JavaScript It will be appreciated! 如果您中的某人按照JavaScript的最佳做法修改了代码并编写了重写代码,将不胜感激!

(function create() {
    var docFragment = document.createDocumentFragment();
    var count = 5;
    var i = 0;
    for (i = 0; i < count; i++) {
        var numerousDivs = document.createElement("div");
        docFragment.appendChild(numerousDivs);
    }
    document.body.appendChild(docFragment);

    var fiveDivs = document.getElementsByTagName("div");
    for (i = 0; i < fiveDivs.length; i++) {
        div = fiveDivs[i];
        alter(div);
        moveDivs(div);
    }
}());

function moveDivs(div) {
    div.style.position = "absolute";
    var topPos = parseInt(Math.random() * (maxHeight - 40));
    div.style.top = topPos + "px";
    var left = parseInt(Math.random() * (maxWidth - 100));
    div.style.left + "px";
}

div.style.left + "px"; should be div.style.left = left+"px"; 应该是div.style.left = left+"px"; .

div.style.left + "px";

这是一个错字。

div.style.left = left+"px";

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM