belford belford - 3 months ago 14
CSS Question

jQuery - onclick zoom function for multiple images

I would like to ask you about the way to reuse the code below for multiple images:
http://jsfiddle.net/a8c9P/

How to avoid redundancy in the CSS code?

An updated example: http://jsfiddle.net/a8c9P/156

$("#imgSmall").click(function() {
$("#imgBig").attr("src", "http://www.freeimageslive.com/galleries/sports/music/pics/musical_notes.jpg");
$("#overlay").show();
$("#overlayContent").show();
});

$("#imgBig").click(function(){
$("#imgBig").attr("src", "");
$("#overlay").hide();
$("#overlayContent").hide();
});

Answer

http://jsfiddle.net/a8c9P/157/

HTML

<div id="overlay"></div>
<div id="overlayContent">
    <img id="imgBig" src="" alt="" width="400" />
</div>

<img class="imgSmall" width="200" src="http://www.freeimageslive.com/galleries/space/earth/pics/a17_h_148_22718.gif" alt="" />


<img class="imgSmall" width="200" src="http://www.freeimageslive.com/galleries/space/earth/pics/a17_h_148_22718.gif" alt="" />

<img class="imgSmall" width="200" src="http://www.freeimageslive.com/galleries/space/earth/pics/a17_h_148_22718.gif" alt="" />

JS

$(".imgSmall").click(function(){
    $("#imgBig").attr("src",$(this).attr('src'));
    $("#overlay").show();
    $("#overlayContent").show();
});

$("#imgBig").click(function(){
    $("#imgBig").attr("src", "");
    $("#overlay").hide();
    $("#overlayContent").hide();
});

CSS

#overlay{
    position: fixed; 
    width: 100%; 
    height: 100%; 
    top: 0px; 
    left: 0px; 
    background-color: #000; 
    opacity: 0.7;
    filter: alpha(opacity = 70) !important;
    display: none;
    z-index: 100;

}

#overlayContent{
    position: fixed; 
    width: 100%;
    top: 100px;
    text-align: center;
    display: none;
    overflow: hidden;
    z-index: 100;
}

#contentGallery{
    margin: 0px auto;
}

#imgBig, .imgSmall{
    cursor: pointer;
}