无法将div元素定位在随机X / Y.

时间:2016-12-20 16:33:06

标签: javascript

// T duplicates the selected element(s).
case 116:
    elementSel = document.querySelectorAll("div.selected");
    var elementClone,
    tmp = getBrowserWidth(),
    bww = tmp[0],
    bwh = tmp[1];

    for (i = 0; i < elementSel.length; i++) {
        elementClone = elementSel[i].cloneNode(true);
        elementClone.id = "boxID" + Math.floor((1 + Math.random()) * 0x10000);
        elementClone.zIndex = "+1";

        var posx = getRandomInt(1, bww) - elementSel[i].offsetWidth;
        var posy = getRandomInt(1, bwh) - elementSel[i].offsetHeight;

        elementClone.style.left = posx + " px";
        elementClone.style.top = posy + " px";

        elementSel[i].appendChild(elementClone);
        elementSel[i].classList.toggle("selected");

        console.log("Created " + elementSel.length + " elements.");
    }

我被困在做学校作业。我试图阅读类似的问题,但我没有进一步。问题是elementClone.style.left/.top未分配新值。为什么呢?

2 个答案:

答案 0 :(得分:0)

相关答案: 它对我有用,可能是您需要更改positionfloat属性的原因。

答案 1 :(得分:0)

好的,我解决了。问题是public func saveGraph() { UIGraphicsBeginImageContextWithOptions(view.bounds.size, false, UIScreen.main.scale) view.layer.render(in: UIGraphicsGetCurrentContext()!) let image = UIGraphicsGetImageFromCurrentImageContext() UIGraphicsEndImageContext() UIImageWriteToSavedPhotosAlbum(image!, nil, nil, nil) } 。 Crappé!