jquery - Using image value to choose an image from an array and display it (Javascript) -
i have bunch of pictures , want display bigger version of them when clicked. have right now:
<div class="gallery"> <div> <img src="content/imggallery/img1.jpg" class="oldimg" value="0"> </div> <div> <img src="content/imggallery/img2.jpg" class="oldimg" value="1"> </div> <div> <img src="content/imggallery/img3.jpg" class="oldimg" value="2"> </div> </div>
and javascript/jquery:
$(function() { var docheight = $(document).height(); var imagedata = new array (3); imagedata[0]="0.jpg"; imagedata[1]="1.jpg"; imagedata[2]="2.jpg"; $(".oldimg").click(function(){ $("body").append("<div id='overlay'></div>"); $("body").append("<div id='newimg'></div>"); $("#overlay") .height(docheight) .css({ 'opacity' : 0.4, 'position': 'fixed', 'top': 0, 'left': 0, 'background-color': 'black', 'width': '100%', '-ms-filter': 'progid:dximagetransform.microsoft.alpha(opacity=50)', 'filter': 'alpha(opacity=50)', 'z-index': 1000 }); $("#newimg") .css({ 'position': 'fixed', 'top': '50%', 'left': '50%', 'background-image': 'url(' + imagedata[] + ')', 'transform': 'translate(-50%, -50%)', 'width': '100%', 'height': '100%', 'z-index': 1010 }); $("#newimg").click(function(){ $("#newimg").remove(); $("#overlay").remove(); }); }); });
now, how value of image, pass array , 'background-image': 'url(' + imagedata[] + ')'
? additionally, how adjust #newimg
size fit image? because width
, height 100%
should no-reapeat, put no-reapeat?
code works fixed height/width , specific img background i. e. 1.jpg
.
working example in jsfiddle: http://jsfiddle.net/bx27sjll/
big thanks!
use attr() image value
$(".oldimg").click(function(){ var value= $(this).attr("value"); imagedata[value]; // value });
Comments
Post a Comment