您好,登錄后才能下訂單哦!
將鼠標移動至圖片區域可放大預覽
效果圖:
代碼如下:
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>Title</title> <style> *{padding:0; margin:0;} #img1{width:300px; height:200px;} #sp1{width:60px; height:40px; position: absolute; left:0; top:0; background: rgba(62, 55, 58, 0.4) } #img2{width:2250px; height:1500px; position: absolute;} div{width:450px; height:300px; position: absolute; left:310px; top:0; overflow: hidden; border:2px solid black; display: none;} </style> <script> window.onload=function(){ var img1 = document.getElementById('img1'); var img2 = document.getElementById('img2'); var sp = document.getElementById('sp1'); var oBox = document.getElementById('box'); sp.onmouseover=function(){ oBox.style.display='block'; }; sp.onmouseout=function(){ oBox.style.display='none'; }; document.onmousemove=function(ev){ var x = (ev.clientX || event.clientX)-sp.offsetWidth/2; var y = (ev.clientY || event.clientY)-sp.offsetHeight/2; if((ev.clientX || event.clientX)<sp.offsetWidth/2){ x = 0; } if((ev.clientX || event.clientX)>img1.offsetWidth-sp.offsetWidth/2){ x = img1.offsetWidth-sp.offsetWidth; } if((ev.clientY || event.clientY)<sp.offsetHeight/2){ y = 0; }if((ev.clientY || event.clientY)>img1.offsetHeight-sp.offsetHeight/2){ y = img1.offsetHeight-sp.offsetHeight; } if((ev.clientX || event.clientX)<300 && (ev.clientY || event.clientY)<200) { sp.style.left = x + 'px'; sp.style.top = y + 'px'; img2.style.left = -x * (img2.offsetWidth / img1.offsetWidth) + 'px'; img2.style.top = -y * (img2.offsetHeight / img1.offsetHeight) + 'px'; } } } </script> </head> <body> <img src="https://timgsa.baidu.com/timg?image&quality=80&size=b9999_10000&sec=1489083204637&di=244fe324db033fc826de48e90d2373f0&imgtype=0&src=http%3A%2F%2Fpic2015.5442.com%2F2016%2F0328%2F025%2F5.jpg%2521960.jpg " id="img1"> <span id="sp1"></span> <div id="box"> <img src="https://timgsa.baidu.com/timg?image&quality=80&size=b9999_10000&sec=1489083204637&di=244fe324db033fc826de48e90d2373f0&imgtype=0&src=http%3A%2F%2Fpic2015.5442.com%2F2016%2F0328%2F025%2F5.jpg%2521960.jpg " id="img2"> </div> </body> </html>
以上就是本文的全部內容,希望本文的內容對大家的學習或者工作能帶來一定的幫助,同時也希望多多支持億速云!
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。