效果展示

(拖动图片查看效果)

代码实现

1
2
3
4
5
6
7
8
9
10
11
<div class="box">
<div class="img down"></div>
<div class="img up"></div>
<input
class="progress"
name="progress"
type="range"
min="0"
max="100"
value="50" />
</div>
1
2
3
4
5
6
const progress = document.querySelector('.progress')
const dom = document.querySelector('.up')
progress.addEventListener('input', (e) => {
let position = e.target.value || 1
dom.style.width = `${position}%`
})
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
.box {
width: 600px;
height: 400px;
border-radius: 10px;
overflow: hidden;
position: relative;
}

.img {
position: absolute;
top: 0;
bottom: 0;
left: 0;
right: 0;
background-size: 600px 100%;
}

.down {
background-image: url(https://wade1999-images-1307558348.cos.ap-shanghai.myqcloud.com/202306251658818.png);
}

.up {
width: 50%;
background-image: url(https://wade1999-images-1307558348.cos.ap-shanghai.myqcloud.com/202306251658817.jpg);
}

.progress {
display: flex;
justify-content: center;
align-items: center;
position: absolute;
appearance: none;
width: 100%;
height: 100%;
background: rgba(255, 255, 255, 0);
outline: none;
margin: 0;
transition: all 0.3s;
}

.progress:hover {
background: rgba(255, 255, 255, 0.1);
}

.progress::-webkit-slider-thumb {
appearance: none;
width: 5px;
height: 400px;
background-color: rgba(0, 0, 0, 0);
cursor: ew-resize;
}