源码
<!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">
<title>Document</title>
<style>
body{
display: flex;
justify-content: center;
align-items: center;
min-height: 100vh;
}
.comparison{
width: 600px;
height: 400px;
position: relative;
overflow: hidden;
box-shadow: 0px 2px 6px rgba(0,0,0,.15);
border-radius: 4px;
}
.comparison::before,
.comparison::after{
content: "";
display: block;
width: inherit;
height: inherit;
/* 图片素材https://unsplash.com/photos/qiSGdBYWtSY */
background-image: url(https://images.unsplash.com/photo-1612142186636-3e65d8a446fe?ixlib=rb-1.2.1&ixid=MnwxMjA3fDB8MHxwaG90by1wYWdlfHx8fGVufDB8fHx8&auto=format&fit=crop&w=870&q=80);
background-size: cover;
position: absolute;
top: 0;
left: 0;
}
.comparison::before{
filter: grayscale(100%);
}
.comparison::after{
/* 为图片加入遮罩阴影 */
/*
clip-path:inset(上 右 下 左)
*/
clip-path: inset(0 0 0 calc(var(--slide)/10* 1%));
}
/* 进度条 */
.slider{
position: absolute;
top: 0;
left: 0;
-webkit-appearance: none;
appearance: none;
outline: none;
margin: 0;
background: transparent;
width: 100%;
height: 100%;
z-index: 100;
}
.slider::-webkit-slider-thumb{
-webkit-appearance: none;
appearance: none;
width: 10px;
height: 400px;
background: #000;
box-shadow: 0px 0px 6px rgba(0, 0, 0, .5);
border-radius: 2px;
cursor: ew-resize;
}
</style>
</head>
<body>
<div class="comparison" style="--slide: 500">
<!-- 当前的子的父亲改变子的变量值 1.-->
<input type="range" class="slider" min="1" max="1000" value="500"
oninput="sum()" >
<!-- 2.使用函数 -->
<input type="range" class="slider" min="1" max="1000" value="500"
oninput="this.parentNode.style.setProperty('--slide',`${this.value}`)" >
<!-- setProperty() 方法接口为一个声明了CSS样式的对象设置一个新的值 -->
</div>
<script>
var i = document.querySelector('.slider')
function sum(){
i.parentNode.style.setProperty('--slide',i.value)
}
</script>
</body>
</html>
效果图