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)

Animating using jQuery (Intermediate)


We have already noticed that Masonry is responsive while resizing the browser window and rearranges the elements according to the width of the container. By default, Masonry has built-in support for animation using standard jQuery animations.

Getting ready

In the code bundle for this book, open the 4-jquery-animation.html file in your preferred text editor to follow along.

How to do it...

  1. Start with the standard Masonry CSS code.

    <style>
      .masonry-item {
        background: #FFA500;
        float: left;
        margin: 5px;
        padding: 5px;
        width: 80px;
      }
    </style>
  2. Build the standard Masonry HTML structure and add as many Masonry items as necessary.

    <div id='masonry-container'>
      <div class='masonry-item '>
        Maecenas faucibus mollis interdum.
      </div>
      <div class='masonry-item '>
        Maecenas faucibus mollis interdum. Donec sed odio dui. Nullam quis risus eget urna mollis ornare vel eu leo. Vestibulum id ligula porta felis euismod semper.
      </div>
      <div class='masonry-item '>
        Nullam quis risus eget urna mollis ornare vel eu leo. Cras justo odio, dapibus ac facilisis in, egestas eget quam. Aenean eu leo quam. Pellentesque ornare sem lacinia quam venenatis vestibulum.
      </div>
    </div>
  3. Add the standard Masonry script and then add the isAnimated option and set it to true.

    <script>
      $(function() {
        $('#masonry-container').masonry({
          itemSelector : '.masonry-item',
          isAnimated: true
        });
      });
    </script>

How it works...

By setting the isAnimated option to true, Masonry will automatically animate rearrangements using the built-in jQuery support. Now if we manipulate the width of the container by shrinking the browser window, we can see the animation in action.

The following screenshot was taken while the animation was happening:

There's more…

We can include additional jQuery animation options using the animationOptions option. Some of the options include duration, easing, queue, specialEasing, step, and complete. Additional documentation for these options can be found in the jQuery documentation on the official jQuery website at http://api.jquery.com/animate. Some of these options can also be extended by using additional jQuery plugins.

<script>
  $(function() {
    $('#masonry-container').masonry({
      itemSelector : '.masonry-item',
      isAnimated: true,
      animationOptions: {
        duration: 500,
        easing: 'swing'
      }
    });
  });
</script>