JS實(shí)現(xiàn)放大鏡效果
JS實(shí)現(xiàn)放大鏡效果,供大家參考,具體內(nèi)容如下
鼠標(biāo)移到圖片上就可以放大一塊區(qū)域
代碼:
<!DOCTYPE html><html lang='en'> <head> <meta charset='UTF-8'> <meta name='viewport' content='width=device-width, initial-scale=1.0'> <title>Document</title> <style> #small{ width: 700px; height: 340px; position: absolute; left: 70px; top: 100px; } #small img{ width: 100%; height: 100%; } #mark{ width: 200px; height: 200px; background-color:white; position: absolute; opacity: 0.5; filter: alpha(opacity=50); left: 0px; top: 0px; display: none; } #big{ width: 400px; height: 400px; border: 1px solid black; left: 800px; top: 100px; position: absolute; overflow: hidden; display: none; } #big img{ width: 1400px; height: 680px; position: absolute; left: 0px; top: 0px; } </style> <script> window.onload = function(){ var oSmall = document.getElementById('small'); var oBig = document.getElementById('big'); var oMark = document.getElementById('mark'); var oBigImg = oBig.getElementsByTagName('img')[0]; oSmall.onmouseover = function(){ oMark.style.display = 'block'; oBig.style.display = 'block'; } oSmall.onmouseout = function(){ oMark.style.display = 'none'; oBig.style.display = 'none'; } oSmall.onmousemove = function(ev){ var e = ev || window.event; var l = e.clientX - oSmall.offsetLeft - 100; if(l <= 0){ l = 0; } if(l >= (700-200)){ l = 500; } var t = e.clientY - oSmall.offsetTop - 100; if(t <= 0){ t = 0; } if(t >= (340-200)){ t = 140; } oMark.style.left = l + ’px’; oMark.style.top = t + ’px’; /* 右邊圖片移動(dòng)方向與左邊圖片方向相反且成倍數(shù)移動(dòng) */ oBigImg.style.left = l * -2 + ’px’; oBigImg.style.top = t * -2 + ’px’; } } </script> </head> <body> <div id='small'> <img src='http://www.baoyu77737.com/bcjs/地獄之門(mén)衛(wèi)士 加里奧.jpg' alt='怎么回事'> <div id='mark'></div> </div> <div id='big'> <img src='http://www.baoyu77737.com/bcjs/地獄之門(mén)衛(wèi)士 加里奧.jpg' alt='怎么回事'> </div> </body></html>
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持好吧啦網(wǎng)。
相關(guān)文章:
1. 淺談?dòng)蓀osition屬性引申的css進(jìn)階討論2. ASP中SELECT下拉菜單同時(shí)獲取VALUE和TEXT值的實(shí)現(xiàn)代碼3. 選擇模式 - XSL教程 - 24. 父div高度不能自適應(yīng)子div高度的解決方案5. ASP錯(cuò)誤捕獲的幾種常規(guī)處理方式6. 利用XMLSerializer將對(duì)象串行化到XML7. CSS3實(shí)例分享之多重背景的實(shí)現(xiàn)(Multiple backgrounds)8. 淺談XML Schema中的elementFormDefault屬性9. 前端從瀏覽器的渲染到性能優(yōu)化10. 三個(gè)不常見(jiàn)的 HTML5 實(shí)用新特性簡(jiǎn)介
