-1

我正在尝试使用 javascript 在页面顶部创建多个框。我有一个盒子,但不知道如何在页面顶部获得多个。这是我到目前为止所拥有的:

    <html>
  <head>
    <title>Boxes on Boxes on Boxes</title>
    <link rel="stylesheet" type="text/css" href="boxes.css">
    <script language="JavaScript">
        el=document.getElementById("box1");
        width=window.innerWidth-50;
        height=window.innerHeight-50;
        el.style.left=width*Math.random();
        el.style.top=height*Math.random();

        el=document.getElementById("box2");
        width=window.innerWidth-50;
        height=window.innerHeight-50;
        el.style.right=width*Math.random();
        el.style.top=height*Math.random();

        el=document.getElementById("box3");
        width=window.innerWidth-50;
        height=window.innerHeight-50;
        el.style.middle=width*Math.random();
        el.style.top=height*Math.random();

    </script>
  </head>
  <body>
    <div id="box1">
      First box 
    </div>

    <div id="box2">
        Second box
    </div>

    <div id="box3">
        Third box
    </div>
  </body>
</html>

这是我拥有的CSS:

#box1{
    background-color:orange;
    padding:5px;
    width:50px;
    height:50px;
    position:absolute;
    left=100px;
    top=100px;
}
#box2{
    background-color:blue;
    padding:5px;
    width:50px;
    height:50px;
    position:absolute;
    left=100px;
    top=100px;
}
#box3{
    background-color:green;
    padding:5px;
    width:50px;
    height:50px;
    position:absolute;
    left=100px;
    top=100px;
}
4

4 回答 4

0

Try this using jQuery :

Here the boxes should be created dynamically and without naming the id's hardcoded it also should be done in a better way with your code. It's easier now as you are creating 4 boxes, what about 100 or more. So it's wise to always take the better way to maintain scalability of our work.

HTML :

<div id="mainDiv">

</div>

CSS :

// general css for all divs inside mainDiv 
#mainDiv div{
    padding:5px;
    width:50px;
    height:50px;
    position:absolute;
    left=100px;
    top=100px;  
    float : left;
}

jQuery :

$(document).ready(function(){
    // taking a color array
    var colorArray = new Array("red", "green", "gray", "blue");
    // loop through as many boxes you want to create
    for(var i = 1; i <= colorArray.length; i++){
        $("#mainDiv").append("<div id=Box" + i + "></div>");
        //changing the background-color 
        $("#Box"+ i).css("background-color", colorArray[i-1]);
    }
});

Demo

Here's Another thread explaining similar Case, And It's Solution

于 2013-10-27T21:32:13.723 回答
0

您需要将<script>元素移动到末尾或将代码包装在 DOM 就绪或 onload 处理程序中,否则getElementById()将找不到任何元素,因为它们尚未被解析。

然后您需要在和样式属性中包含一个单位(例如, "px") 。lefttop

此外,无需为每个框重新计算widthand height,因为您对每个框都进行了相同的计算。(并且你应该用 声明你的变量var,但是虽然良好的做法对于让它工作并不是必不可少的。)

这是一个工作版本:

    var el=document.getElementById("box1");
    var width=window.innerWidth-50;
    var height=window.innerHeight-50;
    el.style.left=width*Math.random() + "px";
    el.style.top=height*Math.random() + "px";

    el=document.getElementById("box2");
    el.style.right=width*Math.random() + "px";
    el.style.top=height*Math.random() + "px";

    el=document.getElementById("box3");
    el.style.middle=width*Math.random() + "px";
    el.style.top=height*Math.random() + "px";

演示:http: //jsfiddle.net/m3Gg3/

CSS 中的leftandtop属性也应该使用:not =

于 2013-10-27T21:09:07.870 回答
0

很难理解你想要什么,也许是这个?

在此处输入图像描述

<script>
window.onload = function() {
    var titles = ["First box", "Second box", "Third box"]
    var width=window.innerWidth-50
    var height=window.innerHeight-50-120
    for (var i = 0; i < titles.length; i++) {
        var el = document.createElement('div')
        console.log(el)
        el.innerHTML = titles[i]
        el.style.position = "absolute"
        el.style.border = "1px solid rgb(0,0,0)"
        el.style.left= (width / titles.length) * i
        el.style.top=0
        el.style.width = width / titles.length
        el.style.height = "120px"
        document.body.appendChild(el);
    }
    for (var i = 0; i < titles.length; i++) {
        var el = document.createElement('div')
        console.log(el)
        el.innerHTML = titles[i]
        el.style.position = "absolute"
        el.style.border = "1px solid rgb(0,0,0)"
        el.style.left=0
        el.style.top=(height / titles.length) * i + 120
        el.style.width = "120px"
        el.style.height = height / titles.length
        document.body.appendChild(el);
    }
}
</script>
于 2013-10-27T21:25:19.090 回答
0
    <html>
  <head>
    <title>Boxes on Boxes on Boxes</title>
    <style type="text/css">
#box_group1, #box_group2, #box_group3, #box_group4, #textbook {
    position:absolute;
    left:100px;
    top:100px;      
}
#box1, #box2, #box3, #box10, #box11, #box12 {
    padding:5px;
    width:50px;
    height:50px;
    cursor:pointer;
    float:left;
}
#box4, #box5, #box6, #box7, #box8, #box9 {
    padding:5px;
    width:50px;
    height:50px;
    cursor:pointer;
}   
#box1, #box4, #box7, #box10{
    background-color:orange;
}
#box2, #box5, #box8, #box11 {
    background-color:blue;
}
#box3, #box6, #box9, #box12{
    background-color:green;
}
#box4, #box7 {
    font-family: Arial;
}
#box5, #box8 {
    font-family: Courier;
}
#box6, #box9 {
    font-family: Tahoma;
}   
#textbook {
    padding: 5px;
    background-color:red;
}
    </style>
    <script language="JavaScript">
        width=window.innerWidth;
        height=window.innerHeight;
    function boxes() {  
        document.getElementById("box_group1").style.left=(width-document.getElementById("box_group1").offsetWidth)/2;
        document.getElementById("box_group2").style.top=(height-document.getElementById("box_group2").offsetHeight)/2;
        document.getElementById("box_group3").style.left=width-100-document.getElementById("box_group3").offsetWidth;
        document.getElementById("box_group3").style.top=(height-document.getElementById("box_group3").offsetHeight)/2;
        document.getElementById("box_group4").style.left=(width-document.getElementById("box_group4").offsetWidth)/2;
        document.getElementById("box_group4").style.top=height-100-document.getElementById("box_group4").offsetHeight;  
        document.getElementById("textbook").style.left=(width-document.getElementById("textbook").offsetWidth)/2;
        document.getElementById("textbook").style.top=(height-document.getElementById("textbook").offsetHeight)/2;
    }

    function colorChange(field,group) {
        switch (group) {
            case 1:
                document.getElementById("box2").style.backgroundColor = field.innerText;
                break;
            case 4:
                document.getElementById("box11").style.backgroundColor = field.innerText;
                break;
        }       
    }
    function fontChange(field,group) {
        switch (group) {
            case 2:
                document.getElementById("box5").style.fontFamily = field.innerText;
                break;
            case 3:
                document.getElementById("box8").style.fontFamily = field.innerText;
                break;
        }       
    }       
    </script>
  </head>
  <body onload="boxes()">
    <div id="box_group1">
        <div id="box1" onclick="colorChange(this,1)">
            Orange 
        </div>

        <div id="box2" onclick="colorChange(this,1)">
            Blue
        </div>

        <div id="box3" onclick="colorChange(this,1)">
            Green
        </div>
    </div>
    <div id="box_group2">
        <div id="box4" onclick="fontChange(this,2)">
            Arial 
        </div>

        <div id="box5" onclick="fontChange(this,2)">
            Courier
        </div>

        <div id="box6" onclick="fontChange(this,2)">
            Tahoma
        </div>
    </div>
    <div id="box_group3">
        <div id="box7" onclick="fontChange(this,3)">
            Arial
        </div>

        <div id="box8" onclick="fontChange(this,3)">
            Courier
        </div>

        <div id="box9" onclick="fontChange(this,3)">
            Tahoma
        </div>
    </div>
    <div id="box_group4">
        <div id="box10" onclick="colorChange(this,4)">
            Orange 
        </div>

        <div id="box11" onclick="colorChange(this,4)">
            Blue
        </div>

        <div id="box12" onclick="colorChange(this,4)">
            Green
        </div>
    </div>
    <div id="textbook">Textbook</div>
  </body>
</html>

盒子

于 2013-10-27T21:14:54.577 回答