欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

CSS 3.0结合JS实现一个酷炫的滚动条

程序员文章站 2022-05-30 18:30:18
...

 给大家分享一个用CSS 3.0结合JS实现的酷炫的滚动条,效果如下:

CSS 3.0结合JS实现一个酷炫的滚动条

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

<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>CSS 3.0结合JS实现一个酷炫的滚动条</title>
    <style>
        * {
            margin: 0;
            padding: 0;
            box-sizing: border-box;
            font-family: '微软雅黑', sans-serif;
        }

        section {
            padding: 50%px;
            background: #000;
        }

        section h2 {
            font-size: 2.5em;
            color: #fff;
        }

        section p {
            font-size: 1.2em;
            color: #fff;
        }

        ::-webkit-scrollbar {
            width: 0;
        }

        #scrollPath {
            position: fixed;
            top: 0;
            right: 0;
            width: 10px;
            height: 100%;
            background: rgba(255, 255, 255, 0.05);
        }

        #progressBar{
            position: fixed;
            top: 0;
            right: 0;
            width: 10px;
            background: linear-gradient(to top, #008aff, #00ffe7);
            animation: animate 5s linear infinite;
        }

        @keyframes animate {

            0%,
            100% {
                filter: hue-rotate(0deg);
            }

            50% {
                filter: hue-rotate(360deg);
            }
        }

        #progressBar::before {
            content: '';
            position: absolute;
            top: 0;
            left: 0;
            width: 100%;
            height: 100%;
            background: linear-gradient(to top, #008aff, #00ffe7);
            filter: blur(10px);
        }

        #progressBar::after {
            content: '';
            position: absolute;
            top: 0;
            left: 0;
            width: 100%;
            height: 100%;
            background: linear-gradient(to top, #008aff, #00ffe7);
            filter: blur(30px);
        }
    </style>
</head>

<body>
    <div id="progressBar"></div>
    <div id="scrollPath"></div>
    <section>
        <p id="content" style="padding:20px">
            晚饭后,爸爸带着我和弟弟到八佰伴广场闲逛。哇!今天商场有活动,里面人山人海,舞台上灯火辉煌,有许多穿着五颜六色衣服的小朋友在舞台上载歌载舞,我立刻被吸引了过去,看得可入迷了。
        </p>
    </section>
    <script>

        // 多加点内容
        let content = document.getElementById('content');
        let text = content.innerText;
        for(let i =0;i<200;i++){
            content.innerText += text;
        };

        // 滚动时修改进度条
        let progress = document.getElementById('progressBar');
        let totalHeight = document.body.scrollHeight - window.innerHeight
        window.onscroll = function () {
            let progressHeight = (window.pageYOffset / totalHeight) * 100
            progress.style.height = progressHeight + '%'
        }
    </script>
</body>

</html>

 

相关标签: CSS