1. 程式人生 > >JS圖片放大1

JS圖片放大1

show nerd photos outer css ner ast else if 點擊圖片

$(function () {
//$(".pimg").click(function () {
$(".layer-photos-demo img").click(function () {
var _this = $(this);//將當前的pimg元素作為_this傳入函數
imgShow("#outerdiv", "#innerdiv", "#bigimg", _this);
});
});

function imgShow(outerdiv, innerdiv, bigimg, _this) {
var src = _this.attr("src");
$(bigimg).attr("src", src);

/*獲取當前點擊圖片的真實大小,並顯示彈出層及大圖*/
$("<img/>").attr("src", src).load(function () {
var windowW = $(window).width();
var windowH = $(window).height();
var realWidth = this.width;
var realHeight = this.height;
var imgWidth, imgHeight;
var scale = 0.8;

if (realHeight > windowH * scale) {
imgHeight = windowH * scale;
imgWidth = imgHeight / realHeight * realWidth;
if (imgWidth > windowW * scale) {
imgWidth = windowW * scale;
}
} else if (realWidth > windowW * scale) {
imgWidth = windowW * scale;
imgHeight = imgWidth / realWidth * realHeight;
} else {
imgWidth = realWidth;
imgHeight = realHeight;
}
$(bigimg).css("width", imgWidth);

var w = (windowW - imgWidth) / 2;
var h = (windowH - imgHeight) / 2;
$(innerdiv).css({ "top": h, "left": w });
$(outerdiv).fadeIn("fast");
});
$(outerdiv).click(function () {
$(this).fadeOut("fast");
});
}

JS圖片放大1