views:

964

answers:

4

Hey guys, I'm running jQuery Cycle for an image gallery. View the link: Here

My problem is that the images are getting squished when viewed in firefox. The problem disappears when I re-load the page. This leads me to believe that the Javascript is triggering before all the images are loaded (usually the first image works fine and the rest are squished.)

A hard re-fresh reproduces the problem.

I've wrapped everything in a $(document).ready(function(){ }); but it still happens.

Additional Info: If I specify the width and height of the image, everything works fine. However there are hundreds of images all at different sizes..

I'm pretty frustrated with this problem. Any ideas/help is greatly appreciated!

Here is my code:

$(document).ready(function(){
//function onBefore(curr,next,opts) {
//    var $slide = jQuery(next);
//    var w = $slide.outerWidth();
//    var h = $slide.outerHeight();
//    $slide.css({
//        marginTop: (482 - h) / 2,
//        marginLeft: (560 - w) / 2
//    });
//};

// Decare the function that center the images...
function onBefore(curr,next,opts) {
    var $slide = jQuery(next);
    var w = $slide.outerWidth();
    var h = $slide.outerHeight();
    $slide.css({
        marginTop: (480 - h) / 2,
        marginLeft: (560 - w) / 2
    });
};


$(document).ready(function() {
    $('#slideshow').cycle({
     fx:     'fade', 
    next:   '#next', 
    pause: 0,
    speed: 500,
    before: onBefore,
    prev:   '#prev',
    pause: '#pause',
    pager:  '.thumbs',
    pagerClick:function(zeroBasedSlideIndex, slideElement) {$(slideElement).find('div.cover').hide();},
    pagerAnchorBuilder: function(idx, slide) {
                        var src = $('img',slide).attr('src');
         //Change height of thumbnail here
                         return '<li><a href="#"><img src="' + slide.src + '" height="90" /></a></li>'; 

                } 
    });});});
+1  A: 

I had the same problem when working on a site several months ago (linked below). If you're starting cycle in $(document).ready(), here's what happens when a client browses to your page:

1) The client's browser sends a request for each img element. Those requests take variable amounts of time to fulfill.

2) Before the image requests are completed, cycle starts. Cycle works by hiding all but the first image in the slide show: it sets visibility:hidden and display:none on each of its images.

The problem is that Firefox fixes the img element's size once and for all at the point the display style is set to none. So if the image hasn't finished loading, its height and width style attributes are small (I'm not sure exactly what they correspond to - perhaps the size of Firefox's image placeholder). When cycle shows the image by setting its style attribute to display:block, it uses whatever dimensions it had at the time it was hidden.

I solved this by changing my code so that it doesn't start the cycle plugin until all the images are finished loading. To do that, I initialize a counter variable to the number of images I'm cycling, then bind a load event to each image like this:

var imagesRemaining = 12; // 12 is just the number of images in the slideshow div

$(document).ready(function() {
    $('#slideshow > img').bind('load', function(e) {
        imagesRemaining = imagesRemaining - 1;
        if (imagesRemaining == 0) {
            // I'm doing some other stuff when initializing cycle
            startCycle();
            // My images all start with visibility:hidden so they don't show
            // before cycle hides them in a 'stack', so ...
            $('#slideshow > img').css('visibility', 'visible');
        }
    });
});

function onBefore(curr, next, opts) { // Your code here ... }

function startCycle() {
    $('#slideshow').cycle({ ... // your initialization here });
}

You can see it in action by viewing the galleries on this site in Firefox. I'm building the gallery pages dynamically, so it's structured a bit differently than your page, but you can see more details if you poke around with Firebug.

Jeff Sternal
I'm somewhat new to Jquery, how would I implement this?
I've updated the example to show it a little more clearly.
Jeff Sternal
+1  A: 

I'd also like to add that it seems adding a width and height attribute solves this problem.

Joshua Cody
+1  A: 

Thanks Joshua - adding the width and height attributes on the div does solve the problem on Firefox 3.5.6 as follows:

#image-rotator {
  float:right;
  width:310px;
  height:215px;
  margin: 3px 7px 5px 6px;
  padding:0;
}

Eoin

Web Design Kilkenny
A: 

If you're using a database to populate the slideshow you could try accessing the image dimensions from the image itself.

For example, using django you can use width="{{ xxx.image.width }}px" and height="{{ xxx.image.height }}px" in your img tag.

Timbadu