2010-05-20 98 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