Book Image

Instant Galleria How-to

By : Nathan Van Gheem
Book Image

Instant Galleria How-to

By: Nathan Van Gheem

Overview of this book

Providing beautiful and usable galleries is an important aspect of the Web today. A gallery solution needs to integrate into your web application easily and seamlessly. Users expect mobile sites that function on par with their desktop counterparts‚Äîespecially for image galleries. In order to accomplish these tasks, you need to use a JavaScript (not Flash) that is extensible and scales to mobile devices. "Instant Galleria How-to" will teach you how to use Galleria in advanced scenarios to become an expert and integrate Galleria into your next project using themes and plugins to accomplish any task. This book teaches you how to use and create themes and plugins,  using the Galleria API through a series of recipes that include a plethora of code examples. You'll be taken through detailed instructions on the usage of JavaScript to customize Galleria. You will create your own theme (mobile friendly), plugin, and learn all the configuration options of Galleria. You'll learn how to customize Galleria by using its extensive API, optimize Galleria, integrate with Google Analytics, create tests for your customization, and integrate into your web application. You'll become an expert user of the Galleria framework, which will enable you to deploy beautiful, mobile friendly galleries for your next web project.
Table of Contents (7 chapters)

Triggering Galleria (Advanced)


This recipe discusses loading Galleria and changing images from different webpage events.

Getting ready

Sometimes it's required to delay loading Galleria until sometime after a page is rendered. Additionally, you may want to trigger displaying a specific image in the gallery with an event outside the gallery. With this recipe, we'll configure Galleria to be loaded only when a user decides to view the gallery and images to be shown when a user performs a specific action outside of the gallery.

To get us started, create a file named trigger-exampe.html in the development directory we've been working in with the following structure:

<!DOCTYPE html>
<html lang="en">
  <head>
    <script src="http://code.jquery.com/jquery.min.js">
    </script>
    <script src="../galleria-1.2.8.min.js"></script>
    <script>
      /* Galleria initialization */
    </script>
  </head>
  <body>
    <div id="galleria"
         style="height: 500px; display: none">
      <!-- supply images here -->
    </div>
    <a id="show-gallery">Show Gallery</a> | 
    <a id="show-image">Show Image 2</a>
</body>
</html>

Make sure to fill in the image HTML structure to test with. We'll fill in the Galleria JavaScript initialization code in the following example.

How to do it...

To accomplish this, we'll add event handlers for click events on the anchor tags to "Show Gallery" and "Show Image 2".

<script>
  $(document).ready(function(){
    /* when the element with the id "show-gallery" is clicked,
       create the gallery. */
    $('#show-gallery').click(function(e){
      $(this).remove();
      $('#galleria').show();
      Galleria.loadTheme(
        '../themes/classic/galleria.classic.min.js');
      Galleria.run('#galleria');
      /* prevent the default link behaviour */
      e.preventDefault();
    });
    $('#show-image').click(function(e){
      var galleria = Galleria.get(0);
      if(galleria === undefined){
        alert('Galleria not initialized yet.');
      }else{
        galleria.show(1);
      }
      e.preventDefault();
    });
  });
</script>

Here, we register click event handlers for each of the anchor tags we added to the document. Rendering the gallery inside a click event is identical to registering as we have been doing throughout the book. Here, we're just doing the same inside the click event.

To show an image on a click event outside the gallery, we use Galleria.get to retrieve the current active Galleria instance active on the page. Then we use the galleria.show method to show the second image of the gallery.

How it works...

Galleria initialization is done with JavaScript so that it can be initialized in any number of ways. In our example, it was just a simple click event; however, any number of scenarios can be devised to destroy existing gallery instances and recreate new instances with events.