jsFiddleRun again Edit this fiddle Result HTML
放大镜 <script> <br>function PhotoZoomer(elements){ <br>this.mask = elements.mask; //蒙版 <br>this.container = elements.container //原图容器 <br>this.originimg = elements.originimg; //原图 <br>this.eventproxy = elements.eventproxy; <br>this.bigContainer = elements.bigContainer; //大图容器 <br>this.bigimg = elements.bigimg; //大图 <br>this.visible = false; <br>this._bind(); <br>} <br>PhotoZoomer.prototype = { <br>display: function(style){ <br>var self = this; <br>self.mask.style.display = style; <br>self.bigContainer.style.display = style; <br>}, <br>//计算放大蒙版位置 <br>zoom: function(clientX, clientY){ <br>var self = this, <br>//位置比例 <br>rate = {}, <br>//放大蒙版最大活动范围 <br>maxrange = { <br>offsetLeft: self.container.offsetWidth - self.mask.offsetWidth, <br>offsetTop: self.container.offsetHeight - self.mask.offsetHeight <br>}, <br>//mask left <br>left = clientX - self.container.offsetLeft - self.mask.offsetWidth/2, <br>//mask top <br>top = clientY - self.container.offsetTop - self.mask.offsetHeight/2; <br>if(left < 0) { <BR>left = 0; <BR>}else if(left> maxrange.offsetLeft) { <br>left = maxrange.offsetLeft; <br>} <br>if(top < 0) { <BR>top = 0; <BR>}else if(top > maxrange.offsetTop){ <br>top = maxrange.offsetTop; <br>} <br>//alert(maxrange.offsetTop); <br>rate.left = left / maxrange.offsetLeft; <br>rate.top = top / maxrange.offsetTop; <br>self.mask.style.left = left + 'px'; <br>self.mask.style.top = top + 'px'; <br>self.bigimg.style.left = -rate.left * (self.bigimg.offsetWidth - self.bigContainer.offsetWidth) + "px"; <br>self.bigimg.style.top = -rate.top * (self.bigimg.offsetHeight - self.bigContainer.offsetHeight) + "px"; <br>}, <br>_bind: function(){ <br>var self = this; <br>self.container.onmouseover = function(e){ <br>e = e || window.event; <br>var target = e.targe || e.srcElement; <br>self.display("block"); <br>this.visible = true; <br>}; <br>self.container.onmouseout = function(e){ <br>e = e || window.event; <br>var target = e.targe || e.srcElement; <br>self.display("none"); <br>this.visible = false; <br>}; <br>self.container.onmousemove = function(e){ <br>e = e || window.event; <br>if(!this.visible )return;//防止元素大小计算错误 <br>self.zoom(e.clientX, e.clientY); <br>}; <br>} <br>}; <br>function get(id){ <br>return document.getElementById(id) <br>} <br>var elements = { <br>mask: get("m"), <br>container: get("p1"), <br>originimg: get("z1"), <br>bigContainer: get("p2"), <br>bigimg: get("z2"), <br>eventproxy: get("eventproxy") <br>}; <br>var zoomer = new PhotoZoomer(elements); <br>// alert(elements.container.offsetParent.tagName) <br></script>
放大镜 <script> <br>function PhotoZoomer(elements){ <br>this.mask = elements.mask; //マスク<br>this.container = elements.container<br> .originimg = elements.originimg; //元のイメージ<br>this.eventproxy = elements.bigContainer; //ビッグイメージコンテナ<br>this.bigimg = elements.bigimg; /大きい画像<br>this.visible = false; <br>this._bind(); <br>PhotoZoomer.prototype = { <br>display: function(style){ <br>var self = this ; <br>self.mask.style.display = style; <br>}, <br>//ズームマスクの位置を計算します<br>zoom: function( , clientY){ <br>var self = this, <br>//位置比<br>rate = {}, <br>//マスクの最大アクティビティ範囲を拡大します<br>maxrange = { <br>offsetLeft : self.container.offsetWidth - self.mask.offsetWidth, <br>offsetTop: self.container.offsetHeight - self.mask.offsetHeight <br>}, <br>//マスク左 <br>left = clientX - self.コンテナ .offsetLeft - self.mask.offsetWidth/2, <br>//マスク トップ <br>top = clientY - self.container.offsetTop - self.mask.offsetHeight/2; <br>if(left left = 0; <br>}else if(left> maxrange.offsetLeft) { <br>left = maxrange.offsetLeft; <br>} <br>if(top < 0) { <BR>top = 0; <BR>}else if(top > maxrange.offsetTop){ <br>top = maxrange.offsetTop; <br>//alert(maxrange.offsetTop); left / maxrange.offsetLeft; <br>rate.top = トップ / maxrange.offsetTop; <br>self.mask.style.left = left 'px'; ; <br>self.bigimg.style.left = -rate.left * (self.bigimg.offsetWidth - self.bigContainer.offsetWidth) "px"; <br>self.bigimg.style.top = -rate.top * ( self.bigimg.offsetHeight - self.bigContainer.offsetHeight) "px"; <br>}, <br>_bind: function(){ <br>var self = this; <br>self.container.onmouseover = function( e ){ <br>e = e || <br>var target = e.srcElement; <br>this.visible = true ; <br>}; <br>self.container.onmouseout = function(e){ <br>e = e || <br>var target = e.srcElement; self .display("none"); <br>this.visible = false; <br>self.container.onmousemove = function(e){ <br>e = e || 🎜 >if(!this.visible )return;//要素サイズの計算エラーを防ぐ<br>self.zoom(e.clientX, e.clientY) <br>} <br>}; > function get(id){ <br>return document.getElementById(id) <br>} <br>var elements = { <br>マスク: get("m"), <br>コンテナ: get("p1" ) 、<br>originimg: get("z1")、<br>bigContainer: get("p2")、<br>bigimg: get("z2")、<br>eventproxy: get("eventproxy") <br>}; <br>varzoomer(elements); <br></script> < ;/html>