用更大的图像交换缩略图?

时间:2012-08-29 09:34:14

标签: javascript jquery

我有一个400px x 400px的大图像,下面有3个较小的图像,每个图像是100px x 100px。

当您点击其中一个缩略图时,它是否有可能与较大的图像交换,实质上是放大它?

2 个答案:

答案 0 :(得分:1)

编辑:

<强> HTML:

<div id="imgThumbs">
    <a href="#" class="showImg"><img src="img1-thumb.jpg" width="100" height="100" alt="Image 1" /></a>
    <a href="#" class="showImg"><img src="img2-thumb.jpg" width="100" height="100" alt="Image 2" /></a>
    <a href="#" class="showImg"><img src="img3-thumb.jpg" width="100" height="100" alt="Image 3" /></a>    
</div>

<div id="imgHolder"></div>

<强> CSS:

#imgThumbs {  
    overflow: hidden;
    margin: 20px auto;
    width: 366px;
    text-align: center;
}

.showImg { 
    width: 100px; 
    padding: 10px; 
    float:left; 
    border: 1px solid #fff;
    border-radius: 5px;    
}

#imgHolder {
    border-top: 5px solid #bbb;
    padding: 20px;
    margin: 0 auto;
    width: 80%;
}

.active {
    border: 1px dashed #aaa;
    background-color: #f4f4f4;
}

<强> jQuery的:

$('.showImg').hover(function(){        
    $(".showImg").removeClass("active");
    $(this).addClass("active");

    var imgURL = $(this).find('img').attr("src");    
    $('#imgHolder').find('img').attr("src", imgURL);    
});​

WORKING DEMO

答案 1 :(得分:0)

是的,这是可能的,也很容易。

您可以执行类似

的操作
// small images have the class 'small'
$('img.small').click(function(){
    this.src = 'path to big image';
});

通常使用文件命名约定。所以你的代码可能是

$('img.small').click(function(){
    // changes the source of small images
    // from somepath.png to somepath-big.png
    this.src = this.src.split('.')[0]+'-big.png';
    $(this).removeClass('small');
});

但你必须决定你想做什么......

相关问题