-1

JavaScript を使用して、ページの上部に沿って複数のボックスを作成しようとしています。ボックスは 1 つですが、ページの上部に複数のボックスを配置する方法がわかりません。これは私がこれまでに持っているものです:

    <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

要素を最後に移動するか<script>、DOM Ready または 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のleftおよびプロパティはnotを使用する必要があります。top:=

于 2013-10-27T21:09:07.870 に答える
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 に答える
0

jQuery を使用してこれを試してください:

ここでは、ボックスを動的に作成する必要があり、ハードコードされた ID に名前を付けずに、コードでより適切な方法で行う必要があります。4 つのボックス、つまり 100 以上のボックスを作成しているので、今は簡単です。したがって、作業のスケーラビリティを維持するには、常により良い方法を取ることが賢明です。

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]);
    }
});

デモ

これは、同様のケースを説明する別のスレッドであり、 それは解決策です

于 2013-10-27T21:32:13.723 に答える
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 に答える