<div class="codetitle"> <span><a style="CURSOR: pointer" data="21928" class="copybut" id="copybut21928" onclick="doCopy('code21928')"><u>复制代码</u></a></span> 代码如下:</div> <div class="codebody" id="code21928"> <br> <br> <br> <br><meta http-equiv="Content-Type" content="text/html; charset=utf-8"> <br><title>JavaScript图片放大技术(放大镜)实现代码分享 - www.jb51.net</title> <br><style type="text/css"> <BR>#magnifier{ <BR> width:342px; <BR> height:420px; <BR> position:absolute; <BR> top:100px; <BR> left:250px; <BR> font-size:0; <BR> border:1px solid #000; <BR>} <BR>#img{ <BR> width:342px; <BR> height:420px; <BR>} <BR>#Browser{ <BR> border:1px solid #000; <BR> z-index:100; <BR> position:absolute; <BR> background:#555; <BR>} <BR>#mag{ <BR> border:1px solid #000; <BR> overflow:hidden; <BR> z-index:100; <BR>} <BR></style> <br><script type="text/javascript"> <BR>function getEventObject(W3CEvent) {//事件标准化函数 <BR> return W3CEvent || window.event; <BR>} <BR>function getPointerPosition(e) {//兼容浏览器的鼠标x,y获得函数 <BR> e = e || getEventObject(e); <BR> var x = e.pageX || (e.clientX + (document.documentElement.scrollLeft || document.body.scrollLeft)); <BR> var y = e.pageY || (e.clientY + (document.documentElement.scrollTop || document.body.scrollTop)); <br><br> return { 'x':x,'y':y }; <BR>} <BR>function setOpacity(elem,level) {//兼容浏览器设置透明值 <BR> if(elem.filters) { <BR> elem.style.filter = 'alpha(opacity=' + level * 100 + ')'; <BR> } else { <BR> elem.style.opacity = level; <BR> } <BR>} <BR>function css(elem,prop) { //css设置函数,可以方便设置css值,并且兼容设置透明值 <BR> for(var i in prop) { <BR> if(i == 'opacity') { <BR> setOpacity(elem,prop[i]); <BR> } else { <BR> elem.style[i] = prop[i]; <BR> } <BR> } <BR> return elem; <BR>} <BR>var magnifier = { <BR> m : null, <br><br> init:function(magni){ <BR> var m = this.m = magni || { <BR> cont : null, //装载原始图像的div <BR> img : null, //放大的图像 <BR> mag : null, //放大框 <BR> scale : 15 //比例值,设置的值越大放大越大,但是这里有个问题就是如果不可以整除时,会产生些很小的白边,目前不知道如何解决 <BR> } <br><br> css(m.img,{ <BR> 'position' : 'absolute', <BR> 'width' : (m.cont.clientWidth * m.scale) + 'px', //原始图像的宽*比例值 <BR> 'height' : (m.cont.clientHeight * m.scale) + 'px' //原始图像的高*比例值 <BR> }) <br><br> css(m.mag,{ <BR> 'display' : 'none', <BR> 'width' : m.cont.clientWidth + 'px', //m.cont为原始图像,与原始图像等宽 <BR> 'height' : m.cont.clientHeight + 'px', <BR> 'position' : 'absolute', <BR> 'left' : m.cont.offsetLeft + m.cont.offsetWidth + 10 + 'px',//放大框的位置为原始图像的右方远10px <BR> 'top' : m.cont.offsetTop + 'px'<BR> }) <br><br> var borderWid = m.cont.getElementsByTagName('div')[0].offsetWidth - m.cont.getElementsByTagName('div')[0].clientWidth; //获取border的宽 <br><br> css(m.cont.getElementsByTagName('div')[0],{ //m.cont.getElementsByTagName('div')[0]为浏览框 <BR> 'display' : 'none', //开始设置为不可见 <BR> 'width' : m.cont.clientWidth / m.scale - borderWid + 'px', //原始图片的宽/比例值 - border的宽度 <BR> 'height' : m.cont.clientHeight / m.scale - borderWid + 'px',//原始图片的高/比例值 - border的宽度 <BR> 'opacity' : 0.5//设置透明度 <BR> }) <br><br> m.img.src = m.cont.getElementsByTagName('img')[0].src;//让原始图像的src值给予放大图像 <BR> m.cont.style.cursor = 'crosshair'; <br><br> m.cont.onmouseover = magnifier.start; <br><br> }, <br><br> start:function(e){ <br><br> if(document.all){//只在IE下执行,主要避免IE6的select无法覆盖 <BR> magnifier.createIframe(magnifier.m.img); <BR> } <br><br> this.onmousemove = magnifier.move;//this指向m.cont <BR> this.onmouseout = magnifier.end; <BR> }, <br><br> move:function(e){ <BR> var pos = getPointerPosition(e); //事件标准化 <br><br> this.getElementsByTagName('div')[0].style.display = ''; <br><br> css(this.getElementsByTagName('div')[0],{ <BR> 'top' : Math.min(Math.max(pos.y - this.offsetTop - parseInt(this.getElementsByTagName('div')[0].style.height) / 2,0),this.clientHeight - this.getElementsByTagName('div')[0].offsetHeight) + 'px', <BR> 'left' : Math.min(Math.max(pos.x - this.offsetLeft - parseInt(this.getElementsByTagName('div')[0].style.width) / 2,0),this.clientWidth - this.getElementsByTagName('div')[0].offsetWidth) + 'px' //left=鼠标x - this.offsetLeft - 浏览框宽/2,Math.max和Math.min让浏览框不会超出图像 <BR> }) <br><br> magnifier.m.mag.style.display = ''; <br><br> css(magnifier.m.img,{ <BR> 'top' : - (parseInt(this.getElementsByTagName('div')[0].style.top) * magnifier.m.scale) + 'px', <BR> 'left' : - (parseInt(this.getElementsByTagName('div')[0].style.left) * magnifier.m.scale) + 'px'<BR> }) <br><br> }, <br><br> end:function(e){ <BR> this.getElementsByTagName('div')[0].style.display = 'none'; <BR> magnifier.removeIframe(magnifier.m.img); //销毁iframe <br><br> magnifier.m.mag.style.display = 'none'; <BR> }, <br><br> createIframe:function(elem){ <BR> var layer = document.createElement('iframe'); <BR> layer.tabIndex = '-1'; <BR> layer.src = 'javascript:false;'; <BR> elem.parentNode.appendChild(layer); <br><br> layer.style.width = elem.offsetWidth + 'px'; <BR> layer.style.height = elem.offsetHeight + 'px'; <BR> }, <br><br> removeIframe:function(elem){ <BR> var layers = elem.parentNode.getElementsByTagName('iframe'); <BR> while(layers.length >0){ <BR> layers[0].parentNode.removeChild(layers[0]); <BR> } <BR> } <BR>} <BR>window.onload = function(){ <BR> magnifier.init({ <BR> cont : document.getElementById('magnifier'), <BR> img : document.getElementById('magnifierImg'), <BR> mag : document.getElementById('mag'), <BR> scale : 3 <BR> }); <BR>} <BR></script> <br> <br> <br><div id="magnifier"> <br><img src="http://www.jb51.net/images/20091107/fangda.jpg" id="img" alt="JavaScript图片放大技术(放大镜)实现代码分享_javascript技巧" > <br><div id="Browser"></div> <br> </div> <br><div id="mag"><img id="magnifierImg" alt="JavaScript图片放大技术(放大镜)实现代码分享_javascript技巧" ></div> <br><select style="max-width:90%"> <br><option>select测试</option> <br><option>是否能覆盖</option> <br></select> <br> <br> <br><br> </div>