This article gives you a detailed analysis of the method of using jquery to achieve the magnifying glass effect, as well as code sharing. If you are interested, learn it.
The two magnifying glass effects written in jquery do not use plug-ins. Conditioning and clarity of thought. It is not written in an object-oriented way, so it is easier for beginners to understand. Without further ado, let’s look at the code. The pictures will not be uploaded here, you can find them by yourself. It is best to find the proportion, so the effect is better.
<body>
<p id="father">
<p id="container">
<img src="/static/imghwm/default1.png" data-src="img/400_1.jpg" class="lazy" style="max-width:90%" alt="Implement magnifying glass through jquery technology" >
<img src="/static/imghwm/default1.png" data-src="img/400_2.jpg" class="lazy" alt="Implement magnifying glass through jquery technology" >
<p class="shade"></p>
</p>
<p class="small first"><img src="/static/imghwm/default1.png" data-src="img/50_1.jpg" class="lazy" alt="Implement magnifying glass through jquery technology" ></p>
<p class="small second"><img src="/static/imghwm/default1.png" data-src="img/50_2.jpg" class="lazy" alt="Implement magnifying glass through jquery technology" ></p>
</p>
<p class="big">
<img src="/static/imghwm/default1.png" data-src="img/800_1.jpg" class="lazy" style="max-width:90%" alt="Implement magnifying glass through jquery technology" >
<img src="/static/imghwm/default1.png" data-src="img/800_2.jpg" class="lazy" alt="Implement magnifying glass through jquery technology" >
</p>
</body>css code
*{padding: 0; margin: 0;}
#father .small{width: 50px; height: 50px; border: 2px solid #ccc; bottom: 0; position: absolute;}
#father .second{left: 70px;}
.third{left: 140px;}
#father{position: relative; top: 100px; left: 50px; height: 460px;}
#container{position: absolute; width: 400px; height: 400px;}
#container img{position: absolute; display: none;}
.shade{width: 200px; height: 200px; position: absolute; background: #000; opacity: 0.4; top: 0;
left: 0; display: none;}
.big{width: 400px; height: 400px; position: absolute; top: 100px; overflow: hidden; left: 500px; display: none;}
.big img{width: 800px; height: 800px; position: absolute; display: none;}js code
<script type="text/javascript" src='js/jquery-1.12.4.min.js'></script>
<script type="text/javascript">
$(function () {
changePic('.first',0);
changePic('.second',1);
var shadeWidth = $('.shade').width(),//阴影的宽度
shadeHeight = $('.shade').height(),//阴影的高度
middleWidth = $('#container').width(),//容器的宽度
middleHeight = $('#container').height(),//容器的高度
bigWidth = $('.big').width(),//放大图片盒子的宽度
bigHeight = $('.big').height(),//放大图片盒子的高度
rateX = bigWidth / shadeWidth,//放大区和遮罩层的宽度比例
rateY = bigHeight / shadeHeight;//放大区和遮罩层的高度比例
//当鼠标移入与移出时阴影与放大去显现/消失
$('#container').hover(function() {
$('.shade').show();
$('.big').show();
}, function() {
$('.shade').hide();
$('.big').hide();
}).mousemove(function(e) {//当鼠标移动时,阴影和放大区图片进行移动
//记录下光标距离页面的距离
var x = e.pageX,
y = e.pageY;
//设置遮罩层的位置
$('.shade').offset({
top: y-shadeHeight/2,
left: x-shadeWidth/2
});
//获取遮罩层相对父元素的位置
var cur = $('.shade').position(),
_top = cur.top,
_left = cur.left,
hdiffer = middleHeight - shadeHeight,
wdiffer = middleWidth - shadeWidth;
if (_top < 0) _top = 0;
else if (_top > hdiffer) _top = hdiffer;
if (_left < 0) _left = 0;
else if (_left > wdiffer) _left =wdiffer;
//判断完成后设置遮罩层的范围
$('.shade').css({
top: _top,
left: _left
});
//设置放大区图片移动
$('.big img').css({
top: - rateY*_top,
left: - rateX*_left
});
});;
//封装的改变图片显示的函数
function changePic (element,index) {
$(element).click(function() {
$('#container img').eq(index).css('display', 'block').siblings().css('display', 'none');
$('.big img').eq(index).css('display', 'block').siblings().css('display', 'none');
});
}
});The above are commonly used, the following one is enlarged based on the original image of
htm
<body>
<p id="father">
<p id="container">
<img src="/static/imghwm/default1.png" data-src="img/400_1.jpg" class="lazy" style="max-width:90%" alt="Implement magnifying glass through jquery technology" >
<img src="/static/imghwm/default1.png" data-src="img/400_2.jpg" class="lazy" alt="Implement magnifying glass through jquery technology" >
<img src="/static/imghwm/default1.png" data-src="img/400_3.jpg" class="lazy" alt="Implement magnifying glass through jquery technology" >
<p class="shade">
<img src="/static/imghwm/default1.png" data-src="img/800_1.jpg" class="lazy" style="max-width:90%" alt="Implement magnifying glass through jquery technology" >
<img src="/static/imghwm/default1.png" data-src="img/800_2.jpg" class="lazy" alt="Implement magnifying glass through jquery technology" >
<img src="/static/imghwm/default1.png" data-src="img/800_3.jpg" class="lazy" alt="Implement magnifying glass through jquery technology" >
</p>
</p>
<p class="small first"><img src="/static/imghwm/default1.png" data-src="img/50_1.jpg" class="lazy" alt="Implement magnifying glass through jquery technology" ></p>
<p class="small second"><img src="/static/imghwm/default1.png" data-src="img/50_2.jpg" class="lazy" alt="Implement magnifying glass through jquery technology" ></p>
<p class="small third"><img src="/static/imghwm/default1.png" data-src="img/50_3.jpg" class="lazy" alt="Implement magnifying glass through jquery technology" ></p>
</p>
</body>css code
*{padding: 0; margin: 0;}
#father .small{width: 50px; height: 50px; border: 2px solid #ccc; bottom: 0; position: absolute;}
#father .second{left: 70px;}
.third{left: 140px;}
#father{position: relative; top: 100px; left: 50px; height: 460px;}
#container{position: absolute; width: 400px; height: 400px;}
#container img{position: absolute; display: none;}
.shade{width: 200px; height: 200px; position: absolute; top: 0;left: 0; display: none; border-radius: 50%; overflow: hidden; background: #000;}
.shade img{display: none; width: 800px; height: 800px; position: absolute;}js code
<span style="white-space:pre"> </span><script type="text/javascript" src='js/jquery-1.12.4.min.js'></script>
<script type="text/javascript">
$(function () {
changePic('.first',0);
changePic('.second',1);
changePic('.third',2);
var shadeWidth = $('.shade').width(),//阴影的宽度
shadeHeight = $('.shade').height(),//阴影的高度
middleWidth = $('#container').width(),//容器的宽度
middleHeight = $('#container').height(),//容器的高度
bigImgWidth = $('.shade img').width(),//放大图片盒子的宽度
bigImgHeight = $('.shade img').height(),//放大图片盒子的高度
rateX = bigImgWidth / middleWidth,//放大区和遮罩层的宽度比例2
rateY = bigImgHeight / middleHeight;//放大区和遮罩层的高度比例2
//当鼠标移入与移出时阴影与放大去显现/消失
$('#container').hover(function() {
$('.shade').show();
$('.big').show();
}, function() {
$('.shade').hide();
$('.big').hide();
}).mousemove(function(e) {//当鼠标移动时,阴影和放大区图片进行移动
//记录下光标距离页面的距离
var x = e.pageX,
y = e.pageY;
//设置遮罩层的位置
$('.shade').offset({
top: y-shadeHeight/2,
left: x-shadeWidth/2
});
//获取遮罩层相对父元素的位置
var cur = $('.shade').position(),
_top = cur.top,
_left = cur.left,
hdiffer = middleHeight - shadeHeight,
wdiffer = middleWidth - shadeWidth;
if (_top < 0) _top = 0;
else if (_top > hdiffer) _top = hdiffer;
if (_left < 0) _left = 0;
else if (_left > wdiffer) _left =wdiffer;
//判断完成后设置遮罩层的范围
$('.shade').css({
top: _top,
left: _left
});
//设置放大区图片移动
$('.shade img').css({
top: - _top*rateY*3/2,
left: - _left*rateX*3/2
});
});;
//封装的改变图片显示的函数
function changePic (element,index) {
$(element).click(function() {
$('#container img').eq(index).css('display', 'block').siblings().css('display', 'none');
$('.shade img').eq(index).css('display', 'block').siblings().css('display', 'none');
});
}
});
<span style="white-space:pre"> </span></script>The above is what I compiled for everyone. I hope it will be helpful to everyone in the future.
Related articles:
How to implement custom global methods in vue
How to implement global registration and local registration in vue components
How to modify the project name through the vue-cli webpack project
The above is the detailed content of Implement magnifying glass through jquery technology. For more information, please follow other related articles on the PHP Chinese website!
JavaScript in Action: Real-World Examples and ProjectsApr 19, 2025 am 12:13 AMJavaScript's application in the real world includes front-end and back-end development. 1) Display front-end applications by building a TODO list application, involving DOM operations and event processing. 2) Build RESTfulAPI through Node.js and Express to demonstrate back-end applications.
JavaScript and the Web: Core Functionality and Use CasesApr 18, 2025 am 12:19 AMThe main uses of JavaScript in web development include client interaction, form verification and asynchronous communication. 1) Dynamic content update and user interaction through DOM operations; 2) Client verification is carried out before the user submits data to improve the user experience; 3) Refreshless communication with the server is achieved through AJAX technology.
Understanding the JavaScript Engine: Implementation DetailsApr 17, 2025 am 12:05 AMUnderstanding how JavaScript engine works internally is important to developers because it helps write more efficient code and understand performance bottlenecks and optimization strategies. 1) The engine's workflow includes three stages: parsing, compiling and execution; 2) During the execution process, the engine will perform dynamic optimization, such as inline cache and hidden classes; 3) Best practices include avoiding global variables, optimizing loops, using const and lets, and avoiding excessive use of closures.
Python vs. JavaScript: The Learning Curve and Ease of UseApr 16, 2025 am 12:12 AMPython is more suitable for beginners, with a smooth learning curve and concise syntax; JavaScript is suitable for front-end development, with a steep learning curve and flexible syntax. 1. Python syntax is intuitive and suitable for data science and back-end development. 2. JavaScript is flexible and widely used in front-end and server-side programming.
Python vs. JavaScript: Community, Libraries, and ResourcesApr 15, 2025 am 12:16 AMPython and JavaScript have their own advantages and disadvantages in terms of community, libraries and resources. 1) The Python community is friendly and suitable for beginners, but the front-end development resources are not as rich as JavaScript. 2) Python is powerful in data science and machine learning libraries, while JavaScript is better in front-end development libraries and frameworks. 3) Both have rich learning resources, but Python is suitable for starting with official documents, while JavaScript is better with MDNWebDocs. The choice should be based on project needs and personal interests.
From C/C to JavaScript: How It All WorksApr 14, 2025 am 12:05 AMThe shift from C/C to JavaScript requires adapting to dynamic typing, garbage collection and asynchronous programming. 1) C/C is a statically typed language that requires manual memory management, while JavaScript is dynamically typed and garbage collection is automatically processed. 2) C/C needs to be compiled into machine code, while JavaScript is an interpreted language. 3) JavaScript introduces concepts such as closures, prototype chains and Promise, which enhances flexibility and asynchronous programming capabilities.
JavaScript Engines: Comparing ImplementationsApr 13, 2025 am 12:05 AMDifferent JavaScript engines have different effects when parsing and executing JavaScript code, because the implementation principles and optimization strategies of each engine differ. 1. Lexical analysis: convert source code into lexical unit. 2. Grammar analysis: Generate an abstract syntax tree. 3. Optimization and compilation: Generate machine code through the JIT compiler. 4. Execute: Run the machine code. V8 engine optimizes through instant compilation and hidden class, SpiderMonkey uses a type inference system, resulting in different performance performance on the same code.
Beyond the Browser: JavaScript in the Real WorldApr 12, 2025 am 12:06 AMJavaScript's applications in the real world include server-side programming, mobile application development and Internet of Things control: 1. Server-side programming is realized through Node.js, suitable for high concurrent request processing. 2. Mobile application development is carried out through ReactNative and supports cross-platform deployment. 3. Used for IoT device control through Johnny-Five library, suitable for hardware interaction.


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

SublimeText3 Chinese version
Chinese version, very easy to use

MinGW - Minimalist GNU for Windows
This project is in the process of being migrated to osdn.net/projects/mingw, you can continue to follow us there. MinGW: A native Windows port of the GNU Compiler Collection (GCC), freely distributable import libraries and header files for building native Windows applications; includes extensions to the MSVC runtime to support C99 functionality. All MinGW software can run on 64-bit Windows platforms.

Dreamweaver CS6
Visual web development tools

mPDF
mPDF is a PHP library that can generate PDF files from UTF-8 encoded HTML. The original author, Ian Back, wrote mPDF to output PDF files "on the fly" from his website and handle different languages. It is slower than original scripts like HTML2FPDF and produces larger files when using Unicode fonts, but supports CSS styles etc. and has a lot of enhancements. Supports almost all languages, including RTL (Arabic and Hebrew) and CJK (Chinese, Japanese and Korean). Supports nested block-level elements (such as P, DIV),

Zend Studio 13.0.1
Powerful PHP integrated development environment






