Implementation of JavaScript image enlargement technology
This article introduces the JavaScript image magnification technology (magnifying glass) implementation code. Friends in need can refer to it
The specific code is as follows:
<!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> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> <title>JavaScript图片放大技术(放大镜)实现代码分享 - www.jb51.net</title> <style type="text/css"> #magnifier{ width:342px; height:420px; position:absolute; top:100px; left:250px; font-size:0; border:1px solid #000; } #img{ width:342px; height:420px; } #Browser{ border:1px solid #000; z-index:100; position:absolute; background:#555; } #mag{ border:1px solid #000; overflow:hidden; z-index:100; } </style> <script type="text/javascript"> function getEventObject(W3CEvent) {//事件标准化函数 return W3CEvent || window.event; } function getPointerPosition(e) {//兼容浏览器的鼠标x,y获得函数 e = e || getEventObject(e); var x = e.pageX || (e.clientX + (document.documentElement.scrollLeft || document.body.scrollLeft)); var y = e.pageY || (e.clientY + (document.documentElement.scrollTop || document.body.scrollTop)); return { 'x':x,'y':y }; } function setOpacity(elem,level) {//兼容浏览器设置透明值 if(elem.filters) { elem.style.filter = 'alpha(opacity=' + level * 100 + ')'; } else { elem.style.opacity = level; } } function css(elem,prop) { //css设置函数,可以方便设置css值,并且兼容设置透明值 for(var i in prop) { if(i == 'opacity') { setOpacity(elem,prop[i]); } else { elem.style[i] = prop[i]; } } return elem; } var magnifier = { m : null, init:function(magni){ var m = this.m = magni || { cont : null, //装载原始图像的p img : null, //放大的图像 mag : null, //放大框 scale : 15 //比例值,设置的值越大放大越大,但是这里有个问题就是如果不可以整除时,会产生些很小的白边,目前不知道如何解决 } css(m.img,{ 'position' : 'absolute', 'width' : (m.cont.clientWidth * m.scale) + 'px', //原始图像的宽*比例值 'height' : (m.cont.clientHeight * m.scale) + 'px' //原始图像的高*比例值 }) css(m.mag,{ 'display' : 'none', 'width' : m.cont.clientWidth + 'px', //m.cont为原始图像,与原始图像等宽 'height' : m.cont.clientHeight + 'px', 'position' : 'absolute', 'left' : m.cont.offsetLeft + m.cont.offsetWidth + 10 + 'px',//放大框的位置为原始图像的右方远10px 'top' : m.cont.offsetTop + 'px' }) var borderWid = m.cont.getElementsByTagName('p')[0].offsetWidth - m.cont.getElementsByTagName('p')[0].clientWidth; //获取border的宽 css(m.cont.getElementsByTagName('p')[0],{ //m.cont.getElementsByTagName('p')[0]为浏览框 'display' : 'none', //开始设置为不可见 'width' : m.cont.clientWidth / m.scale - borderWid + 'px', //原始图片的宽/比例值 - border的宽度 'height' : m.cont.clientHeight / m.scale - borderWid + 'px',//原始图片的高/比例值 - border的宽度 'opacity' : 0.5//设置透明度 }) m.img.src = m.cont.getElementsByTagName('img')[0].src;//让原始图像的src值给予放大图像 m.cont.style.cursor = 'crosshair'; m.cont.onmouseover = magnifier.start; }, start:function(e){ if(document.all){//只在IE下执行,主要避免IE6的select无法覆盖 magnifier.createIframe(magnifier.m.img); } this.onmousemove = magnifier.move;//this指向m.cont this.onmouseout = magnifier.end; }, move:function(e){ var pos = getPointerPosition(e); //事件标准化 this.getElementsByTagName('p')[0].style.display = ''; css(this.getElementsByTagName('p')[0],{ 'top' : Math.min(Math.max(pos.y - this.offsetTop - parseInt(this.getElementsByTagName('p')[0].style.height) / 2,0),this.clientHeight - this.getElementsByTagName('p')[0].offsetHeight) + 'px', 'left' : Math.min(Math.max(pos.x - this.offsetLeft - parseInt(this.getElementsByTagName('p')[0].style.width) / 2,0),this.clientWidth - this.getElementsByTagName('p')[0].offsetWidth) + 'px' //left=鼠标x - this.offsetLeft - 浏览框宽/2,Math.max和Math.min让浏览框不会超出图像 }) magnifier.m.mag.style.display = ''; css(magnifier.m.img,{ 'top' : - (parseInt(this.getElementsByTagName('p')[0].style.top) * magnifier.m.scale) + 'px', 'left' : - (parseInt(this.getElementsByTagName('p')[0].style.left) * magnifier.m.scale) + 'px' }) }, end:function(e){ this.getElementsByTagName('p')[0].style.display = 'none'; magnifier.removeIframe(magnifier.m.img); //销毁iframe magnifier.m.mag.style.display = 'none'; }, createIframe:function(elem){ var layer = document.createElement('iframe'); layer.tabIndex = '-1'; layer.src = 'javascript:false;'; elem.parentNode.appendChild(layer); layer.style.width = elem.offsetWidth + 'px'; layer.style.height = elem.offsetHeight + 'px'; }, removeIframe:function(elem){ var layers = elem.parentNode.getElementsByTagName('iframe'); while(layers.length >0){ layers[0].parentNode.removeChild(layers[0]); } } } window.onload = function(){ magnifier.init({ cont : document.getElementById('magnifier'), img : document.getElementById('magnifierImg'), mag : document.getElementById('mag'), scale : 3 }); } </script> </head> <body> <p id="magnifier"> <img src="//www.jb51.net/images/20091107/fangda.jpg" id="img" /> <p id="Browser"></p> </p> <p id="mag"><img id="magnifierImg" /></p> <select style="position:absolute;top:200px;left:650px;width:100px;"> <option>select测试</option> <option>是否能覆盖</option> </select> </body> </html>
The above is The entire content of this article is hoped to be helpful to everyone's study. For more related content, please pay attention to the PHP Chinese website!
Related recommendations:
How to use JS to implement the imitation WeChat payment pop-up window function
javascript to implement waterfall flow dynamic loading of images
JavaScript implements drawing graphics using Canvas
##
The above is the detailed content of Implementation of JavaScript image enlargement technology. For more information, please follow other related articles on the PHP Chinese website!

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics



How to use WebSocket and JavaScript to implement an online speech recognition system Introduction: With the continuous development of technology, speech recognition technology has become an important part of the field of artificial intelligence. The online speech recognition system based on WebSocket and JavaScript has the characteristics of low latency, real-time and cross-platform, and has become a widely used solution. This article will introduce how to use WebSocket and JavaScript to implement an online speech recognition system.

WebSocket and JavaScript: Key technologies for realizing real-time monitoring systems Introduction: With the rapid development of Internet technology, real-time monitoring systems have been widely used in various fields. One of the key technologies to achieve real-time monitoring is the combination of WebSocket and JavaScript. This article will introduce the application of WebSocket and JavaScript in real-time monitoring systems, give code examples, and explain their implementation principles in detail. 1. WebSocket technology

Introduction to how to use JavaScript and WebSocket to implement a real-time online ordering system: With the popularity of the Internet and the advancement of technology, more and more restaurants have begun to provide online ordering services. In order to implement a real-time online ordering system, we can use JavaScript and WebSocket technology. WebSocket is a full-duplex communication protocol based on the TCP protocol, which can realize real-time two-way communication between the client and the server. In the real-time online ordering system, when the user selects dishes and places an order

How to use WebSocket and JavaScript to implement an online reservation system. In today's digital era, more and more businesses and services need to provide online reservation functions. It is crucial to implement an efficient and real-time online reservation system. This article will introduce how to use WebSocket and JavaScript to implement an online reservation system, and provide specific code examples. 1. What is WebSocket? WebSocket is a full-duplex method on a single TCP connection.

JavaScript and WebSocket: Building an efficient real-time weather forecast system Introduction: Today, the accuracy of weather forecasts is of great significance to daily life and decision-making. As technology develops, we can provide more accurate and reliable weather forecasts by obtaining weather data in real time. In this article, we will learn how to use JavaScript and WebSocket technology to build an efficient real-time weather forecast system. This article will demonstrate the implementation process through specific code examples. We

JavaScript tutorial: How to get HTTP status code, specific code examples are required. Preface: In web development, data interaction with the server is often involved. When communicating with the server, we often need to obtain the returned HTTP status code to determine whether the operation is successful, and perform corresponding processing based on different status codes. This article will teach you how to use JavaScript to obtain HTTP status codes and provide some practical code examples. Using XMLHttpRequest

Usage: In JavaScript, the insertBefore() method is used to insert a new node in the DOM tree. This method requires two parameters: the new node to be inserted and the reference node (that is, the node where the new node will be inserted).

Introduction to the method of obtaining HTTP status code in JavaScript: In front-end development, we often need to deal with the interaction with the back-end interface, and HTTP status code is a very important part of it. Understanding and obtaining HTTP status codes helps us better handle the data returned by the interface. This article will introduce how to use JavaScript to obtain HTTP status codes and provide specific code examples. 1. What is HTTP status code? HTTP status code means that when the browser initiates a request to the server, the service
