当前位置:网站首页>Imitating the magnifying glass effect of JD products -- JS Foundation
Imitating the magnifying glass effect of JD products -- JS Foundation
2022-07-25 23:41:00 【qiaokelizhuzhu】
The analysis idea of imitating the effect of JD product magnifying glass
#### Mainly practice element offset offset Series
Display function :
- The mouse passes the picture box ( ** name :preview-img **), A small transparent box will appear under the mouse arrow ( name :mask), and A big picture box ( name :box)
- mask Used as a magnifying glass , Will follow the mouse in preview-img Internal movement ;
- Mouse in **preview-img When moving inside ,box The pictures in the will also follow the mouse preview-img ** Internal movement ;
- When the mouse weighs out preview-img,mask and box Also disappeared ;
To achieve the above functions , Disassembly procedure
- Write html frame
<div class="preview-img">
<img src="images/banner1.jpg" alt="" class="min-img">
<div class="mask"></div>
<div class="box">
<img src="images/banner1.jpg" alt="" class="boxImg">
</div>
</div>
- Definition of style
.preview-img{position:relative;width:400px;height:400px;text-align:center;border:solid 1px #ccc;}.min-img{width:400px;height:400px;}.mask{display:none;position:absolute;top:0;left:0;width:300px;height:300px;background-color:rgba(0,0,0,.3);}.box{display:none;position:absolute;top:0;right:-510px;width:500px;height:500px;z-index:999;overflow:hidden;}.box img{position:absolute;top:0;left:0;width:800px;height:800px;}
- js Code
// Get elements
var preview = document.querySelector('.preview-img');
var mask = document.querySelector('.mask');
var box = document.querySelector('.box');
// The mouse passes through the big box ,mask 、box Show
function over() {
mask.style.display = 'block';
box.style.display = 'block';
};
preview.addEventListener('mouseover', over);
// The mouse leaves the big box ,mask 、box hide
function out() {
mask.style.display = 'none';
box.style.display = 'none';
};
preview.addEventListener('mouseout', out);
// Get the real-time coordinates of the mouse moving in the box Assign a value to mask layer
//
function move(e) {
var x = e.pageX - preview.offsetLeft;
var y = e.pageY - preview.offsetTop;
var maskX = x - mask.offsetWidth / 2;
var masky = y - mask.offsetHeight / 2;
// If maskX Less than equal 0 Let it be equal to 0 Not allow masky beyond preview The left and right borders of
var maskMax = preview.offsetWidth - mask.offsetWidth;
if (maskX <= 0) {
maskX = 0
} else if (maskX >= maskMax) {
maskX = maskMax
}
// If masky Less than equal 0 Let it be equal to 0, Not allow masky beyond preview The upper and lower boundaries of
if (masky <= 0) {
masky = 0
} else if (masky >= preview.offsetHeight - mask.offsetHeight) {
masky = preview.offsetHeight - mask.offsetHeight
}
//offset Series can only read values without units ,style You can modify the number
mask.style.left = maskX + 'px';
mask.style.top = masky + 'px';
// Get the element of the largest picture
var boxImg = document.querySelector('.boxImg');
// The moving distance of the big picture
var bigMax = boxImg.offsetWidth - box.offsetWidth;
// Moving distance of large map =mask Moving distance * The maximum moving distance of the big picture / The maximum moving distance of the occlusion layer
// The moving distance of the big picture X Y
var bigX = maskX * bigMax / maskMax;
console.log(maskX);
var bigY = masky * bigMax / maskMax;
//style Assign to big picture x The distance between the upper distance and the base left distance
boxImg.style.left = -bigX + 'px';
boxImg.style.top = bigY + 'px';
}
preview.addEventListener('mousemove', move);
ps: The most difficult point for me is to understand this and calculate the moving distance of the big picture Formula
== Moving distance of large map =mask Moving distance * The maximum moving distance of the big picture / The maximum moving distance of the occlusion layer ==
边栏推荐
- Why are there many snapshot tables in the BI system?
- ratio学习之ratio_add,ratio_subtract,ratio_multiply,ratio_divide的使用
- Summary of built-in instructions and custom instructions
- TS interface
- 静态代理+动态代理
- [Muduo] package EventLoop and thread
- JS regular expression content:
- S4/HANA MM & SD EDI基于NAST的集成配置(ORDERS, ORDRSP, DESADV, INVOIC)
- Moment.js
- Implementation of mesh parameterized least squares conformal maps (3D mesh mapping to 2D plane)
猜你喜欢

行云管家V6.5.1/2/3系列版本发布:数据库OpenAPI能力持续强化

chown: changing ownership of ‘/var/lib/mysql/‘: Operation not permitted

ratio学习之ratio_add,ratio_subtract,ratio_multiply,ratio_divide的使用

Graph traversal DFS, BFS (code explanation)

S4/hana ME21N create Po output control message button missing solution (switch EDI output mode brf+ to Nast mode)

Dynamic memory management

initializer_ List tool library learning

What is the difference between'1 and'b1 when assigning values

Redis basic data type (string/list/set/hash/zset)

BI 系统中为什么会有很多快照表?
随机推荐
【MUDUO】EventLoopThreadPool
TS function
新手开户选择哪个券商公司好呢?安全吗
[QNX Hypervisor 2.2用户手册]9.8 load
Qpprogressbar for QT style (QSS) application
SAP Message No. VG202 IDoc E1EDK18 中付款条款已经转移:检查数据
Learning exploration-3d rotation card
Graph traversal DFS, BFS (code explanation)
【MUDUO】打包EventLoop和Thread
Macro task, micro task and event cycle mechanism
行云管家V6.5.1/2/3系列版本发布:数据库OpenAPI能力持续强化
【MUDUO】EventLoopThreadPool
Same origin strategy and cross domain
获取马蜂窝酒店数据
Qt风格(QSS)应用之QProgressBar
Matchmaker's words
Classes and objects (2) (6 default member functions)
【MUDUO】Thread封装
Multimodal deep multi modal sets
静态代理+动态代理