压缩重复的JQuery代码

时间:2010-04-02 21:16:04

标签: jquery consolidation

我有一个页面上有一个大图像,上面有许多缩略图。当您将鼠标悬停在缩略图上时,主图像会更改为刚刚将鼠标滑过的图像。问题是我的缩略图越多,我的代码就越重复。我怎么能减少它?

Jquery代码如下。

<script type="text/javascript">  
    $('#thumb1')
.mouseover(function(){  
$('#big_img').fadeOut('slow', function(){  
$('#big_img').attr('src', '0001.jpg');  
$('#big_img').fadeIn('slow');  
            });  
        });  
    $('#thumb2')  
        .mouseover(function(){  
            $('#big_img').fadeOut('slow', function(){  
                $('#big_img').attr('src', 'p_0002.jpg');  
                $('#big_img').fadeIn('slow');  
            });  
        });  
     $('#thumb3')  
        .mouseover(function(){  
    $('#big_img').fadeOut('slow', function(){  
                $('#big_img').attr('src', '_img/p_0003.jpg');  
                $('#big_img').fadeIn('slow');  
            });  
        });  
     $('#thumb4')  
        .mouseover(function(){  
            $('#big_img').fadeOut('slow', function(){  
                $('#big_img').attr('src', '0004.jpg');  
                $('#big_img').fadeIn('slow');  
            });  
        });  
</script>

#big_img =完整尺寸图片的ID

#thumb1#thumb2#thumb3#thumb4 =缩略图的ID

如果有帮助,页面的主要代码是PHP。

4 个答案:

答案 0 :(得分:3)

$(this)是你的朋友。您还需要开发某种命名法,您可以使用它来匹配您的文件名和您的ID。我通常做的是使用PHP生成HTML,但放入处理文件名的特殊属性:

// PHP GeneratedContent

<?php

while(/* some range */) {
    $i = 1;
    $output .= '<element class="thumb" rel="p_' . $i . '.jpg">';
    $i++;
}

echo $output;
?>

然后我将进行下一步:

$('.thumb').mouseover( function() {
    var link = $(this).attr('rel');

    /* Now that you have the link, just put it into whatever function you need to */
});

Fehays方法肯定有效,但是这样,你就不会有大量不必要的ID,而且你不必进行不必要的参数传输。它将使用类thumb自动传播到DOM中的每个实例。

答案 1 :(得分:2)

你可以写一个我认为的函数。

applyImageFade('#thumb1','0001.jpg');
applyImageFade('#thumb2','p_0002.jpg');
//etc...

function applyImageFade(thumbId, image) {
    $(thumbId).mouseover(function(){
        $('#big_img').fadeOut('slow', function(){
            $('#big_img').attr('src', image);
            $('#big_img').fadeIn('slow');
        });
    });
}

答案 2 :(得分:2)

首先,您应该修改代码,以便每个缩略图都有一个易于查找的“类”。

假设你有

<img id="thumb1" scr=""/>
<img id="thumb2" scr=""/>
..

你的HTML应该是

<img id="thumb1" class='thumb' scr=""/>
<img id="thumb2" class='thumb' scr=""/>
..

其次,您应确保在所有缩略图与其大图像对应物之间具有可识别的模式。在您的代码中我们有

0001.jpg
p_0002.jpg
_img/p_0003.jpg
0004.jpg

您的文件组织是什么?

让我们想象一下,缩略图与bug图像具有相同的src

jQuery代码将缩小为:

$('.thumb').mouseover(function(){

    var thumb_src = $(this).attr('src');

    // ==> add code here to transform thumb_src into the big_img src

    $('#big_img').fadeOut('slow', function(){
        $('#big_img').attr('src', thumb_src);
        $('#big_img').fadeIn('slow');
    });
});

此代码应该可以工作,只需等待将拇指的src转换为大图像的src的算法

我希望这会对你有所帮助 杰罗姆瓦格纳

答案 3 :(得分:0)

我认为你能做的最干净的事情就是扩展jQuery以包含你想要的功能:

//random images pulled off of Google Image Search
src1 = "http://www.o3mobi.com/jquery.jpg";
src2 = "http://www.bioneural.net/wp-content/uploads/2008/02/jquery.jpg";

$.fn.fadeToSrc = function(src, speedOut, speedIn, callback) {
    return this.fadeOut(speedOut, function() {
        $(this).attr('src', src).fadeIn(speedIn, callback);
    });
};

$("#image").click(function () {
    $(this).fadeToSrc(src2, 1000, 4000);
});

你可以在这里测试一下! http://jsfiddle.net/jPYyZ/

======更新=======

如果您想要执行鼠标悬停缩略图/预览系统,只需:

//HTML
<img class="thumbnail" src="http://www.o3mobi.com/jquery.jpg">
<img class="thumbnail" src="http://www.bioneural.net/wp-content/uploads/2008/02/jquery.jpg">
<img id="fullsized">


//CSS
​.thumbnail {
    width: 5em;
    height: 5em;
}

#fullsized {
    width: 10em;
    height: 10em;
    border: 2px solid red;
}​


//JAVASCRIPT
$.fn.fadeToSrc = function(src, speedOut, speedIn, callback) {
    return this.fadeOut(speedOut, function() {
        $(this).attr('src', src).fadeIn(speedIn, callback);
    });
};

$(".thumbnail").mouseover(function () {
    var newsrc = $(this).attr("src");
    $("#fullsized").fadeToSrc(newsrc, 1000, 1000);
});

你可以在这里测试/修补这个:http://jsfiddle.net/AhwH7/