< html xmlns="http://www.w3.org/1999/xhtml">[Recommended] JavaScript image magnification technology (magnifying glass) sample code - www.webdm.cn<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><br>function getEventObject(W3CEvent) {//Event normalization function <br> return W3CEvent || window.event;<br>}<br>function getPointerPosition(e) {//Browser-compatible mouse x, y get function<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) {//Set transparent value for compatible browsers<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 setting function, which can conveniently set css values, And compatible with setting transparent values <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, / /Load the div of the original image<br> img: null, //Enlarged image<br> mag: null, //Magnification box<br> scale: 15 //Scale value, the larger the value set, the greater the magnification, but there is a problem here is if When it is not divisible, some small white edges will be produced. I don’t know how to solve it at the moment. }<br> <br> css(m.img,{ <br> 'position' : 'absolute',<br> 'width' : (m.cont.clientWidth * m.scale) + 'px', //The width * ratio value of the original image <br> 'height' : (m.cont.clientHeight * m.scale) + 'px' //The height * ratio value of the original image <br> } )<br> <br> css(m.mag,{<br> 'display' : 'none',<br> 'width' : m.cont.clientWidth + 'px', //m.cont is the original image, which is the same width as the original image <br> ' height' : m.cont.clientHeight + 'px',<br> 'position' : 'absolute',<br> 'left' : m.cont.offsetLeft + m.cont.offsetWidth + 10 + 'px',//Enlarge the box The position is 10px to the right of the original image. .getElementsByTagName('div')[0].clientWidth; //Get the width of the border <br> <br> css(m.cont.getElementsByTagName('div')[0],{ //m.cont.getElementsByTagName('div') [0] is the browsing box<br> 'display' : 'none', //Start setting to invisible<br> 'width' : m.cont.clientWidth / m.scale - borderWid + 'px', //Width of the original image/ Proportional value - the width of the border <br> 'height' : m.cont.clientHeight / m.scale - borderWid + 'px',//The height/proportional value of the original image - the width of the border <br> 'opacity' : 0.5//Set transparency <br> })<br> <br> m.img.src = m.cont.getElementsByTagName('img')[0].src;//Let the src value of the original image be given to the enlarged image<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){//Only executed under IE, mainly to avoid IE6’s select from being overwritten<br> magnifier.createIframe(magnifier.m.img);<br> } <br> <br> this.onmousemove = magnifier.move;//This points to m.cont<br> this.onmouseout = magnifier.end;<br>},<br> <br> move:function(e){<br> var pos = getPointerPosition(e); //Event Standardize <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=mouse x - this.offsetLeft - browsing box width/2, Math.max and Math.min allow browsing The box will not exceed the image<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); //Destroy 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> . lt; body><br><div id="magnifier"><br><img src="http://up.2cto.com/2012/0309/20120309093024297.jpg" id="img" /><br><div id="Browser"></div><br></div><br><div id="mag"><img id="magnifierImg" /></div><br><select style ="position:absolute;top:200px;left:650px;width:100px;"><br><option>select test</option><br><option>whether it can cover</option><br></ select><br></body><br></html><br></p>