原生JS实现拖动拉开序幕特效

2021-12-14 19:30:20 浏览数 (1)

给大家分享一个用原生JS实现的拖动拉开序幕特效,效果如下:

以下是代码实现,欢迎大家复制粘贴和收藏。

代码语言:javascript复制
<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>原生JS实现拖动拉开序幕特效</title>
    <style>
        * {
            margin: 0;
            padding: 0;
            box-sizing: border-box;
        }

        body {
            display: flex;
            align-items: center;
            justify-content: center;
            min-height: 100vh;
            background: #151515;
        }

        div {
            position: relative;
        }

        #rangeValue {
            position: relative;
            display: block;
            font-size: 6em;
            color: rgba(0, 0, 0, 0.8);
            z-index: 2;
            text-align: center;
        }

        #rangeValue::after {
            content: '%';
        }

        #fillRangeValue {
            position: fixed;
            top: 0;
            left: 0;
            height: 100%;
            width: 0;
            background: #00adff;
            z-index: 1;
        }

        .range {
            position: relative;
            width: 400px;
            height: 15px;
            -webkit-appearance: none;
            background: rgba(0, 0, 0, 0.8);
            outline: none;
            border-radius: 15px;
            box-shadow: 0 0 0 2px #151515, inset 0 0 5px #000;
            z-index: 2;
            overflow: hidden;
        }

        .range::-webkit-slider-thumb {
            -webkit-appearance: none;
            width: 15px;
            height: 15px;
            border-radius: 50%;
            background: #00adff;
            border: 4px solid #222;
            z-index: 2;
            box-shadow: -407px 0 0 400px #00adff;
        }
    </style>
</head>

<body>
    <div>
        <h2 id="rangeValue"></h2>
        <div id="fillRangeValue"></div>
        <input type="range" class="range" value="0" min="0" max="100" onmousemove="rangeSlider(this.value)"
            onchange="rangeSlider(this.value)">
    </div>
    <script>
        function rangeSlider(value) {
            document.getElementById('rangeValue').innerHTML = value
            document.getElementById('fillRangeValue').style.width = `${value}%`
        }
    </script>
</body>

</html>

0 人点赞