-
Notifications
You must be signed in to change notification settings - Fork 39
/
Copy path放大镜.html
137 lines (129 loc) · 4.09 KB
/
放大镜.html
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8" />
<style type="text/css">
* {
margin: 0;
padding: 0;
}
.normal {
width: 300px;
height: 300px;
position: relative;
}
.normal > img {
width: 100%;
height: 100%;
}
.lay {
width: 150px;
height: 150px;
background: orange;
opacity: 0.5;
position: absolute;
left: 0;
top: 0;
cursor: move;
display: none;
}
.big {
width: 300px;
height: 300px;
position: absolute;
left: 350px;
top: 0px;
overflow: hidden;
display: none;
}
.big > img {
width: 600px;
height: 600px;
position: absolute;
display: block;
}
</style>
</head>
<body>
<section id="magnifier"></section>
<script type="text/javascript">
var magnifier = {
init(param) {
const el = param.el;
const src = param.src;
if (!el || !src) return;
this.createElement(el, src);
},
createElement(el, src) {
const _this = this;
const normalDiv = document.createElement('div');
normalDiv.className = 'normal';
const normalImg = document.createElement('img');
normalImg.src = src.normal;
const normalSpan = document.createElement('span');
normalSpan.className = 'lay';
normalDiv.appendChild(normalImg);
normalDiv.appendChild(normalSpan);
const bigDiv = document.createElement('div');
bigDiv.className = 'big';
const bigImg = document.createElement('img');
bigImg.src = src.big;
bigDiv.appendChild(bigImg);
el.appendChild(normalDiv);
el.appendChild(bigDiv);
normalDiv.addEventListener('mouseover', () => {
// TODO: 放大镜、被放大的图片都显示,渲染为块级元素
normalSpan.style.display = 'block';
bigDiv.style.display = 'block';
});
normalDiv.addEventListener('mouseout', () => {
// TODO: 放大镜、被放大的图片都隐藏,不渲染
normalSpan.style.display = 'none';
bigDiv.style.display = 'none';
});
normalDiv.addEventListener('mousemove', _moveHandler);
function _moveHandler(event) {
event = event || window.event;
_this.moveHandler(event, normalDiv, normalSpan, bigImg);
}
},
moveHandler(event, normalDiv, normalSpan, bigImg) {
// tip: 该函数处理放大镜、被放大的图片的显示区域,x、y分别为鼠标在该模块中的位置坐标
const scale = 2;
let x = event.clientX - normalSpan.offsetWidth / 2;
let y = event.clientY - normalSpan.offsetHeight / 2;
// TODO: 判断临界值,重新设置放大镜、被放大的图片的位置
let xMin = 0;
let yMin = 0;
let xMax = normalDiv.offsetWidth - normalSpan.offsetWidth;
let yMax = normalDiv.offsetWidth - normalSpan.offsetWidth;
if (x < xMin) {
x = xMin;
} else if (x > xMax) {
x = xMax;
}
if (y < yMin) {
y = yMin;
} else if (y > yMax) {
y = yMax;
}
// console.log(x, y);
// console.log(normalSpan);
normalSpan.style.left = x + 'px';
normalSpan.style.top = y + 'px';
bigImg.style.left = -x * 2 + 'px';
bigImg.style.top = -y * 2 + 'px';
},
};
magnifier.init({
// TODO: 请获取id=magnifier的节点
el: document.getElementById('magnifier'),
src: {
normal:
'https://uploadfiles.nowcoder.com/images/20211201/920662346_1638327818015/FE8B1A979ADF6E3C2C114AF3F9CA693C',
big: 'https://uploadfiles.nowcoder.com/images/20211201/920662346_1638327818015/FE8B1A979ADF6E3C2C114AF3F9CA693C',
},
});
</script>
</body>
</html>