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)

Your first gallery (Simple)


In this recipe, we're going to go through the basics of creating a Galleria gallery. HTML structure and Galleria JavaScript are topics that will be covered in this recipe.

Getting ready

Assemble together a group of images that you'll use while working with Galleria and place them into your Galleria development image source directory (galleria/development/images).

How to do it...

  1. Create a myfirstgallery.html file inside the development folder.

  2. Start off with the usual HTML5 doctype structure.

  3. Inside the head section include the JavaScript dependencies, jquery.js and galleria.js. In both the cases, we'll use the following minified versions:

    <!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>
         /* end of Javascript file includes */

    Make sure the relative path used to reference galleria.js is correct for the folder structure being used.

    Tip

    Downloading the example code

    You can download the example code files for all Packt books you have purchased from your account at http://www.PacktPub.com. If you purchased this book elsewhere, you can visit http://www.PacktPub.com/support and register to have the files e-mailed directly to you.

  4. Next, we'll need to provide the following code to initialize the gallery:

    /* end of Javascript file includes */
    <script>
    $(document).ready(function(){
      Galleria.loadTheme(
        '../themes/classic/galleria.classic.min.js');
      Galleria.run('#galleria');
    });
    </script>
    </head>
    /* end of head tag */
    • Here, $(document).ready is a jQuery construct used to run the code inside the anonymous function after the document is ready. This is important because we need to load the gallery after all the images are loaded in the DOM.

    • The Galleria.loadTheme function loads the theme file. It takes only one parameter, the path of the JavaScript file.

    • Finally, the Galleria.run function is used to wire a gallery up via a CSS selector. In this case, #galleria refers to a tag with the ID of "galleria" that we'll define later.

  5. Next, we'll add references to the images we want to provide in the gallery. The standard way to do this, demonstrated here, is to provide them through an HTML structure. The basic structure is to provide the full size image as the href attribute of an anchor tag. The thumbnail image is defined via an img tag. If you have not generated thumbnails, just use your full size image for the thumbnail.

  6. Inside the body tag, we'll provide the following HTML code to reference the images for our gallery:

    <body>
     <div id="galleria" style="height: 500px;">
       <a href="images/image1-1600px.jpg">
         <img data-title="Image 1"
              data-description="Image 1 description"
              src="images/image1-200px.jpg" />
       </a>
       <!-- Additional images in same HTML format
            can be provided below -->
     </div>
    </body>
    • The id="galleria" attribute and value refers to what we were using with the Galleria.run method to instruct Galleria where to look for the images

    • The style="height: 500px;" attribute and value gives a height to the gallery, which is required for Galleria to function

    • The data-title attribute is where we can give our image a title that the theme will use

    • Finally, the data-description attribute, similar to the title attribute, gives the image a description to be shown in the gallery

How it works...

Galleria searches the div tag with the id "galleria" to find images to load the gallery. Galleria then calls the theme JavaScript initialization code to generate additional HTML and JavaScript events that set up the gallery further. Essentially, galleria.js provides the basic gallery components and the theme uses these components to further customize the look and feel of the gallery.

Defining a height

It is important to define a height for all Galleria galleries. If no height is defined in CSS or a style tag, the gallery will fail to load.

Data attributes

In this example, we use data attributes, which are attributes that start with "data-", to define the title and description of the image. We could have just as easily specified title and alt attributes that would also populate the title and description values. Either way works the same.