简体   繁体   English

如何使用CSS和JavaScript创建可自定义的动态网格布局?

[英]How to create customizable, dynamic grid layout using CSS and JavaScript?

I am working on a project that involves a lot of CSS. 我正在开发一个涉及大量CSS的项目。 The customer wants to have a grid layout on the home page where he wants to be able to rearrange UI components with drag and drop. 客户希望在主页上具有网格布局,他希望能够通过拖放重新排列UI组件。 These UI components could be of different sizes: 1x1, 2x2 and 3x3. 这些UI组件可以具有不同的大小:1x1,2x2和3x3。 When I drop an UI item at the desired new location it should push the other components aside. 当我将UI项目放在所需的新位置时,它应该将其他组件推到一边。 Any possible holes should be filled with 1x1 components. 任何可能的孔应填充1x1组件。

How it should work 它应该如何工作

  1. Before I have draged a component 在我放弃一个组件之前
  2. Draging the 2x2 component 拖动2x2组件
  3. Dropping the component in the middle, the two 1x1 components make room and adapt around the 2x2 将组件放在中间,两个1x1组件可以腾出空间并适应2x2

Note that the size of the grid is not limited to 8 1x1, but the height as well as the width of it should be possible to expand and make smaller. 请注意,网格的大小不限于8 1x1,但高度和宽度应该可以扩展并缩小。

I'll rather not use tables but other than that I am open to suggestions. 我宁愿不使用表格,但除此之外,我愿意接受建议。 Right now I've just used inline-block divs which I can drag and drop to switch the jQuery DOM objects. 现在我刚刚使用了内联块div,我可以拖放来切换jQuery DOM对象。 The effect isn't quite what the customer wants: How it is now 效果并不是客户想要的: 现在如何

I've made a lot dynamic layouts with the same idea. 我用相同的想法做了很多动态布局。 You need to think more in how your float behavior from block to block is stopping for the next following blocks, so they become correct repositioned like you want. 您需要更多地考虑从块到块的浮动行为如何在下一个块中停止,以便它们按照您的需要重新定位。 So to define a float-stop element is necessary. 因此,定义浮点元素是必要的。 Your blocks will work with float:left maybe float:right . 你的块可以使用float:left也许float:right At some point you will figure out that this behavior has to stop somewhere best done with CSS 在某些时候,你会发现这种行为必须停止在CSS最好的地方

.floatstop {
     clear: both; //the important code here..
     width: 100%;
     height: 1px;
     line-height: 1px;
     margin-top:-1px;
}

and Html 和Html

<div class="floatstop"></div> 

Made of all blocks who need border to the next block on the left side (maybe right side too) you have to define a base layout which has space for the very right placed block too with borderspace for it, otherwise it would float down under the block before. 由需要与左侧下一个区域(也许是右侧)边界的所有区块组成,您必须定义一个基本布局,该区域具有用于非常右侧放置区块的空间,并且具有边界空间,否则它将在阻止之前。

But there is a more modern way! 但是有一种更现代的方式! You can use CSS3 codes to define your layout. 您可以使用CSS3代码来定义布局。

.columnblock {
     width: 100%;
     column-gap: 30px;
     // for symmetric columned layouts use..
     column-count: 3; 
     // or for not symmetric layouts use..
     column-width: 280px;
}

<div class="columnblock">
    <p>Lorem Ipsum</p>
    <p>another Paragraph</p>
</div>

There other things to mention here but you can read about http://www.w3schools.com/css3/css3_multiple_columns.asp 还有其他的事情需要提及,但你可以阅读http://www.w3schools.com/css3/css3_multiple_columns.asp

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

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