-
Notifications
You must be signed in to change notification settings - Fork 14
/
003.滑块拼图验证.html
168 lines (147 loc) · 5.29 KB
/
003.滑块拼图验证.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
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<link rel="stylesheet" href="./assets/global.css">
<style>
body {
display: flex;
justify-content: center;
align-items: center;
min-height: 100vh;
}
#captcha {
display: block;
width: var(--width);
height: var(--height);
border-radius: 4px;
background-image: url(https://dss1.baidu.com/6ONXsjip0QIZ8tyhnq/it/u=2676921178,3792372773&fm=55&app=54&f=JPEG?w=1680&h=630);
background-size: cover;
background-position: center;
box-shadow: 0 2px 4px rgba(0, 0, 0, .3);
position: relative;
box-sizing: border-box;
}
#captcha::before,
#captcha::after {
position: absolute;
content: "";
display: block;
width: inherit;
height: inherit;
background-image: inherit;
background-size: inherit;
background-position: inherit;
clip-path: var(--clip-path);
--webkit-clip-path: var(--clip-path);
}
#captcha::after {
transform: translateX(calc(var(--clip-offsetX) * -1 + var(--moved)));
transition: .25s all ease-in-out;
}
#captcha::before {
background-color: rgba(0, 0, 0, .5);
background-blend-mode: multiply;
}
#captcha:active #handle span,
#captcha:active::after {
transition: none;
}
#captcha #handle {
width: calc(var(--width) - (3px * 2));
height: 30px;
border-radius: 18px;
background-color: #eee;
position: absolute;
bottom: -50px;
left: 0;
box-shadow: inset 0 0 12px rgba(0, 0, 0, .2);
border: 3px solid #ccc;
}
#handle span {
display: block;
width: var(--puzzle-width);
height: inherit;
border-radius: inherit;
background-color: #fff;
box-shadow: inset 0 0 6px rgba(0, 0, 0, .25), 0 2px 4px rgba(0, 0, 0, .3);
position: absolute;
cursor: move;
transform: translateX(var(--moved));
transition: .25s all ease-in-out;
}
#captcha.passed #handle,
#captcha.passed::after,
#captcha.passed::before {
opacity: 0;
}
</style>
</head>
<body>
<div id="captcha">
<div id="handle">
<span></span>
</div>
</div>
<script>
let width = 400; // 宽度
let height = 260; // 高度
let puzzleWidth = 80; // 切图宽
let puzzleHeight = 80; // 切图高
let moved = 0; // 移动位置
let mpe = 5; // 允许最大误差
let offsetX = 6; // 边距值
let offsetY = 0; // 边距值
let shouldMove = false;
let clipRectX1 = randomRange(puzzleWidth + offsetX, width - puzzleWidth - offsetX);
let clipRectX2 = clipRectX1 + puzzleWidth;
let clipRectY1 = randomRange(puzzleHeight + offsetX, height - puzzleHeight - offsetY);
let clipRectY2 = clipRectY1 + puzzleHeight;
let captcha = document.querySelector('#captcha');
let handle = document.querySelector('#handle');
let button = document.querySelector('#handle span');
button.addEventListener("mousedown", (e) => {
shouldMove = true;
})
window.addEventListener("mousemove", (e) => {
if (shouldMove) {
const offsetLeft = handle.getBoundingClientRect().left;
const buttonWidth = button.getBoundingClientRect().width;
moved = e.clientX - offsetLeft - buttonWidth / 2;
render();
}
})
window.addEventListener("mouseup", (e) => {
if (shouldMove) {
const finalOffset = e.clientX - handle.getBoundingClientRect().left - puzzleWidth / 2;
if (Math.abs(finalOffset - clipRectX1) < mpe) captcha.classList.add('passed')
else moved = 0;
render();
shouldMove = false;
}
})
/**
* 超出省略
*/
function clamp(num, a, b) {
return Math.max(Math.min(num, Math.max(a, b)), Math.min(a, b))
}
/**
* 随机数字
*/
function randomRange(min, max) {
return Math.round(Math.random() * (max - min)) + min
}
/**
* 渲染
*/
function render() {
let clipPath = `polygon( ${clipRectX1}px ${clipRectY1}px, ${clipRectX2}px ${clipRectY1}px, ${clipRectX2}px ${clipRectY2}px, ${clipRectX1}px ${clipRectY2}px )`;
captcha.style = `--clip-offsetX:${clipRectX1}px;--moved:${clamp(moved, 0, width - puzzleWidth - offsetX)}px;--clip-path:${clipPath};--width:${width}px;--height:${height}px;--puzzle-width:${puzzleWidth}px;--puzzle-height:${puzzleHeight}px`;
}
render();
</script>
</body>
</html>