Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Fetching contributors…

Cannot retrieve contributors at this time

executable file 328 lines (236 sloc) 16.412 kB
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
<title>jQuery Masonry &rsaquo; Primer</title>
<link rel="stylesheet" type="text/css" href="css/masonry-example.css" media="screen" />
<script type="text/javascript" src="js/jquery-1.4.2.min.js"></script>
<script type="text/javascript" src="js/jquery.masonry.js"></script>
<script type="text/javascript">
$(function(){
$('#secondary').masonry({columnWidth: 100});
})
</script>
</head>
<body>
<ol id="nav">
<li><strong>jQuery Masonry &rsaquo;</strong></li>
<li class="selected"><a href=".">Primer</a></li>
<li><a href="basic.html">Basic Examples</a></li>
<li><a href="tumblelog.html">Tumblelog Example</a></li>
<li><a href="infinite-scroll.html">Infinite Scroll Example</a></li>
<li><a href="animated.html">Animated Example</a></li>
</ol>
<h1>Primer</h1>
<div class="copy">
<p>Masonry is a layout plugin for jQuery. Think of it as the flip side of CSS floats. Whereas floating arranges elements horizontally then vertically, Masonry arranges elements vertically then horizontally according to a grid. The result minimizes vertical gaps between elements of varying height, just like a mason fitting stones in a wall.</p>
</div><!-- .copy -->
<div id="comparison">
<h2>Comparison Example</h2>
<div class="example">
<h3>CSS Floats</h3>
<div id="primary" class="wrap">
<div class="box col1">
<h5>1</h5>
<p>Donec nec justo eget felis facilisis fermentum. Aliquam porttitor mauris. </p>
</div>
<div class="box col2">
<h5>2</h5>
<p>Nullam malesuada erat ut turpis. Suspendisse urna nibh, viverra non, semper suscipit.</p>
</div>
<div class="box col1">
<h5>3</h5>
<p>Fusce accumsan mollis eros. Pellentesque a diam sit amet mi ullamcorper vehicula. Ut eget sem risus, et posuere velit. Aenean ac mauris non ligula. </p>
</div>
<div class="box col1">
<h5>4</h5>
<p>Morbi purus libero, faucibus adipiscing, commodo quis, gravida id, est. Sed lectus.</p>
</div>
<div class="box col1">
<h5>5</h5>
<p>Lorem ipsum dolor sit amet, consectetuer adipiscing elit. Donec odio.</p>
</div>
<div class="box col1">
<h5>6</h5>
<p>Cadipiscing in, lacinia vel, tellus. </p>
</div>
<div class="box col1">
<h5>7</h5>
<p>Pellentesque a diam sit amet mi ullamcorper vehicula. adipiscing in, lacinia vel, tellus.</p>
</div>
<div class="box col3">
<h5>8</h5>
<p>Sed ac risus. Phasellus lacinia, magna a ullamcorper laoreet, lectus arcu pulvinar risus, vitae facilisis libero dolor a purus. Suspendisse ac urna. Etiam pellentesque. Sed vel lacus. Mauris nibh felis, adipiscing varius, adipiscing in, lacinia vel, tellus. Suspendisse ac urna. Etiam pellentesque mauris ut lectus. Nunc tellus ante, mattis eget, gravida vitae, ultricies ac, leo. Integer leo pede, ornare a, lacinia eu, vulputate vel, nisl. Suspendisse ac urna. Etiam pellentesque. </p>
</div>
<div class="box col2">
<h5>9</h5>
<p>Ut convallis, sem sit amet interdum consectetuer, odio augue aliquam leo, nec dapibus tortor nibh sed augue. Ut condimentum mi vel tellus. Suspendisse laoreet. Fusce ut est sed dolor gravida convallis. Morbi vitae ante. Vivamus ultrices luctus nunc. Suspendisse et dolor. Pellentesque a diam sit amet mi ullamcorper vehicula. adipiscing in, lacinia vel, tellus.</p>
</div>
<div class="box col1">
<h5>10</h5>
<p>Etiam pellen tesque mauris ut lectus.</p>
</div>
<div class="box col1">
<h5>11</h5>
<p>Mauris nibh felis, adipiscing varius, adipiscing in, lacinia vel, tellus. Suspen disse ac urna. Ut condi mentum mi vel tellus.</p>
</div>
<div class="box col1">
<h5>12</h5>
<p>Phasellus pede arcu, dapibus eu, fermentum et, dapibus sed, urna.</p>
</div>
</div> <!-- /#primary.wrap -->
</div>
<div class="example">
<h3>Masonry</h3>
<div id="secondary" class="wrap">
<div class="box col1">
<h5>1</h5>
<p>Donec nec justo eget felis facilisis fermentum. Aliquam porttitor mauris. </p>
</div>
<div class="box col2">
<h5>2</h5>
<p>Nullam malesuada erat ut turpis. Suspendisse urna nibh, viverra non, semper suscipit.</p>
</div>
<div class="box col1">
<h5>3</h5>
<p>Fusce accumsan mollis eros. Pellentesque a diam sit amet mi ullamcorper vehicula. Ut eget sem risus, et posuere velit. Aenean ac mauris non ligula. </p>
</div>
<div class="box col1">
<h5>4</h5>
<p>Morbi purus libero, faucibus adipiscing, commodo quis, gravida id, est. Sed lectus.</p>
</div>
<div class="box col1">
<h5>5</h5>
<p>Lorem ipsum dolor sit amet, consectetuer adipiscing elit. Donec odio.</p>
</div>
<div class="box col1">
<h5>6</h5>
<p>Cadipiscing in, lacinia vel, tellus. </p>
</div>
<div class="box col1">
<h5>7</h5>
<p>Pellentesque a diam sit amet mi ullamcorper vehicula. adipiscing in, lacinia vel, tellus.</p>
</div>
<div class="box col3">
<h5>8</h5>
<p>Sed ac risus. Phasellus lacinia, magna a ullamcorper laoreet, lectus arcu pulvinar risus, vitae facilisis libero dolor a purus. Suspendisse ac urna. Etiam pellentesque. Sed vel lacus. Mauris nibh felis, adipiscing varius, adipiscing in, lacinia vel, tellus. Suspendisse ac urna. Etiam pellentesque mauris ut lectus. Nunc tellus ante, mattis eget, gravida vitae, ultricies ac, leo. Integer leo pede, ornare a, lacinia eu, vulputate vel, nisl. Suspendisse ac urna. Etiam pellentesque. </p>
</div>
<div class="box col2">
<h5>9</h5>
<p>Ut convallis, sem sit amet interdum consectetuer, odio augue aliquam leo, nec dapibus tortor nibh sed augue. Ut condimentum mi vel tellus. Suspendisse laoreet. Fusce ut est sed dolor gravida convallis. Morbi vitae ante. Vivamus ultrices luctus nunc. Suspendisse et dolor. Pellentesque a diam sit amet mi ullamcorper vehicula. adipiscing in, lacinia vel, tellus.</p>
</div>
<div class="box col1">
<h5>10</h5>
<p>Etiam pellen tesque mauris ut lectus.</p>
</div>
<div class="box col1">
<h5>11</h5>
<p>Mauris nibh felis, adipiscing varius, adipiscing in, lacinia vel, tellus. Suspen disse ac urna. Ut condi mentum mi vel tellus.</p>
</div>
<div class="box col1">
<h5>12</h5>
<p>Phasellus pede arcu, dapibus eu, fermentum et, dapibus sed, urna.</p>
</div>
</div> <!-- /#secondary.wrap -->
</div>
</div>
<div class="copy">
<h2 id="min-config">Minimum recommended configuration</h2>
<p>Configuring Masonry is fairly easy. Simply attach the <code>.masonry()</code> method to the wrapping container element. Depending on the layout, you&rsquo;ll most likely need to specify one option.</p>
<p>For layouts with elements that span multiple widths:</p>
<pre><code><span class="keyword">$</span>(<span class="string">'#wrapper'</span>).masonry({ columnWidth: <span class="constant">200</span> });</code></pre>
<p>For layouts with elements that span the same width:</p>
<pre><code><span class="keyword">$</span>(<span class="string">'#wrapper'</span>).masonry({ singleMode: <span class="constant">true</span> });</code></pre>
<h2 id="options">All options</h2>
<pre><code><span class="keyword">$</span>(<span class="string">'#wrapper'</span>).masonry({
singleMode: <span class="constant">false</span>,
<span class="comment">// Disables measuring the width of each floated element.
// Set to true if floated elements have the same width.
// default: false</span>
columnWidth: <span class="constant">240</span>,
<span class="comment">// Width in pixels of 1 column of your grid.
// default: outer width of the first floated element.</span>
itemSelector: <span class="string">'.box:visible'</span>,
<span class="comment">// Additional selector to specify which elements inside
// the wrapping element will be rearranged.
// Required for Infinite Scroll with window resizing.</span>
resizeable: <span class="constant">true</span>,
<span class="comment">// Binds a Masonry call to window resizes
// so layout appears fluid.
// default: true</span>
animate: <span class="constant">true</span>,
<span class="comment">// Animates layout rearrangements.
// default: false</span>
duration: <span class="constant">1500</span>,
<span class="comment">// If animated, speed of animation.
// default: 'normal'</span>
easing: <span class="string">'linear'</span>,
<span class="comment">// If animated, the easing function of the animation.
// default: 'swing'</span>
appendedContent: <span class="keyword">$</span>(<span class="string">'.new_content'</span>),
<span class="comment">// Additional container element for appended content.
// Useful for Infinite Scroll integration.</span>
saveOptions: <span class="constant">true</span>,
<span class="comment">// Masonry will use the options from previous Masonry
// calls by default, so you only have to enter in options once
// default: true</span>
}, <span class="storage">function</span>() {}
<span class="comment">// Optional callback.
// 'this' will refer to the applicable elements Masonry just rearranged</span>
);</code></pre>
<p>No need to worry about how many columns, or width of gutters, or how big the container is. Thanks to the wonders of jQuery’s <code>outerWidth()</code> and <code>innerWidth()</code> methods, Masonry appropriately accounts for the space of any margin, padding, and border-width set with CSS. </p>
<h2 id="resizing">Window Resizing</h2>
<p>By default, Masonry binds a call to the browser window for resizing. But the rearrangement script is triggered only when the layout adds or loses a column, so you don&rsquo;t have to worry about Masonry slowing down window resizes, or with fixed layouts.</p>
<h2 id="images">Handling Images &amp; Other Media</h2>
<p>Since Masonry measures the height of the elements when placing them, you will need to account for images and other media that haven&rsquo;t loaded yet. For images, the best method is to specify the width and height of images inline.</p>
<pre><code><span class="storage">&lt;img src=<span class="string">"img_file.jpg"</span> <span class="string">width="280"</span> <span class="string">height="160"</span> /&gt;</span></code></pre>
<p>If you&rsquo;re using a PHP-based CMS, you can use the <a href="http://php.net/manual/en/function.getimagesize.php">getimagesize</a> function.</p>
<p>If this is not possible or if you&rsquo;re dealing with @font-face fonts, another option is to call Masonry after all media has loaded. This is done by calling the function inside of <code>$(window).load()</code> instead of <code>$(document).ready()</code>.</p>
<pre><code><span class="keyword">$</span>(<span class="string">'window'</span>).<span class="keyword">load</span>(<span class="storage">function</span>(){
<span class="keyword">$</span>(<span class="string">'#wrapper'</span>).masonry({ columnWidth: <span class="constant">200</span> });
});</code></pre>
<h2 id="anchor-links">Resolving Anchor Links</h2>
<p>Since Masonry relies on absolute positioning, any anchor links that occur within or after the wrapping element will not work when the page first loads. The following script is one solution.</p>
<pre><code><span class="keyword">$</span>(<span class="string">'window'</span>).<span class="keyword">load</span>(<span class="storage">function</span>(){
<span class="keyword">if</span> ( <span class="keyword">window</span>.<span class="keyword">location</span>.<span class="keyword">hash</span> ) {
<span class="storage">var</span> destination = <span class="keyword">$</span>( <span class="keyword">window</span>.<span class="keyword">location</span>.<span class="keyword">hash</span> ).offset().<span class="keyword">top</span>;
<span class="keyword">$</span>(<span class="string">'html:not(:animated),body:not(:animated)'</span>).scrollTop( destination );
}
});</code></pre>
<p>This will set the window of the document to the appropriate place. These couple lines were taken from <a href="http://www.position-absolute.com/articles/better-html-anchor-a-jquery-script-to-slide-the-scrollbar/">Cedric Dugas’s anchorAnything.</a></p>
<h2>Examples in the Wild</h2>
<p>In addition to the examples below, see <a href="http://delicious.com/desandro/jquerymasonry">delicious links tagged with jquerymasonry</a> and <a href="http://emberapp.com/desandro/collections/jquery-masonry">jQuery Masonry Collection on Ember for screenshots</a>.</p>
<ul id="inthewild">
<li><a href="http://bygonebureau.com/">The Bygone Bureau</a></li>
<li><a href="http://safe.tumblr.com/theme/preview/8951">Scaffold Tumblr Theme by Mike Harding</a></li>
<li><a href="http://thomasmatthews.com/index.php/portfolio/print/">thomas.matthews</a></li>
<li><a href="http://www.inkoherence.com/">Inkoherence</a></li>
<a href="http://photography.iamkoa.net/"></a>
<li><a href="http://rathersplendid.net/">Rather Splendid</a></li>
<li><a href="http://chromeography.com">Chromeography</a></li>
<li><a href="http://matthewanderson.cc/">Matthew Anderson</a></li>
</ul>
<h2 id="changelog">Changelog</h2>
<dl>
<dt>v1.1.0: 29 Apr 2010</dt>
<dd>Add animation</dd>
<dt>v1.0.1: 19 Dec 2009</dt>
<dd>Bug fix, checks that container element exists and has children before running the script.</dd>
<dt>v1.0: 7 Dec 2009</dt>
<dd>Multi-column width support</dd>
<dd>Appending elements and Infinite Scroll support</dd>
<dd>Less obstrusive layout. No inserting additional markup.</dd>
<dd>Automatically binds event to window resizing</dd>
<dt>v0.4: 14 Jun 2009</dt>
<dd>Better fluid rearrangement support</dd>
<dt>v0.1: Feb 2009</dt>
<dd>Original release</dd>
</dl>
<h2 id="license">License</h2>
<p>jQuery Masonry is dual-licensed under <a href="http://www.gnu.org/licenses/gpl.html">GPL</a> and <a href="http://en.wikipedia.org/wiki/MIT_License">MIT</a>, just like <a href="http://docs.jquery.com/License">jQuery itself.</a> You can use it for both personal and commercial applications.</p>
</div>
<p id="footer">
jQuery Masonry by <a href="http://desandro.com/">David DeSandro</a>
</p>
</body>
</html>
Jump to Line
Something went wrong with that request. Please try again.