Quantcast
Channel: computeraxe » plugin
Viewing all articles
Browse latest Browse all 10

Slideshow Loading Problems Solved in jQuery Cycle

$
0
0

When it comes to JavaScript components that act on elements of the page, it is highly important to have those elements in place before the script tries to manipulate them, else things won’t work as intended. So, that begs the question, “What’s the best way to have JavaScript run on my page?” In this example we’ll focus on handling javascript while making slideshows with the jQuery Cycle plugin.

At any rate we want our pages to be seen by the visitor right away. We know if it takes more than a couple of seconds for anything to appear on the screen, the visitor is likely to click away to another site. Remember, text appears quickly when it’s not part of a table. The whole table has to be ready before you’ll see the table, so don’t use tables for design purposes, especially nested tables. Save your tables for presenting data.

A problem with big pages having slideshows is that if the page is not ready when a script tries to manipulate things, the effects may be all wrong. For example, when a script calculates the center position for an image it may get the positioning wrong, especially if all the images in the slideshow are not of the same size.

So, how do we improve our slideshow for the masses? We need to tell the javascript when to do its magic. In this example we’ll use this approach:

  1. Perform initial actions with the .ready() method. Actions in this case are to hide images with the .hide() method until they are called for by the script.
  2. Run the slideshow after the content has loaded with the .load() method.
  3. Use a function to calculate the center for each image.
  4. Apply the function with the before option in jQuery Cycle.

A previous post on centering slideshows with jQuery Cycle put all the javascript inside a .ready() statement. For many scripts this would be just fine, but for the slideshow it resulted in the action starting up before all the images were available.

For this example the slideshow is marked up as a group of images inside a <div> that has been assigned an id for JS targeting, #big_show, and a class for CSS targeting, .photos.

HTML Markup:

one two three four

Hide Images While Page Loads

Because it may take a while to load up all the photos for the slideshow, and everything else on the page, hide the photos except for the first one to start. Use a .ready() statement to hide the photos because we want to do that right away. Put everything in the .ready() method that you want to run first, even before the rest of the page or content has loaded. The .ready() function is a jQuery construct that allows you to bring functionality to the page before all the content has loaded. That way a page with lot of images can be useful even before all the images are visible.

For hiding images use the .ready() function so that this task will occur as soon as possible.

$(document).ready(function() {
    $('#s1 img:gt(0)').hide(); // hides all images with index greater than 0, so it shows the first image only - in one JS/jQuery call

    $('#s2 img').hide(); // hides all images
    $('#s2 img:first').show(); // shows the first image

    $('#s3 img').hide(); // hides all images
    $('#s3 img:eq(0)').show(); // shows the image with index equal to 0
});

Many selectors could be used to target the first element in a series and the example above shows three ways of doing so for three different slideshows, #s1, #s2 and #s3. The outcome of each is the same in that only the first image is shown until the rest of the slideshow is loaded.

Control Slideshow Action After Window Loads

It makes sense to run a slideshow only when all the components for the show are present. To do that run Cycle with a .load() statement so that the slideshow waits to start until all of the images are loaded.

$(window).load(function() {
  $('#big_show').cycle({ 
    fx:     'fadeZoom', 
    timeout:  2000,
    before: onBefore
  });
});

If you have a big slide show with lots of images, you might want to present an animated loading image until the slideshow starts.

Calculate Image Size for Centering in Slideshow

Take a look at the solution provided by “malsup” for users of his Cycle plugin:

function onBefore(curr,next,opts) {
    var $slide = $(next);
    var w = $slide.outerWidth();
    var h = $slide.outerHeight();
    $slide.css({
        marginTop: (400 - h) / 2,
        marginLeft: (300 - w) / 2
    });
};

Basically, what his code is doing is creating a function that will run before any slides are manipulated by Cycle. The purpose of the onBefore function is to calculate the correct margins for images and set the CSS parameters margin-top and margin-left for centering the images.

The onBefore function creates a variable called $slide to hold an array of the parameters of the $(next) element in the slideshow. The variables w and h are set to the width and height of said element via the $slide.outerWidth() and $slide.outerHeight() methods, respectively.

Finally, the margins are calculated from knowing the width and height of the slide container as set in the CSS (in this example 400 px and 300 px) and subtracting the slide’s width (w) and height (h) values, respectively. The margins only need to have half of the total margin value to accommodate both sides of the box, so the difference is divided by 2.

Use Cycle’s Before Option to Apply Centering Function

Using the onBefore function with Cycle’s before option works beautifully with images of different sizes, see line 5 of the .load() method above. Margins are set using the .css() method after they are calculated by the onBefore function.

CSS:

#big_show { 
    height: 400px; 
    width: 300px; 
    text-align: center;  
    background-color: #f3c; 
}
.photos img { 
    margin: 0;
    padding: 4px; 
    border: 1px solid #ccc; 
    background-color: #eee; 
    max-width: 290px; 
    max-height: 390px; 
}

You have to specify some CSS to get this thing to work right, namely set the width and height on the slide container and slides themselves.

Setting the max-width and max-height for the images helps to keep them inside of the container. Note that adding up the padding and border for both sides of the box is 10 px of the slideshow box that can’t be used to show an image. Therefore, the maximum image dimensions are the overall width or height minus 10 px. By accounting for the padding and border sizes a large image won’t overflow its container.

Any comments on this improved slideshow using Cycle plugin with jQuery?


Viewing all articles
Browse latest Browse all 10

Trending Articles