javascript|網頁
參考了一些代碼,自己寫了個圖片縮放的指令碼,可以點擊放大,同時用滑鼠滑輪自由縮放.
//圖片按比例縮放,可輸入參數設定初始大小
function resizeimg(ImgD,iwidth,iheight) {
var image=new Image();
image.src=ImgD.src;
if(image.width>0 && image.height>0){
if(image.width/image.height>= iwidth/iheight){
if(image.width>iwidth){
ImgD.width=iwidth;
ImgD.height=(image.height*iwidth)/image.width;
}else{
ImgD.width=image.width;
ImgD.height=image.height;
}
ImgD.alt=image.width+"×"+image.height;
}
else{
if(image.height>iheight){
ImgD.height=iheight;
ImgD.width=(image.width*iheight)/image.height;
}else{
ImgD.width=image.width;
ImgD.height=image.height;
}
ImgD.alt=image.width+"×"+image.height;
}
ImgD.style.cursor= "pointer"; //改變滑鼠指標
ImgD.onclick = function() { window.open(this.src);} //點擊開啟大圖片
if (navigator.userAgent.toLowerCase().indexOf("ie") > -1) { //判斷瀏覽器,如果是IE
ImgD.title = "請使用滑鼠滾輪縮放圖片,點擊圖片可在新視窗開啟";
ImgD.onmousewheel = function img_zoom() //滾輪縮放
{
var zoom = parseInt(this.style.zoom, 10) || 100;
zoom += event.wheelDelta / 12;
if (zoom> 0) this.style.zoom = zoom + "%";
return false;
}
} else { //如果不是IE
ImgD.title = "點擊圖片可在新視窗開啟";
}
}
}
具體實現代碼如下:
<img name="" src="" >
可能必較多,如果有更好的代碼,歡迎指教!