Book Image

Instant jQuery Masonry How-to

By : Kyle David Taylor
Book Image

Instant jQuery Masonry How-to

By: Kyle David Taylor

Overview of this book

jQuery Masonry is a revolutionary new plugin that enables dynamic layouts on any website. This powerful tool shuffles and rearranges elements to fit any screen size which will ensure your website keeps up with one of the biggest trends of the year, Responsive Web Design.Instant jQuery Masonry How-to will allow you to start building fully responsive layouts for portfolios, galleries, blog pages, and more. Gain a complete understanding of the workings of Masonry, which will allow you to integrate this unique tool into your existing and future projects.The hands-on guide will take you from the basics of jQuery Masonry to utilizing its power within Wordpress and Drupal. You will learn the basics such as implementing single and multi-column layouts, before developing more complex solutions for various media types, fluid layouts, and animating transitions. With the widespread adoption of responsive web design, this book will bring your blog, portfolio, or any project you are working on up to speed.
Table of Contents (7 chapters)

Adding media for multi-width columns (Intermediate)


Using Masonry with images and media is relatively simple, but it is not that straightforward when it comes to integrating media with multi-width columns. To implement integration with multiple widths, we need to look back at the multi-column technique and apply the same principles.

Getting ready

In the code bundle for this book, open the 3-images-loaded.html file in your preferred text editor to follow along. There is a lot of additional styling used for various types of elements, so I will only be going over what directly affects the use of Masonry.

How to do it...

  1. We only need to set the width of the various column classes we will be using with CSS.

    <style>
      .masonry-item {
        background: #DEDEDE;
        border-radius: 5px;
        margin: 5px;
        padding: 10px;
        float: left;
      }
      .masonry-item img {
        display: block;
        height: auto;
        max-width: 100%;
        margin: 0 auto;
      }
      .column-1 {
        width: 170px;
      }
      .column-2 {
        width: 370px;
      }
      .column-3 {
        width: 570px;
      }
    </style>
  2. We will set up our HTML code with various types of media, such as images and videos.

    <div id='masonry-container'>
      <div class='masonry-item column-1'>
        <h3>Vehicula Vulputate Sem Ridiculus</h3>
        <p>Maecenas faucibus mollis interdum. Vehicula Vulputate Sem Ridiculus</p>
      </div>
      <div class='masonry-item column-2'>
        <h3>Quam Ridiculus Vestibulum</h3>
        <div class="image-caption">
        <img src="http://lorempixel.com/400/300/technics" alt="random" />
        <span class="caption">Cum sociis natoque penatibus et magnis dis parturient montes, nascetur ridiculus mus.</span>
        </div>
      </div>
      <div class='masonry-item column-2'>
        <blockquote>
        Porta sem malesuada magna mollis euismod. Nullam quis risus eget urna mollis ornare vel eu leo. Curabitur blandit tempus porttitor. Etiam porta sem malesuada magna mollis euismod. </span>
        </blockquote>
      </div>
      <div class='masonry-item column-1'>
        Praesent commodo cursus magna, vel scelerisque nisl consectetur et. Donec id elit non mi porta gravida at eget metus.
      </div>
      <div class='masonry-item column-3'>
        <iframe src="http://player.vimeo.com/video/18743950" width="560" height="320" frameborder="0" webkitAllowFullScreen mozallowfullscreen allowFullScreen></iframe>
      </div>
    </div>

    The Masonry script in this exercise will use imagesLoaded in conjunction with the columnWidth option.

    <script>
       $container = $('#masonry-container');
       $container.imagesLoaded(function() {
          $container.masonry({
             itemSelector : '.masonry-item',
             columnWidth : 200
          });
       });
    </script>

How it works...

Once again, we defined the columnWidth property and created a few CSS classes for the columns; inside the elements, we have various types of media, which were set using imagesLoaded. The result should look similar to the following screenshot: