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)

Creating your own theme (Medium)


Creating your own theme will allow you to customize Galleria in a very simple format. Here we will also introduce the structure for creating a basic theme.

Getting ready

In order to keep all the theme related files together, create a new folder inside the development folder named themes. Within the new themes folder, create another folder named basic. After this is done, our folder structure should be identical to the following screenshot:

Lastly, create empty galleria.basic.js, galleria.basic.css, and galleria.basic-example.html files inside the basic folder that was just created. Then, copy the contents of the myfirstgallery.html file and insert the text into the galleria.basic-example.html file. These are the files that we will be customizing in this recipe.

How to do it...

  1. First, let's create the skeleton for our basic galleria theme in the galleria.basic.js file:

    (function($) {
    Galleria.addTheme({
        name: 'basic',
        author: 'Galleria How-to',
        css: 'galleria.basic.css',
        defaults: { transition: 'slide' },
        init: function(options) {
        }
    });
    }(jQuery));

    The addTheme function registers the theme with the provided settings to Galleria. The .css file specified with the css option is automatically loaded here also. The init anonymous function is empty right now but this is where we would put the custom JavaScript functionality.

  2. Next, we'll go on to styling our gallery. At its basic level, this involves placing tags into the DOM so that they can be positioned on the page.

    The major components of the gallery that need to be styled are:

    • #galleria-loader

    • .galleria-info

    • .galleria-stage

    • .galleria-thumbnails-container

    • .galleria-counter

    • .galleria-image-nav

    Now, let's go through the contents of the CSS file that provides very basic styles for our gallery.

    #galleria{ height: 550px; }
    

    The preceding code sets the height of the gallery. This is required by the gallery to load and scale all the elements for the gallery correctly.

    #galleria-loader{height:1px!important}
    

    The preceding code provides the definition of the gallery loader container that is required for Galleria to load properly.

    .galleria-stage { position: absolute; top: 0;bottom: 60px; left: 10px; right: 10px;width: 500px; float: right; max-height: 550px;}
    

    .galleria-stage is the container for the images, loader, counter, and gallery navigation buttons.

    .galleria-thumbnails-container { top: 0;position: absolute; left: 10px; right: 10px;z-index: 2; width: 500px; }

    .galleria-thumbnails-container is the container for the thumbnail carousel. Here, we're placing it on top of the gallery.

    .galleria-thumbnails .galleria-image { height: 40px;
        width: 60px; margin: 0 5px 0 0;border: 1px solid #000;
        float: left; cursor: pointer; }

    With this rule we're styling the images in the thumbnail carousel. We make sure to float left here so that the images are displayed one after another.

    .galleria-info{ position: absolute; top: 48px; opacity: 0.7;
      background-color: #F1F1F1; z-index: 4; left: 10px;
      width: 500px; }

    With this rule, we target the tag that places the title and description of the images as they're browsed. In this theme, we're placing it just below the thumbnail carousel.

    .galleria-counter { position: absolute; bottom: 30px;
      left: 10px; }

    .galleria-counter is used to display which image the user is currently viewing out of the total number of images in the gallery.

    .galleria-image-nav { position: absolute; top: 50%;
       margin-top: -62px; width: 500px; height: 62px;left: 0; }

    .galleria-image-nav places the navigation buttons on the gallery so that we can manually move forward and backward in the gallery. In this case, we're just trying to place the buttons in the middle of the gallery.

    .galleria-image-nav-left,
    .galleria-image-nav-right { opacity: .5; cursor: pointer; 
       width: 35px; height: 124px; position: absolute;
       left: 0; z-index: 2; background-color: gray; }
    .galleria-image-nav-right { left: auto; right: 0;
       z-index: 2;}
    .galleria-image-nav-left:hover,
    .galleria-image-nav-right:hover { opacity: .9; }

    This preceding set of rules places right and left navigation buttons on the edges of the gallery.

  3. Finally, we need to modify the inline JavaScript code in our galleria.basic-example.html file to call our new theme, using the following code:

      <script src="http://code.jquery.com/jquery.min.js"></script>
      <script src="../../../galleria-1.2.8.min.js"></script>
      <script>
     $(document).ready(function(){
       Galleria.loadTheme('galleria.basic.js');
          Galleria.run('#galleria');
     });
      </script>
  4. Additionally, you'll need to change the image references in the galleria-basic-example.html file to work with the images where they are stored. If following along with the provided code and examples, the images should be placed two folders above the theme file:

    <a href="../../images/image1-1600px.jpg">
      <img title="Image 1"
           alt="Image 1 description"
           src="../../images/image1-200px.jpg" />
    </a>

    This is essentially identical to our previous Galleria bootstrapping with the simple exception that we're loading the new galleria.basic.js theme instead of the classic theme in the following screenshot:

Once finished, the theme should be rendering a screen like the following screenshot:

How it works...

Galleria loads the theme JavaScript file we specified in the Galleria.loadTheme function. Galleria then looks at the CSS file specified in the theme JavaScript file to load the stylesheets. The markup is generated by Galleria and then all the styling is done by us.

For the sake of brevity

The basic gallery is very plain and obviously doesn't look very appealing. This is so we don't get bombarded with too many CSS rules to digest. We're going to work with the most barebones examples possible. The objective is to understand how to use and extend Galleria, not how to style it beautifully.

However, while working with this theme, feel free to use additional styles to provide a custom look and feel.