看JS源碼:
複製代碼 代碼如下:// 放大縮小控制
var PhotoSize = {
zoom: 0, // 縮放率
count: 0, // 縮放次數
cpu: 0, // 當前縮放倍數值
elem: "", // 圖片節點
photoWidth: 0, // 圖片初始寬度記錄
photoHeight: 0, // 圖片初始高度記錄
init: function(){
this.elem = document.getElementById("focusphoto");
this.photoWidth = this.elem.scrollWidth;
this.photoHeight = this.elem.scrollHeight;
this.zoom = 1.2; // 設定基本參數
this.count = 0;
this.cpu = 1;
},
action: function(x){
if(x === 0){
this.cpu = 1;
this.count = 0;
}else{
this.count += x; // 添加記錄
this.cpu = Math.pow(this.zoom, this.count); // 任意次冪運算
};
this.elem.style.width = this.photoWidth * this.cpu +"px";
this.elem.style.height = this.photoHeight * this.cpu +"px";
}
};
// 啟動放大縮小效果 用onload方式載入,防止第一次點擊擷取不到圖片的寬高
window.onload = function(){PhotoSize.init()};
建議最好採用onload方式引用,可以準確讀到初始圖片的大小 <meta http-equiv="Content-Type" content="text/html; charset=gb2312"><meta name="auther" content="f7"><input type="button" value="放大" onclick="PhotoSize.action(1);"><input type="button" value="縮小" onclick="PhotoSize.action(-1);"><input type="button" value="還原大小" onclick="PhotoSize.action(0);"><input type="button" value="查看當前倍數" onclick="alert(PhotoSize.cpu);"><br />