我试过了,效果很好,可以试试!
HTML代码:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>仿淘宝网的UED相册,可图片放大</title>
<script src="http://www.codefans.net/ajaxjs/jquery-1.4.2.min.js"></script>
<style>
body { font-size:12px; padding:0}
</style>
<script language="javascript">
pic={
href:"http://www.codefans.net/jscss/demoimg/201105/b.jpg",
boxSize:250,
width:1330,
height:2128,
creatImg:function(id){//创建图片
var width = $("#"+id).css("width");
$("#"+id).append("<img src='"+this.href+"' style='width:"+width+"'/>");
$("#"+id).append("<div id='bigImg'></div>");
var that = this;
$("#"+id).mousemove(function(e){
var x = e.pageX-$(this).offset().left;
var y = e.pageY-$(this).offset().top;
if((x<parseInt(width) && x>0) && (y<that.height/that.multiple() && y>0)){
$("#bigImg").fadeIn(200);
that.creatBigImg(x,y);
}else{
$("#bigImg").fadeOut(200);
}
});
},
multiple:function(id){//和实际图片的倍数关系
return this.width/parseInt($("#pic").css("width"));
},
bigBoxPost:function(x,y){//大图的坐标
return [x-this.boxSize/2,y-this.boxSize/2];
},
bigLoadPost:function(x,y){//大图的坐标
return [-(x*this.multiple()-this.boxSize/2),-(y*this.multiple()-this.boxSize/2)];
},
creatBigImg:function(x,y){
var that = this;
$("#bigImg").css({
width:that.boxSize,
height:that.boxSize,
border:"#FFF 5px solid",
position:"absolute",
top:that.bigBoxPost(x,y)[1],
left:that.bigBoxPost(x,y)[0],
background:"url("+that.href+") no-repeat",
backgroundPosition:that.bigLoadPost(x,y)[0]+"px "+that.bigLoadPost(x,y)[1]+"px"
})
}
}
$(document).ready(function(){
pic.creatImg("pic");
})
</script>
</head>
<body >
<div id="pic" style=" width:500px; position: relative; margin:100px;"></div>
</body >
</html>