var zoomSmallNum=90; //縮小倍數
var zoomNum; //中間變量
//鼠標滾輪滑動縮放圖片
function resizeimg(obj){
var iwidth=1000;
var iheight=1200;
var image=new Image();
image.src=obj.src;
if(image.width>0 && image.height>0){ //控制初始圖片大小
if(image.width/image.height>= iwidth/iheight){
if(image.width>iwidth){
obj.width=iwidth;
obj.height=(image.height*iwidth)/image.width;
}else{
obj.width=image.width;
obj.height=image.height;
}
}
else{
if(image.height>iheight){
obj.height=iheight;
obj.width=(image.width*iheight)/image.height;
}else{
obj.width=image.width;
obj.height=image.height;
}
}
}
obj.style.cursor= "pointer"; //改變鼠標指針
if (navigator.userAgent.toLowerCase().indexOf("ie") > -1) { //判斷瀏覽器,如果是IE
obj.title = "請使用鼠標滾輪縮放圖片";
obj.onmousewheel = function img_zoom(){ //滾輪縮放
var zoom = parseInt(this.style.zoom, 10) || 100;
zoom += event.wheelDelta / 12;
if (zoom> 0){
if(zoom>100){
MakeZoomPic(obj,"large"); //鼠標滾輪放大圖片
}else{
MakeZoomPic(obj,"small"); //鼠標滾輪縮小圖片
}
}
return false;
}
}else{ //如果不是IE
obj.title = "點擊圖片可在新窗口打開";
}
}
function MakeZoomPic(selPic,zoomType){ //圖片縮放事件,以焦點爲中心進行縮放.處理鼠標單擊、滾輪事件。
selPic.style.position = "absolute";
var elm = event.srcElement;
var iDivHeight = document.body.offsetHeight; //獲得當前ie高度
var iDivWidth = document.body.offsetWidth; //獲得當前ie寬度
var iBigImgWidth;
var iBigImgHeight;
var iPosX, iPosY; //放大圖對應顯示區域左上角的座標
var iMouseX = event.offsetX; //鼠標對應縮略圖左上角的橫座標
var iMouseY = event.offsetY; //鼠標對應縮略圖左上角的縱座標
if(zoomType=="large"){
zoomNum=zoomLargeNum;
iBigImgWidth = selPic.clientWidth * zoomNum/100; //放大圖寬度,是縮略圖的寬度乘以放大倍數
iBigImgHeight = selPic.clientHeight * zoomNum/100; //放大圖高度,是縮略圖的高度乘以放大倍數
}else{
zoomNum=zoomSmallNum;
iBigImgWidth = selPic.clientWidth * zoomNum/100; //縮小圖寬度,是縮略圖的寬度乘以縮小倍數
iBigImgHeight = selPic.clientHeight * zoomNum/100; //縮小圖高度,是縮略圖的高度乘以縮小倍數
}
if (iBigImgWidth <= iDivWidth){
iPosX = 0;
}else{
if ((iMouseX * zoomNum/100) <= (iDivWidth / 2)){
iPosX = 0;
}else{
if (((selPic.clientWidth - iMouseX) * zoomNum/100) <= (iDivWidth / 2)){
iPosX = -(iBigImgWidth - iDivWidth);
}else{
iPosX = -(iMouseX * zoomNum/100 - iDivWidth / 2);
}
}
}
if (iBigImgHeight <= iDivHeight){
iPosY = 0;
}else{
if ((iMouseY * zoomNum/100) <= (iDivHeight / 2)){
iPosY = 0;
}else{
if (((selPic.clientHeight - iMouseY) * zoomNum/100) <= (iDivHeight / 2)){
iPosY = -(iBigImgHeight - iDivHeight);
}else{
iPosY = -(iMouseY * zoomNum/100 - iDivHeight / 2);
}
}
}
selPic.style.top = iPosY;
selPic.style.left = iPosX;
selPic.width = iBigImgWidth;
selPic.height = iBigImgHeight;
}