<br> *{<br> margin:0px;<br> padding:0px;<br> }<br> #box{<br> width:430px; <br> height:430px;<br> margin:100px;<br> margin-left:17%;<br> position:relative;//這裡使用相對定位,好讓其他的元素能依靠這個元素定位<br> }<br> #move{<br> background-color:green;<br> width:220px;<br> height:220px;<br> position:absolute;<br> left:0px;<br> top: 0px;<br> display:none;//先讓他隱藏,用js使其顯示<br> }<br> #bimg{<br> width:430px;<br> height:430px;<br> overflow: hidden;<br> position:absolute;<br> top:0px;<br> left:450px;<br> display:none;//先讓他隱藏,用js使其顯示<br> }<br> # bimg img{<br> width:800px;<br> height:800px;<br> position:absolute;<br> top:0px;<br> left:0px;<br><br> <# > <br></head><br><body onload="bigimg()"><br> <div id="box"><br> <img src="img/4.jpg " alt="#"><br> <div id="move"></div><br> <div id="bimg"><br> <img id="b_bimg"><br> <img id="b_bimg" src="img/4.jpg" alt="#"><br> </div><br> </div><br> <script><br>#. var bbox = document.getElementById("box");<br> var bmove = document.getElementById("move");<br> var bbimg = document.getElementById("bi ] getElementById("b_bimg");<br> bbox.onmouseover = function(){//滑鼠移到box上顯示大圖片和選框<br>bbimg.style.display = "block";<br>bmove.style .display="block";<br> }<br> bbox.onmouseout = function(){//滑鼠移開box不顯示大圖片和選取框<br>bbimg.style.display = "none";<br>bmove.style.display="none";<br> }<br> bbox.onmousemove = function(e){//取得滑鼠位置<br>var x = e.clientX;//滑鼠相對於視窗的位置<br>var y = e.clientY;<br>var t = bbox.offsetTop;//box相對於視口的位置<br>var l = bbox.offsetLeft;<br>var _left = x - l - bmove.offsetWidth/2;//計算move的位置<br>var _top = y - t -bmove.offsetHeight/2;<br>if(_top<=0)//滑到box的最上方<br/> _top = 0;<br/>else if(_top>=bbox.offsetHeight-bmove.offsetHeight)//滑到box的最底部<br> _top = bbox.offsetHeight-bmove.offsetHeight ;<br>if(_left<=0 )//滑到box的最左邊<br/> _left=0;<br/>else if(_left>=bbox.offsetWidth-bmove.offsetWidth)//滑到box的最右邊<br> _left=bbox.offsetWidth- bmove.offsetWidth ;<br>bmove.style.top = _top +"px";//設定move的位置<br>bmove.style.left = _left + "px";<br>var w = _left/(bbox .offsetWidth-bmove.offsetWidth);//計算移動的比例<br>var h = _top/(bbox.offsetHeight-bmove.offsetHeight);<br>var b_bimg_top = (b_bimg.offsetHeight-bbimg.offsetHeight)*h; //計算大圖的位置<br>var b_bimg_left = (b_bimg.offsetWidth-bbimg.offsetWidth)*w;<br>b_bimg.style.top = -b_bimg_top + "px";//設定大圖的位置資訊<br>b_bimg.style.left = -b_bimg_left + "px";<br> }<br><br> }<br> </script><br></body><br></html> <br></p> <p></p> <p></p> <p><img src="https://img.php.cn/upload/article/000/000/001/627a4c9c613b9f2bc25ecf0dfd66a71b-0.png" alt=""></p> <p></p> <p></p> <p></p> 黑色箭頭:<p></p> <div class="jb51code"><div class="syntaxhighlighter js"> <div class="toolbar"></div> <table border="0"> <tbody><tr> <td class="gutter">1<div class="line number1 index0 alt2"></div>2<div class="line number2 index1 alt1"></div> </td> <td class="code"><div class="container"> <div class="line number1 index0 alt2">var<code class="js keyword"> </code>x = e.clientX;<code class="js plain"></code>//滑鼠相對於視窗的位置<code class="js comments"></code> </div> <div class="line number2 index1 alt1">var<code class="js keyword"> </code>y = e.clientY;<code class="js plain"></code> </div> </div></td> </tr></tbody>#<p>紅箭:</p> <div class="jb51code"><div class="syntaxhighlighter js"> <div class="toolbar"> </div> <table border="0"><tbody><tr> <td class="gutter"> <div class="line number1 index0 alt2">1</div> <div class="line number2 index1 alt1">2 </div> </td> <td class="code"><div class="container"> <div class="line number1 index0 alt2"> <code class="js keyword">var</code> <code class="js plain">t = bbox.offsetTop;</code><code class="js comments">#//box相對於視窗的位置</code> </div> <div class="line number2 index1 alt1"> <code class="js keyword">var</code> <code class="js plain">l = bbox.offsetLeft;</code> </div> </div></td> </tr></tbody></table> </div></div> <p>橘色箭頭:</p> <div class="jb51code"><div class="syntaxhighlighter js"> <div class="toolbar"> </div> <table border="0"><tbody><tr> <td class="gutter"> <div class="line number1 index0 alt2">#1</div> <div class="line number2 index1 alt1">2</div> </td> <td class="code"> <div class="container"> <div class="line number1 index0 alt2"> <code class="js keyword">#var</code> <code class="js plain">_left = x - l - bmove.offsetWidth/2;</code><code class="js comments">//計算move的位置</code> </div> <div class="line number2 index1 alt1"> <code class="js keyword">#var</code> <code class="js plain">_top = y - t -bmove.offsetHeight/2;</code> </div> </div> </td> </tr></tbody></table> </div></div> <p>2、bimg區塊的計算</p> <p>利用move區塊在可移動範圍內的比例來設定大圖的位置</p> <p>move區塊的移動範圍:</p> <div class="jb51code"><div class="syntaxhighlighter js"> <div class="toolbar"> </div> <table border="0"><tbody><tr> <td class="gutter"><div class="line number1 index0 alt2">1</div></td> <td class="code"><div class="container"><div class="line number1 index0 alt2"><code class="js plain">#bbox.offsetWidth-bmove.offsetWidth</code></div></div></td> </tr></tbody></table> </div></div> <p>move區塊現在的座標佔可移動範圍的比例:</p> <div class="jb51code"> <div class="syntaxhighlighter js"> <div class="toolbar"> </div> <table border="0"><tbody><tr> <td class="gutter"> <div class="line number1 index0 alt2">1</div> <div class="line number2 index1 alt1">2</div> </td> <td class="code"><div class="container"> <div class="line number1 index0 alt2">##var<code class="js keyword"> </code>w = _left/(bbox.offsetWidth-bmove.offsetWidth);<code class="js plain"></code>//計算移動的比例<code class="js comments"></code> </div>##var<div class="line number2 index1 alt1"> <code class="js keyword">#h = _top/(bbox.offsetHeight-bmove.offsetHeight);</code><code class="js plain"></code> </div> </div></td> </tr></tbody></table> </div>bimg的移動範圍:</div> <p></p> <div class="jb51code"> <div class="syntaxhighlighter js"> <div class="toolbar"></div> <table border="0"><tbody><tr>1<td class="gutter"><div class="line number1 index0 alt2"></div></td> <td class="code"><div class="container">b_bimg.offsetHeight- bbimg.offsetHeight<div class="line number1 index0 alt2"><code class="js plain"></code></div> </div></td> </tr></tbody></table> </div>#bimg的位置:</div> <p></p> <div class="jb51code"> <div class="syntaxhighlighter js"> <div class="toolbar"></div> <table border="0"><tbody><tr>1<td class="gutter"> <div class="line number1 index0 alt2">2</div> <div class="line number2 index1 alt1"></div> </td> <td class="code"><div class="container">var<div class="line number1 index0 alt2"> <code class="js keyword"> b_bimg_top = (b_bimg.offsetHeight-bbimg.offsetHeight)*h;</code><code class="js plain">//計算大圖的位置</code><code class="js comments"></code> </div>var<div class="line number2 index1 alt1"> <code class="js keyword">#b_bimg_left = (b_bimg .offsetWidth-bbimg.offsetWidth)*w;</code><code class="js plain"></code> </div> </div></td> </tr></tbody></table> </div> </div> </table> </div></div>