62,041
社区成员
发帖
与我相关
我的任务
分享
<script language="javascript" type="text/javascript" src="js/jquery-1.2.3.js"></script>
<script language="javascript" type="text/javascript">
$(document).ready(function(){
var el = $("#col > img");
el.css({width: 90, height: 90});
el.mouseover(function(){
$(this).css({opacity:"1"}).stop().animate({opacity:".4"},300);
});
el.mouseout(function(){
$(this).css({opacity:".4"}).stop().animate({opacity:"1"},300);
});
el.click(function(){
var url = $(this).attr("title");
view(url);
});
});
var view = function(url){
var $_ = $(this),$img = $('img', $_),$el = $("#img1"),$load = $("#load");
var $w, $h, wContainer, hContainer;
wContainer = 400;//缩放宽度设置为500
hContainer = 500;//缩放高度设置为400
$load.css({opacity:"1",top:300,left:300,position:'absolute'}).show(); //load图片
$el.attr({src:url}).css({top:300,left:300,position:'absolute'}).hide();//img
$img = new Image();
$img.onload = function(){//创建image对象 添加onload方法
$img.onload = null;
$w = $img.width;//获取图片宽度
$h = $img.height;//获取图片高度
if ($w > wContainer) {//如果图片宽度大于 缩放宽度
$h = $h * (wContainer / $w);
$w = wContainer;
if ($h > wContainer) {
$w = $w * (hContainer / $h);
$h = hContainer;
}
} else if ($h > hContainer) { //如果图片高度大于 缩放高度
$w = $w * (hContainer / $h);
$h = hContainer;
if ($w > wContainer) {
$h = $h * (wContainer / $w);
$w = wContainer;
}
}
$el.css({width:$w,height:$h, marginLeft:(wContainer-$w)*.5,marginTop:(hContainer-$h)*.5}) //img样式--缩放后的高度,缩放后的宽度
$load.fadeOut('fast',function(){$el.fadeIn();});//load隐藏,img显示
};
$img.src = url;
};
</script>