前端:拉动进度条,出现图片的前后对比效果

源码

<!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>

效果图

在这里插入图片描述
在这里插入图片描述

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值