|
热门文章 |
|
|
|
|
JavaScript 实用的图片放大镜代码 |
来源:源码爱好者 更新时间:2011/2/18 9:10:10 阅读次数: 我要投稿 |
|
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <style type="text/css"> #div1 { width:304px; height:222px; position:relative; margin:30px auto 0px;} #div1 img{width:304px; height:222px;} #div1 span { width:100px; height:100px; background:red; left:0px;top:0px; position:absolute; display:none; filter:alpha(opacity:20); opacity:0.2;} .show { width:100%; height:100%; background:red; position:absolute; z-index:10px; filter:alpha(opacity:10); opacity:0.1; left:0px; top:0px; } #div2 {width:304px; height:222px; position:relative; display:none; overflow:hidden; margin:0px auto 0px;} #img1 { position:absolute;} </style> <meta http-equiv="Content-Type" content="text/html; charset=gb2312" /> <title>JavaScript 图片放大代码</title> <script type="text/javascript"> window.onload=function () { var oDiv=document.getElementById('div1'); var oShow=oDiv.getElementsByTagName('div')[0]; var oSpan=oDiv.getElementsByTagName('span')[0]; var oImg=document.getElementById('img1'); oShow.onmouseover=function() { oSpan.style.display='block'; oImg.parentNode.style.display='block'; }; oShow.onmouseout=function() { oSpan.style.display='none'; oImg.parentNode.style.display='none'; }; oShow.onmousemove=function(ev) { var oEvent=ev||event; var x=oEvent.clientX-oDiv.offsetLeft-oSpan.offsetWidth/2; var y=oEvent.clientY-oDiv.offsetTop-oSpan.offsetHeight/2; if(x<0) { x=0; } else if(x>oShow.offsetWidth-oSpan.offsetWidth) { x=oShow.offsetWidth-oSpan.offsetWidth; } if(y<0) { y=0; } else if(y>oShow.offsetHeight-oSpan.offsetHeight) { y=oShow.offsetHeight-oSpan.offsetHeight } oSpan.style.left=x+'px'; oSpan.style.top=y+'px'; var percentX=x/(oShow.offsetWidth-oSpan.offsetWidth); var percentY=y/(oShow.offsetHeight-oSpan.offsetHeight); var oImgparent=oImg.parentNode; oImg.style.left=-percentX*(oImg.offsetWidth-oImgparent.offsetWidth)+'px'; oImg.style.top=-percentY*(oImg.offsetHeight-oImgparent.offsetHeight)+'px'; }; }; </script> </head> <body> <div id="div1"> <img src="/effects/UploadFiles_7074/201102/2011021809122625.jpg" /> <span></span> <div class="show"></div> </div> <div id="div2"> <img id="img1" src="/effects/UploadFiles_7074/201102/2011021809122625.jpg" /> </div> </body> </html> |
特效说明: |
实用漂亮的放大镜效果,基于JavaScript代码实现,貌似代码不太多,但效果确实不错,因调用的图片比较大,所以运行效果后请稍等一会,等待图片加载。 |
|
上一篇文章: 鼠标放上后的图片由模糊变清晰的代码下一篇文章: Js图片轮播类附应用实例 |
|
|