2009-11-03 67 views
0

試圖讓我的翻轉在翻轉時更改src。這工作正常,但有一個錯誤。點擊縮略圖後,src有時可能包含錯誤的src(即使在鼠標懸停狀態下仍然保持翻轉狀態)。 。要找到這個錯誤,點擊幾個縮略圖並點擊幾下鼠標,你會看到翻轉src保留已經被點擊過的src。演示不再可用,對不起!jquery翻轉混亂

jQuery的 -

function image_gallery(){ 

if ($('ul.thumbs').length > 0) { 
    $('.gallery').each(function(){ 
     $('ul.thumbs li img:gt(0)').addClass('unselected'); 
     $('ul.thumbs li img:eq(0)').addClass('selected'); 

     function mouse_overs() { 
      var unselected = $('li img.unselected'); 
      unselected.hover(function(){ 
        var thumb = $(this); 
        thumb.attr('src',thumb.attr('src') 
          .replace(/([^.]*\d)\.(.*)/, "$1r.$2")); 
       }, function(){ 
         var thumb = $(this); 
        thumb.each(function(){ 
         $(this).attr('src',$(this) 
          .attr('src').replace('r.jpg','.jpg')); 
        }); 
      }); 
     }; 
     mouse_overs(); 
     var img_main = $(this).find('img.main:first'); 
     $(this).find('ul.thumbs img').each(function(){ 
      $(this).click(function(){ 
       var thumb = $(this); 
       var src = thumb.attr('src'); 
       if (src.indexOf('r.jpg') == -1) { 
        $(this).attr('src',thumb.attr('src') 
           .replace(/([^.]*)\.(.*)/, "$1r.$2")); 
       } 
       var selected = $('ul.thumbs li img.selected'); 

       // previous img remove r.jpg 
       selected.attr('src',selected.attr('src') 
            .replace('r.jpg','.jpg')); 
        selected.removeClass('selected'); 
       selected.addClass('unselected'); 

       //current thumb add class "selected", remove "unselected" 
       thumb.addClass('selected'); 
       thumb.removeClass('unselected'); 
       mouse_overs(); 
       var rel = $(this).parent('a').attr('rel'); 
       img_main.fadeOut(500, function(){ 
        img_main.attr('src', rel); 
        img_main.fadeIn('slow'); 
       }); 

       thumb.mouseout(function(){ 
        var src = $(this).attr('src'); 
        if (src.indexOf('r.jpg') == -1) { 
         $(this).attr('src',thumb.attr('src') 
             .replace(/([^.]*)\.(.*)/, "$1r.$2")); 
        } 
        else return false; 
       }); 
      }); 
}); 
    }); 
    } 
} 

的HTML:

<div class="gallery"> 
<img class="main" src="images/gallery/yes-campaign/NL1.jpg"/> 
<ul class="thumbs"> 
     <li><a rel="images/gallery/yes-campaign/NL1.jpg"><img src="images/thumbs/yes-campaign/NL-1r.jpg"/></a></li> 
     <li><a rel="images/gallery/yes-campaign/NL2.jpg"><img src="images/thumbs/yes-campaign/NL-2.jpg"/></a></li> 
     <li><a rel="images/gallery/yes-campaign/NL3.jpg"><img src="images/thumbs/yes-campaign/NL-3.jpg"/></a></li> 
     <li><a rel="images/gallery/yes-campaign/NL4.jpg"><img src="images/thumbs/yes-campaign/NL-4.jpg"/></a></li> 
    </ul> 
</div> 

這個HTML在整個頁面重複不同的時間。翻轉狀態爲NL1r.jpg,NL2r.jpg等。圖像按文件夾排列,所有圖像文件名使用相同的命名約定。

回答

1

我可以建議下面的代碼而不是你的嗎?

$(function gallery(){ 

     var transparency = .5; 
     var selectedClassName = 'selected'; 
     var imageFadeSpeed = 'fast'; 

     $('.gallery').each(function(i, gallery) { 
      var $gallery = $(gallery); 

      var $main = $gallery.find('.main'); 

      $gallery.find('.thumbs a') 

       // image preloader 
       .each(function(){ 
        var tempImg = $('<img src="'+ $(this).attr('rel') +'" width="'+ $main.width() +'" />').appendTo('body').hide(); 
       }) 

       .hover(function() { 
        if ($(this).is('.'+selectedClassName)) 
         return; 
        $(this).children().css('opacity', 1); 
       }, function() { 
        if ($(this).is('.'+selectedClassName)) 
         return; 
        $(this).children().css('opacity', transparency); 
       }) 

       .click(function(ev) { 
        ev.preventDefault(); 

        var self = $(this); 

        $main.fadeOut(imageFadeSpeed, function() {      
         var tempImg = $('<img src="'+ self.attr('rel') +'" width="'+ $main.width() +'" />').appendTo('body'); 
         var newHeight = tempImg.height(); 
         tempImg.remove(); 

         $(this) 
          .attr('src', self.attr('rel')) 
          .height(newHeight); 

         $(this).fadeIn(imageFadeSpeed); 
        }); 

        $gallery.find('.'+selectedClassName) 
         .removeClass(selectedClassName) 
         .children() 
         .css('opacity', transparency); 

        self 
         .addClass(selectedClassName) 
         .children() 
         .css('opacity', 1); 
        return; 
       }) 

       .children() 
       .css('opacity', transparency) 
       .end() 

       .filter(':first') 
       .addClass(selectedClassName) 
       .children() 
       .css('opacity', 1); 
     }); 
}); 

我已經更換了懸停圖像交換與透明度改變,這減少了服務器的負載,但你可以很容易地與SRC交換替換這些。您將需要使用帶有「r.jpg」的圖像才能結束。

我也拉出了一些配置變量,所以你可以玩一些東西。

+0

真棒,非常感謝,從中學到了很多東西。 – 2009-11-03 23:43:31

+0

演示現在使用上面的代碼 – 2009-11-04 01:09:38

+0

目前,您的圖像上有浮動左側的CSS位置,請確保向周圍的LI元素添加「overflow:hidden」。 – 2009-11-04 12:48:48

0

雖然我認爲馬特在腳本方面做了很棒的工作(+1),但我仍然推薦使用GalleryView pluginDemo here)。


我還在學習一樣,所以我可能不是我的假設是否正確下方,隨時糾正我,如果我錯了。但在再次看看您的代碼後,我想添加以下評論:

  • 您的mouse_overs函數似乎可以修復最初的「未選中」類。最好的方法是使用jQuery的「live」事件處理程序。通過這種方式,當您將選擇的類更改爲未選中時,反之亦然,實時事件將更新(注意:當前版本的jquery不支持懸停,因此您必須使用鼠標懸停和鼠標懸停)。它也似乎懸停的鼠標懸停部分被稱爲3次,這也可能是相關的。
  • 替換函數中使用的正則表達式無法正常工作。點擊縮略圖切換圖像後,我注意到,只要你mouseout(這是從thumb.mouseout函數),URL開始添加r的單詞靜態...的結尾約10個鼠標後,我結束了這個URL「http://staticrrrrrrrrrr.yourdomain.com/someimage.jpg」。我不太瞭解我的正則表達式,所以我無法幫你解決這個問題。
  • 而不是修改網址,它會更容易做,因爲馬特建議並使用不透明度,但如果你想使用不同的圖像,我會將URL存儲在img的rel屬性中,只需將其切換,那麼對於URL發生的問題就不那麼重要了。
+0

感謝信心投票fudgey – 2009-11-03 23:13:15

+0

雖然我的代碼可能僅僅是抓住jQuery和js的表面,通過手工編碼,我是一個離開我的目標的錯誤,我在這個過程中學到了很多東西。從看到M.S的榜樣,我學到了更多的財富。如果我使用了一個插件,我個人將不會學習任何東西,我會盡量縮小版本的腳本,並花時間嘗試將其轉化爲客戶的需求,而這些需求不會產生效率,而且具有破壞性,我會試圖忽略該插件的功能,我敢肯定插件岩石,但在這種情況下,它看起來矯枉過正我的小要求。 – 2009-11-03 23:42:52

+0

我很欣賞你想學習,我自己也從這裏的有經驗的人那裏學到很多東西,我只是建議讓你的生活更輕鬆:P ...我更深入地瞭解了你的代碼,並給我添加了一些評論在上面發帖。順便說一句,螢火蟲是你的朋友:) – Mottie 2009-11-04 07:38:47