Masonry is a very powerful jquery dynamic grid layout plug-in that helps developers quickly develop an interface effect similar to clip art. The difference between float and CSS is that float is arranged horizontally and then vertically, using masonry to arrange the elements vertically and then place the next element in the next development area in the grid. This effect minimizes the vertical clearance of elements with different heights. As follows:
As you can see, using float in a grid layout to handle elements of different heights makes the distance between elements in the vertical direction larger, and the interval becomes smaller after using masonry processing.
Usage
First pour into the class library, as follows:
. Code
- <script src="//ajax.googleapis.com/ajax/libs/jquery/1.6.1/jquery.min.js" >
- </script><script src="/path/to/jquery.masonry.min.js" ></script>
Then, execute the masonry for the element container, as follows:
. Code
- $ (function () {
- $ (' #container '). Masonry ({
- Options
- Itemselector: '. Item ',
- ColumnWidth:
- });
- });
HTML code
. Code
- <div id="container" >
- <div class="item" >...</div>
- <div class="item" >...</div>
- <div class="item" >...</div>
- ...
- </div>
Css
. Code
- . Item {
- width:220px;
- margin:10px;
- Float:left;
- }
If you have a picture in your loaded element, you need to make sure that masonry executes after all of the pictures have been loaded, calling the following code:
. Code
- var $container = $ (' #container ');
- $container. imagesloaded (function () {
- $container. Masonry ({
- Itemselector: '. Item ',
- ColumnWidth:
- });
- });
JQuery masonry-Powerful dynamic irregular layout plugin that makes your Web page more natural