Catalin Stefan Cernat
Catalin Stefan Cernat

Reputation: 35

Jquery fadeIn lags in Chrome

I'm trying to make an image gallery. When I click on an image, I resize it and I fade it in. It works just fine in Firefox and Internet Explorer, but it lags in Chrome. I tried for days to solve the problem but can't find the answer. I narrowed the problem the css i think. If i remove the class '.resize', which resisez the images to fit in page, it looks crappy but it works fine. The problem only occurs with big images though. I tried the .load(), but it doesn't seem to work. The most absurd think to me is that without that class everything works.

Here is my code :

HTML :

<!DOCTYPE HTML>
<html lang="en-US">
<head>
    <meta charset="UTF-8">
    <title>Gallery</title>
    <link rel="stylesheet" href="Gallery.css">
</head>
<body>

<div class="gallery">
    <div class="images">
        <ul class="resize">
            <li><img src="img/img1.jpg" alt="image1"></li>
            <li><img src="img/img2.jpg" alt="image2"></li>
            <li><img src="img/img3.jpg" alt="image3"></li>
            <li><img src="img/img4.jpg" alt="image4"></li>
            <li><img src="img/img5.jpg" alt="image5"></li>
            <li><img src="img/img6.jpg" alt="image6"></li>
            <li><img src="img/img7.jpg" alt="image7"></li>
            <li><img src="img/img8.jpg" alt="image8"></li>
        </ul>
    </div>
    <div class="clear"></div>
    <div class="backgroundOpacity"></div>
    <div class="imageBox">
        <img class="displayImage" src="" alt="displayImage">
        <img class="loadingGif" src="img/loading.gif" alt="loadgindGif">
        <img class="closeImage" src="img/closeImage.png" alt="close">
        <p class="imageNumber">1</p>
    </div>
</div>

<script type="text/javascript" src="jquery-1.8.2.js"></script>
<script type="text/javascript" src="Gallery.js"></script>
<script type="text/javascript">
window.onload = function(){
    var content = $('.gallery'),
        gallery = new Gallery(content);
}
</script>

</body>
</html>

The CSS :

*{
    margin: 0;
    padding: 0;
}
/*-------------------------gallery-----------------------*/
.gallery{
    width: 1400px;
    margin: 100px auto 0;
}
/*------------------------------------------------*/
/*-------------------------images-----------------------*/
.images{
    width: inherit;
    height: inherit;
}
.images li{
    list-style-type: none;
    float: left;
    margin: 0 20px 20px 0;
    width: 300px;
    height: 150px;
    cursor: pointer;
    padding: 5px;
    border: solid 1px #CCC;
    -moz-box-shadow: 1px 1px 5px #999;
    -webkit-box-shadow: 1px 1px 5px #999;
    box-shadow: 1px 1px 5px #999;
}
.resize img{
    width: 300px;
    height: 150px;
}
/*------------------------------------------------*/
/*-------------------------imageBox-----------------------*/
.imageBox{
    background-color: white;
    clear: both;
    width: 300px;
    height: 150px;
    display: none;
    position: fixed;
    overflow: visible;
}
.backgroundOpacity{
    background-color: black;
    width: 5000px;
    height: 5000px;
    position: fixed;
    left: 0px;
    top: 0px;
    opacity: 0.7;
    display: none;
}
.loadingGif{
    display: block;
    position: absolute;
}
.displayImage{
    display: none;
}
.closeImage{
    position: absolute;
    top: -10px;
    right: -10px;
    cursor: pointer;
}
.imageNumber{
    position: absolute;
    bottom: 0px;
    left: 0px;
    font-family: sans-serif;
    font-size: 18px;
    font-weight: bold;
    opacity: 0.6;
}
/*------------------------------------------------*/
.clear{
    clear: both;
}

The javascript :

function Gallery (galleryDiv, resizeDuration, freeSpace) {
    this.config = {
        resizeDuration: 600,
        freeSpace: 100
    };

    this.galleryDiv = galleryDiv;

    this.imagesUl = this.galleryDiv.find('.images ul');
    this.images = this.imagesUl.find('img');
    this.backgroundOpacity = this.galleryDiv.find('.backgroundOpacity'); // the background div which when is clicked hides the imageBox
    this.imageBox = this.galleryDiv.find('.imageBox'); // where the images will be displayed when they'r clicked
    this.closeButton = this.imageBox.find('.closeImage'); // top-right x
    this.loadingGif = this.imageBox.find('.loadingGif'); // the animated gif that gives the effect of 'loading'
    this.imageNumber = this.imageBox.find('.imageNumber'); // bottom-left text
    this.displayImage = this.imageBox.find('.displayImage'); // image to be displayed


    this.currentImageIndex; // index of the current image
    this.imagesWidth = new Array(); // the images default widths
    this.imagesHeight = new Array(); // the images default heights
    this.imagesSrc = new Array(); // the location of the images
    this.imagesLength = this.images.length // number of images
    this.imageBoxSize = new Array(); // [0] is width, [1] is height
    this.loadingGifSize = new Array() // [0] is width, [1] is height
    this.canceled; // if loading procress was canceled this variable will alert the show function


    this.freeSpace = freeSpace || this.config.freeSpace; // spcea between imageBox and window
    this.resizeDuration = resizeDuration || this.config.resizeDuration; // duration to resize imageBox

    this.init();
};

Gallery.prototype.init = function () { // puts things in move
    this.getImageAttributes().bind();

    return this;
};

Gallery.prototype.bind = function () { // bind events 
    var self = this;
    this.images.on('click', function () {
        self.currentImageIndex = $(self.images).index( $(this) );
        self.canceled = 0;
        self.showImage();
    });

    $(window).on('resize', function () { // center the imagebox whenever the window is resized
        self.centerImageBox(self.imageBoxSize[0], self.imageBoxSize[1]);
    });

    $(this.closeButton).on('click', function () { // hide the image
        self.hideImage();
    });

    $(this.backgroundOpacity).on('click', function () {
        self.hideImage();
        self.canceled = 1;
    });

    return this;
};

Gallery.prototype.getImageAttributes = function () { // get the default images sizes
    var self = this;

    this.imagesUl.removeClass('resize');
    $.each(this.images, function (index, value) {
        self.imagesWidth[index] = value.width;
        self.imagesHeight[index] = value.height;
        self.imagesSrc[index] = $(value).attr('src');
    });
    this.imagesUl.addClass('resize');

    this.imageBox.show();
    this.loadingGifSize = [this.loadingGif.width(), this.loadingGif.height()];
    this.imageBox.hide();

    return this;
};

Gallery.prototype.showImage = function () { // shows the image when it is clicked
    var self = this,
        index = this.currentImageIndex,
        imageSize = new Array(), // [0] is width, [1] is height,
        imageBoxHeight, imageBoxWidth;
    //show imageBox and resize it
    imageSize = this.resizeImage();
    this.imageBoxSize = [imageSize[0], imageSize[1]]; // captures the current imageBox size
    this.imageNumber.text('Image ' + (index+1) + ' of ' + this.imagesLength); // set bottom-left text
    this.displayImage.attr('src', this.imagesSrc[index]).css({
        'width': imageSize[0],
        'height': imageSize[1]
    });
    this.backgroundOpacity.show();
    this.imageBox.show();
    this.loadingGif.show();

    this.imageBox.animate({
        'width': imageSize[0],
        'height': imageSize[1]
    },{
        duration: self.resizeDuration,
        step: function () {
            // center the image box with every resize;
            imageBoxWidth = self.imageBox.width();
            imageBoxHeight = self.imageBox.height();
            self.centerImageBox(imageBoxWidth, imageBoxHeight, 1);
            // center the loadingGif
            self.loadingGif.css({
                'right': (imageBoxWidth - self.loadingGifSize[0])/2,
                'top': (imageBoxHeight - self.loadingGifSize[1])/2
            });
        },
        complete: function () {
            if(!self.canceled){
                self.closeButton.show();
                self.imageNumber.show();
                self.loadingGif.hide();
                self.displayImage.fadeIn(500);
            }
                else self.hideImage();
        }
    });


    return this;
};

Gallery.prototype.hideImage = function () { // hide the image
    //reset imageBox and other elements atributes
    this.imageBox.css({
        'width': '300px',
        'height': '300px'
    });
    this.imageBox.hide();
    this.backgroundOpacity.hide();
    this.closeButton.hide();
    this.imageNumber.hide();
    this.displayImage.hide();

    return this;
};

Gallery.prototype.resizeImage = function () { // resize the image to the current monitor size
    var index = this.currentImageIndex,
        imageWidth = this.imagesWidth[index], imageHeight = this.imagesHeight[index],
        windowWidth = $(window).width(), windowHeight = $(window).height(),
        ratio = imageWidth / imageHeight;

    while(imageWidth > (windowWidth - this.freeSpace) || imageHeight > (windowHeight-this.freeSpace)){
        if(imageWidth > windowWidth){
            imageWidth = windowWidth - this.freeSpace;
            imageHeight = imageWidth / ratio;
        }
        else{
            imageHeight = windowHeight - this.freeSpace;
            imageWidth = imageHeight * ratio;
        }
    }

    return [imageWidth, imageHeight];
};

Gallery.prototype.centerImageBox = function (width, height, resize) { // if animated parameter is specified, the imageBox will not pe resized
    var windowWidth = $(window).width(),
        windowHeight = $(window).height(),
        index =  this.currentImageIndex,
        imageSize = new Array(), // [0] is width, [1] is height
        imageDefaultWidth = this.imagesWidth[index], imageDefaultHeight = this.imagesHeight[index];

    if(windowWidth < 200 || windowHeight < 200) this.imageBox.hide(); // if the window is too small, hide the imageBox, otherwise it looks silly
        else if(this.backgroundOpacity.css('display') === 'block') this.imageBox.show();
    this.imageBox.css({
        'right': (windowWidth - width)/2, 
        'top': (windowHeight - height)/2
    });
    // resize the image and the imageBox, if the imageBox is bigger than the window, or the image can be bigger
    if(!resize){
        imageSize = this.resizeImage();
        width = imageSize[0];
        height = imageSize[1];
        this.imageBoxSize = [width, height];

        this.imageBox.css({
            'width': width,
            'height': height,
            'right': (windowWidth - width)/2, 
            'top': (windowHeight - height)/2
        });
        this.displayImage.css({
            'width': imageSize[0],
            'height': imageSize[1]
        });
    }

    return this;
};

I would greatly appreciate if someone could help me.

As requested here is a fiddle of the project http://jsfiddle.net/uaD5s/6/ . Should have done this from the beginning, sorry.

Upvotes: 2

Views: 1989

Answers (1)

VIDesignz
VIDesignz

Reputation: 4783

With a little tweaking of the script, I have found a way to prevent the lag no matter the image size.

Instead of fading in the Image, we create a white cover overlay that goes on top of the image

<div class="imageBox">
    <div class='cover'></div> // <<<< Added Cover Overlay
    <img class="displayImage" src="" alt="displayImage">
    <img class="loadingGif" src="img/loading.gif" alt="loadgindGif">
    <img class="closeImage" src="img/closeImage.png" alt="close">
    <p class="imageNumber">1</p>
</div>

Then we add this CSS to style the cover

.cover { 
     position:absolute;
     width:100%; height:100%;
     z-index:10;
     background-color:white;
     display:block;
 }

Finally, when the image is fully shown, we .fadeOut() the cover instead of fading in the image as originally written.

Originally this

 self.displayImage.fadeIn(500);

Now this instead

self.displayImage.show(function(){
   $('.cover').fadeOut(500);
     });

Here is a fiddle to see it in action...

FIDDLE

And to make sure the Cover is reset, this needs to be added to the 'Reset' Function

$('.cover').show();

Here is the function it should be added to.

Gallery.prototype.hideImage = function () { // hide the image
    //reset imageBox and other elements atributes
    this.imageBox.css({
        'width': '300px',
        'height': '300px'
    });
    this.imageBox.hide();
    this.backgroundOpacity.hide();
    this.closeButton.hide();
    this.imageNumber.hide();
    this.displayImage.hide();
    $('.cover').show(); // <<<< Add the Script Here

    return this;
};

Upvotes: 3

Related Questions