当前位置:网站首页>Imitation Jingdong magnifying glass effect (pink teacher version)
Imitation Jingdong magnifying glass effect (pink teacher version)
2022-07-02 19:04:00 【Zhou million】

First, define it briefly HTML and CSS, That's not the point , The focus is on learning javascript
<style>
div {
box-sizing: border-box;
}
.content {
width: 1000px;
height: 600px;
margin: 0 auto;
margin-top: 100px;
}
.left {
position: relative;
float: left;
width: 400px;
height: 400px;
border: 1px solid #ccc;
}
.left img {
width: 100%;
}
.right {
overflow: hidden;
position: relative;
display: none;
float: left;
width: 600px;
height: 736px;
border: 1px solid #ccc;
}
.right img {
position: absolute;
top: 40px;
left: 50px;
height: 636px;
}
span {
position: absolute;
top: 0;
left: 0;
width: 200px;
height: 200px;
background-color: rgb(193, 193, 33);
opacity: .5;
display: none;
}
</style>
<script src=" Zoom in 2.js"></script>
</head>
<body>
<div class="content">
<div class="left">
<img src="phonemin.jpg" alt="">
<span id="span"></span>
</div>
<div class="right">
<img src="phonebig.png" alt="" class="rightImg">
</div>
</div>
</body> That's how it works

Two 、js part
Steps and points for attention
1. use load Let all the code load , Re execution JS Code , You don't have to JS The code is written later
2、 When the mouse moves in and out left Box time ,span and right Show and hide ( That is to say, use left The reason for being the target )
3、 When the mouse is on left When moving in the box , Get him in left The coordinates of
e.pageX - left.offsetLeft: Mouse position in page - left The distance of the leftmost box in the page ( Because here left The parent element of the box is not located, so its offsetLeft Size is the distance between pages )
4、 Assign the above value to span Mask the box top and left ( because span It is made by positioning , So don't give extra )—— Mouse in left The position in the box is the same as span The upper left corner of the mask coincides
5、 The above is a little ugly , So let the position of the mouse in the box - span Half the size of the mask box , So the mouse is left Position in , Namely span In the middle .
Be careful : At this time, the position coordinate is still the position of the mouse in the box . Although his position has changed , But the size is subtracted from the above (e.pageX - left.offsetLeft - span.offsetWidth / 2 = Mouse position in box )
6、 use if else Statement limit span The location of the box , Don't let his mobile position exceed left The box
Be careful : If you simply consider If left<=0, Give Way left = 0; But at this time, his up and down can still go beyond the boundary !!
If you add : If left<=0 & top<=0 -->left = 0;top = 0; This will make left Will not cross the border ,top Will not cross the border , The remaining principles are similar
7、 The big box on the right follows the small box in the corresponding proportion
The distance the big box should go / The maximum distance a big box can travel = span The distance the box should go / span The maximum distance the box can go
---> The distance the big box should go = The maximum distance a big box can travel * span The distance the box should go / span The maximum distance the box can go
8、 Direct to right The image assignment in the box does not work , To give right The picture in the box is absolutely positioned
9、 to right When assigning values to pictures in the box , to shouldX and shouldY negative , It works better
10、 When the mouse is on left At the top left corner of the box , to right The position of the picture transmission in the box by top:0;left:0; In this way right The picture in the box is close to right The upper left corner of the box , such span When the box moves to the right again ,right The picture in the box will exceed .
Let the mouse in left At the top left corner , Give Way right The picture in the box is near the lower right corner , When moving in proportion , The image will not exceed right The box
window.addEventListener('load', function() {
var left = this.document.querySelector('.left');
var right = this.document.querySelector('.right');
var span = this.document.querySelector('span');
//1、 When the mouse moves in left Box time ,span and right Show
left.addEventListener('mouseover', function() {
span.style.display = 'inline-block';
right.style.display = 'block';
});
left.addEventListener('mouseout', function() {
span.style.display = 'none';
right.style.display = 'none';
});
// 2、 Give Way span The box can move with the mouse left Move while moving in
// (1) When the mouse moves, get the mouse in left Position in the box
left.addEventListener('mousemove', function(e) {
var x = e.pageX - left.offsetLeft;
var y = e.pageY - left.offsetTop;
// (2) hold x,y Assign a value to span The location of
// span.style.left = x + 'px';
// span.style.top = y + 'px';
// (3) Problems arise , The mouse is always span The top left corner of the box moves , Move him to span The middle of the box
// Translated a distance to the lower right corner , In general, it is the distance of the mouse in the box
var hleft = x - span.offsetWidth / 2;
var htop = y - span.offsetHeight / 2;
// span.style.left = hleft + 'px';
// span.style.top = htop + 'px';
// (4) Not allow span Box out left The boundary of the
if (hleft <= 0 & htop <= 0) {
hleft = 0;
htop = 0
} else if (hleft <= 0 & htop >= 200) {
hleft = 0;
htop = 200;
} else if (hleft >= 200 & htop <= 0) {
hleft = 200;
htop = 0;
} else if (hleft >= 200 & htop >= 200) {
hleft = 200;
htop = 200;
} else if (hleft <= 0) {
hleft = 0;
} else if (hleft >= 200) {
hleft = 200;
} else if (htop <= 0) {
htop = 0
} else if (htop >= 200) {
htop = 200;
}
console.log(hleft, htop);
span.style.left = hleft + 'px';
span.style.top = htop + 'px';
// 3、 Let the big box follow span The box moves the corresponding proportional distance
// 3.1 Maximum moving distance of large box
var rightImg = document.querySelector('.rightImg');
var bigMaxX = right.offsetWidth - rightImg.offsetWidth;
var bigMaxY = right.offsetHeight - rightImg.offsetHeight;
// right The distance the box should move = span Distance traveled * right Maximum moving distance / span Maximum moving distance
var shouldX = hleft * bigMaxX / (left.offsetWidth - span.offsetWidth);
var shouldY = htop * bigMaxY / (left.offsetHeight - span.offsetHeight);
console.log('shouldX ' + shouldX);
console.log('shouldY ' + shouldY);
// When the mouse is on left At the top left corner of the box , to right The position of the picture transmission in the box by top:0;left:0; In this way right The picture in the box is close to right The upper left corner of the box , such span When the box moves to the right again ,right The picture in the box will exceed .
// Let the mouse in left At the top left corner , Give Way right The picture in the box is near the lower right corner , When moving in proportion , The image will not exceed right The box
rightImg.style.left = -shouldX + 85 + 'px';
rightImg.style.top = -shouldY + 100 + 'px';
})
})边栏推荐
- Troubleshooting: kubectl reports an error validationerror: unknown field \u00a0
- UML class diagram
- 拦截器与过滤器的区别
- promise 和 Observable 的区别
- 在纽约寻找童真——新泽西州乐高乐园探索中心的美好一天
- Mysql高级篇学习总结7:Mysql数据结构-Hash索引、AVL树、B树、B+树的对比
- Looking for innocence in New York -- a beautiful day at the discovery center of Legoland, New Jersey
- #gStore-weekly | gStore源码解析(四):安全机制之黑白名单配置解析
- 鸿蒙第四次学习
- 【JVM调优实战100例】01——JVM的介绍与程序计数器
猜你喜欢

任职 22 年,PowerShell 之父将从微软离职:曾因开发 PowerShell 被微软降级过

拦截器与过滤器的区别

Distance measurement - Jaccard distance

LightGroupButton* sender = static_ cast<LightGroupButton*>(QObject::sender());

LightGroupButton* sender = static_cast<LightGroupButton*>(QObject::sender());

高频面试题

使用 Cheat Engine 修改 Kingdom Rush 中的金钱、生命、星

STM32G0 USB DFU 升级校验出错-2

Mysql高级篇学习总结7:Mysql数据结构-Hash索引、AVL树、B树、B+树的对比

Have you stepped on the nine common pits in the e-commerce system?
随机推荐
R语言使用epiDisplay包的cox.display函数获取cox回归模型汇总统计信息(风险率HR、调整风险率及其置信区间、模型系数的t检验的p值、Wald检验的p值和似然比检验的p值)、汇总统计
[daily question] the next day
Tips for material UV masking
Leetcode(81)——搜索旋转排序数组 II
R语言ggplot2可视化:gganimate包创建动态柱状图动画(gif)、使用transition_states函数在动画中沿给定维度逐步显示柱状图
科技公司不同人对Bug的反应 | 每日趣闻
Detailed explanation of cjson usage
《病人家属,请来一下》读书笔记
Responses of different people in technology companies to bugs | daily anecdotes
Stratégie touristique d'été de Singapour: un jour pour visiter l'île de San taosha à Singapour
【愚公系列】2022年07月 Go教学课程 001-Go语言前提简介
【JVM调优实战100例】02——虚拟机栈与本地方法栈调优五例
The difference between interceptor and filter
@Component 拿不到dao层
Mysql高级篇学习总结7:Mysql数据结构-Hash索引、AVL树、B树、B+树的对比
Progress-进度条
R language uses Cox of epidisplay package Display function obtains the summary statistical information of Cox regression model (risk rate HR, adjusted risk rate and its confidence interval, P value of
性能测试如何创造业务价值
Stretchdibits function
鸿蒙第四次学习