2012-11-17 16 views
2

我正在嘗試創建一個圖片庫。當我點擊一張圖片時,我調整了它的大小,並將其淡入。它在Firefox和Internet Explorer中運行得很好,但它在Chrome中滯後。我試了幾天才解決問題,但找不到答案。我縮小了我認爲的CSS問題。如果我刪除類'resize',resisez圖像適合頁面,它看起來蹩腳,但它工作正常。但問題只發生在大圖像。我試過.load(),但它似乎沒有工作。最荒謬的想法是,如果沒有這個班級,一切都會奏效。jquery淡入Chrome中的滯後

這裏是我的代碼:

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> 

的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; 
} 

的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; 
}; 

我將不勝感激,如果有人能夠幫助我。

按照要求,這裏是項目http://jsfiddle.net/uaD5s/6/的小提琴。應該從一開始就做到這一點,對不起。

+7

你介意建立一個小提琴? http://jsfiddle.net – VIDesignz

+1

我加了小提琴,只是想讓你知道。 –

+0

您的歡迎!很高興我能幫上忙! – VIDesignz

回答

3

隨着劇本的一些調整,我已經找到一種方法,以防止滯後無論圖像的大小。

而是在圖像褪色,我們創建了一個白色的蓋覆蓋該那張圖像

<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> 

然後我們添加這個CSS樣式封面

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

最後,當頂部圖像完全顯示,我們.fadeOut()封面,而不是像最初寫入的那樣在圖像中消失。

本來這

self.displayImage.fadeIn(500); 

現在這不是

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

這裏是一個小提琴看到它在行動......

FIDDLE

,並確保封面被重置,這需要被添加到'重置'功能

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

這裏是它應該加入的功能。

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; 
}; 
+1

謝謝配發,並沒有想過,現在它的偉大工程。 –