简体   繁体   English

如何设置div的样式,以使它们不会彼此堆叠?

[英]How to style div's so that they don't stack right on top of each other?

I'm trying to create a X x Y grid of jquery.droppable elements inside a single div. 我正在尝试在单个div内创建jquery.droppable元素的X x Y网格。

for (var y = 0; y < 3; y++) {
        for (var x = 0; x < 3; x++) {
            $('<div>grid_' + x + '-' + y + '_').data( {'coordinates': [x, y]} ).appendTo( '#canvas-screen' ).droppable( {
                accept: '#elements-screen div',
                hoverClass: 'hovered',
                drop: handleElementDrop
            });
        }
    }

As you can see in the code above, I'm creating 9 jquery.droppable elements and appending them to a div called #canvas-screen . 如您在上面的代码中看到的,我正在创建9个jquery.droppable元素,并将它们附加到名为#canvas-screen的div中。 The only problem is that they're just stacking one on top of the other. 唯一的问题是它们只是一个堆叠在另一个之上。 How can i make it so that it would be a 3 x 3 grid, or more generally, an mxn grid, since I'll later want the user to choose the dimensions. 我将如何使其成为3 x 3的网格,或更一般地说是mxn的网格,因为稍后我会希望用户选择尺寸。

Upon inspecting my elements after the DOM has loaded, I'm getting: 在DOM加载后检查我的元素时,我得到:

<div id="canvas-screen">
  <div class="ui-droppable"></div>
  <div class="ui-droppable"></div>
  <div class="ui-droppable"></div>
  <div class="ui-droppable"></div>
  <div class="ui-droppable"></div>
  <div class="ui-droppable"></div>
  <div class="ui-droppable"></div>
  <div class="ui-droppable"></div>
  <div class="ui-droppable"></div>
</div> 

which makes sense to me, but I'm just not sure what CSS to add to get it to not just stack on top of each other. 这对我来说很有意义,但是我不确定要添加什么CSS使其不只是堆叠在一起。

kpscript, not sure how big your canvas-screen should be, but you can add a style of float: left to each .ui-droppable, and then give the #canvas-screen a width. kpscript,不确定您的画布屏幕应该有多大,但是您可以添加一个float样式:在每个.ui-droppable左边,然后为#canvas-screen设置一个宽度。

#canvas-screen {
     width: 500px;
}
.ui-droppable {
     width: 33%;
     float: left;
}

Here's a possible solution to setting up the grid programmatically using javascript. 这是使用javascript以编程方式设置网格的可能解决方案。 Each element positioned according to it's width, height, x and y. 每个元素根据其宽度,高度,x和y定位。

var width = 100,
    height = 100,
    m = 3,
    n = 3;
for (var y = 0; y < m; y++) {
        for (var x = 0; x < n; x++) {
            $('<div>grid_' + x + '-' + y + '_').data( {'coordinates': [x, y]} )
                .css({
                  "width"    : width  + "px",
                  "height"   : height + "px",
                  "left"     : width * x  + "px",
                  "top"      : height * y + "px",
                  "position" : "absolute"
                })
                .appendTo( '#canvas-screen' )
                .droppable( {
                    accept: '#elements-screen div',
                    hoverClass: 'hovered',
                    drop: handleElementDrop
                });
         }
}

Here is an another alternative solution, i gave fixed width 50px to droppable box 这是另一种替代解决方案,我给可放置框固定宽度为50px

<style>    
.column{
    clear:both;
}
.ui-droppable{
  width:50px;
  height:50px;
  border:1px solid black;
  display:block;  
  float:left;
}
</style>
<script>
    $(document).ready(function(){
        for (var y = 0; y < 3; y++) {
            $('<div class="column">').appendTo( '#canvas-screen');
            for (var x = 0; x < 3; x++) {
                $('<div>grid_' + x + '-' + y + '_').data( {'coordinates': [x, y]} ).appendTo( ".column:eq("+y+")").droppable( {
                    accept: '#elements-screen div',
                    hoverClass: 'hovered',
                    drop: handleElementDrop
                });
            }
        }
    function handleElementDrop(){
    }
    });
</script>


    <div id="canvas-screen">
    </div>

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

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