I use jQuery cycle for lots of freelance development projects, but ran in to problems recently when building a photography site. My client was uploading around 40 high-resolution images in to a slideshow, which caused two problems:

  1. Elements using CSS background images loaded after all the img elements - meaning the navigation menu would only load after all the images had finished loading.
  2. The page took a seriously long time to finish loading.

So, I decided to fix the problem by loading each image on the fly - as soon as the cycle transition completes, it loads in the next image.

The HTML structure is as follows:

              
<ul id="slideshow">
    <li id="slide-1" src="/images/image1.jpg">
        <img src="/images/image1.jpg" />
    </li>
    <li id="slide-2" src="/images/image2.jpg"></li>
    <li id="slide-3" src="/images/image3.jpg"></li>
</ul>

            

Each img is surrounded by a list item (li), but notice how only one img is included in the HTML - all other li’s simply have a src attribute set.

The CSS simply sets the height, width and overflow:hidden (so only one image is visible, even if the client uploads images smaller than the ul dimensions:

              
#slideshow { width: 950px; height: 480px; overflow: hidden; }
#slideshow li { width: 100%; height: 100%; display: block; }

            

And finally the jQuery Cycle code:

              
$('#slideshow').cycle({ 
    delay: -3000, 
    timeout: 5000, 
    fx: 'fade', 
    prev: '#prev', 
    next: '#next', 
    pause: true, 
    pauseOnPagerHover: true,
    after: function(currSlideElement, nextSlideElement) {

        // Grab the ID of the next li
        var next = $(nextSlideElement).next();
        next = $('#' + next.attr('id'));

        // if the image has not already been loaded
        if (next.find('img').length == 0) {

            // create the img element and insert it in to the li
            var img = $('<img />').attr('src', next.attr('imgsrc'));
            next.append( img );
        }
    }
});

            
« back

Related posts

IE9 Hell(vetica)

On Tuesday I eagerly downloaded the new version of Internet Explorer, hoping that this version would be a turning point for the browser. I hoped this might be the end of IE’s inconsistencies, the start of Microsoft conforming to web standards, and the death of IE-specific style sheets. …

19th March 2011

read more »

Should websites look identical in every browser?

Throughout the course of my web development career I have debated this point with project managers, designers, clients, and even other web developers, who have argued that websites should look identical in every browser. There are several reasons that is impossible …

23rd February 2011

read more »

Upper case HTML elements in Drupal with TinyMCE

I’ve just spent a while trying to fix a really annoying little problem using TinyMCE in Drupal 6, so thought I’d share the solution. My client was entering content into a TinyMCE text field and had complained that the spacing between paragraphs and list items was too big. …

7th January 2011

read more »