2010-05-20 93 views
2

〜在我的文章頁面(http://www.adrtimes.squarespace.com/articles)我的每個條目都是以翻轉時變化的圖像開始的。這工作正常。問題jQuery swapImage();

但是,在我的主頁(http://www.adrtimes.squarespace.com)中,我加載了最近兩篇未歸類爲視頻的文章以及最近被標記爲視頻的兩篇文章。我正在使用jQuery load()從文章頁面獲取內容。除了主頁上的swapImage滾動條之外,一切正常。我試圖將swapImage函數作爲回調函數來使用,但只有一個組或另一個組合會正確地滾動,而不是兩組內容。我不確定是什麼問題!

下面是代碼:

<script type="text/javascript"> 
<!-- 

$(function(){ 

$.swapImage(".swapImage"); 

/* Homepage */ 
// load recent articles 
$("#LoadRecentArticles").load("/articles/ .list-journal-entry-wrapper .journal-entry-wrapper:not(.category-video)", function(){ 
    //callback... 
    $("#LoadRecentArticles .journal-entry-wrapper").wrapAll('<div class="list-journal-entry-wrapper" />'); 
    $("#LoadRecentArticles .journal-entry-wrapper:gt(1)").remove(); 
    // modify Read More tag 
    $('.journal-read-more-tag a:contains(Click to read more ...)').each(function(){ 
     var str = $(this).html(); 
     $(this).html(str.replace('Click to read more ...','Continue reading—')); 
    }); 
    $.swapImage(".swapImage"); 
}); 

// load recent videos 
$("#LoadRecentVideos").load("/articles/category/video .list-journal-entry-wrapper", function(){ 
    //callback... 
    $("#LoadRecentVideos .journal-entry-wrapper:gt(1)").remove(); 
    $('<div class="VideoTag">—video</div>').insertBefore("#LoadRecentVideos .category-video .body img"); 
    // modify Read More tag 
    $('.journal-read-more-tag a:contains(Click to read more ...)').each(function(){ 
     var str = $(this).html(); 
     $(this).html(str.replace('Click to read more ...','Continue reading—')); 
    }); 
    $.swapImage(".swapImage"); 
}); 


}); 
--> 
</script> 

這裏是HTML的在一篇文章條目中的圖像樣本:

<a href="/articles/2010/5/6/article-title-goes-here.html"><img class="swapImage {src: '/storage/post-images/Sample2_color.jpg'}" src="/storage/post-images/Sample2_bw.jpg" /></a> 
+0

我不知道該插件的任何(其他比我看了看剛纔),但會不會是它會很困惑,因爲你使用的是完全相同的圖像' src'兩套掉期?你可以嘗試給不想交換的圖像指定一個獨特的名稱,看看是否有幫助? (基本上用新名稱複製圖像。)我可以從Firebug中突出顯示的代碼中看到,當您懸停時,圖像的「src」會發生變化。但它似乎只是給它兩個不起作用的原始'src'。 – user113716 2010-05-20 02:01:40

+0

看來,文章頁面(http://www.adrtimes.squarespace.com)會有相同的問題,如果它是由使用相同的圖像造成的......但我試過了,並設置它,使所有的圖片加載進入主頁都有獨特的名稱 - 但沒有工作。 :( – VUELA 2010-05-20 02:15:52

+0

出於好奇,這是'{src:'/storage/post-images/Sample2_color.jpg'}'由你或插件添加嗎? – user113716 2010-05-20 02:20:22

回答

1

我的道歉,如果你想要的東西,這不是,但做自己的交換真的很簡單。

我不知道你的選擇應該是什麼,但這會搶了圖像時,你mouseentersrc,它從_bw.jpg改爲_color.jpg,以及回來時,你mouseleave

HTML:

<img class='imageToSwap' src="http://adrtimes.squarespace.com/storage/post-images/Sample2_bw.jpg"> 

的jQuery:

$('.imageToSwap').hover(function() { 
     var $th = $(this); 
     var src = $(this).attr('src'); 
     var newSrc = src.replace(/_bw.jpg/, '_color.jpg'); 
     $th.attr('src', newSrc) 
    }, 
    function() { 
     var $th = $(this); 
     var src = $(this).attr('src'); 
     var newSrc = src.replace(/_color.jpg/, '_bw.jpg'); 
     $th.attr('src', newSrc) 
    }); 

編輯:用活

版本()

充滿希望這將會爲你做。 jQuery的live()函數將管理頁面加載後動態添加到DOM的元素的事件。

試一試,讓我知道結果如何。

$('.imageToSwap').live('mouseover mouseout', function(event) { 
    var $th = $(this); 
    var src = $(this).attr('src'); 
    if (event.type == 'mouseover') { 
     var newSrc = src.replace(/_bw.jpg/, '_color.jpg'); 
     $th.attr('src', newSrc) 
    } else { 
     var newSrc = src.replace(/_color.jpg/, '_bw.jpg'); 
     $th.attr('src', newSrc) 
    } 
}); 
+0

這是一個很好的片段!謝謝!我切換到這個版本,並再次有同樣的問題...翻轉工作正常在原始頁面(http://www.adrtimes.squarespace.com/articles),但不加載時使用jquery負載()在主頁上(http://www.adrtimes.squarespace.com)。當內容加載到主頁上時,我不得不重新啓動任何需要應用於內容的jquery,而且它似乎沒有按照我嘗試的方式工作。 – VUELA 2010-05-20 02:59:15

+0

@VUELA - 我會在一分鐘內更新我的答案。 jQuery的'live()'方法可能是要走的路。讓我知道它是否有效。 – user113716 2010-05-20 03:13:23

+0

我正在搜索與jquery load()相關的以前的問題,並碰到這個:http://stackoverflow.com/questions/1539129/figuring-out-when-images-are-loaded-after-they-are-inserted-using -jquery-loadur ....或許在圖像實際完成加載之前應用翻轉腳本? – VUELA 2010-05-20 03:15:39

0

如果您第二次運行$.swapimage(),它會自動禁用它。所以在回調試試這個:

$.swapImage("#LoadRecentVideos .swapImage"); 
+0

我在想這件事,我想你是對的。不檢查這種情況的插件是不值得使用的(在我看來)。 – user113716 2010-05-20 03:37:45

+0

感謝您的提示!如果我再次遇到問題,我會記住這個方法(bug?)。 – VUELA 2010-05-20 19:33:46

+0

我認爲這是有目的的,儘管它似乎沒有記錄。無論哪種方式,我很高興你有你的答案:) – Mottie 2010-05-20 20:27:28