前端 button 组件之按钮式进度条实现 完整js+css+html

1、实现效果

在这里插入图片描述

2、html

<div class="button">
    <i class="fa fa-arrow-down" aria-hidden="true"></i>
    <span class="text">download</span>
    <span class="progress"></span>
</div>

3、css

 body,
        html {
    
    
            background-color: pink;
        }

        .button {
    
    
            -webkit-user-select: none;
            -moz-user-select: none;
            -ms-user-select: none;
            user-select: none;
            position: relative;
            display: flex;
            align-items: center;
            justify-content: center;
            width: 160px;
            height: 40px;
            color: black;
            background: white;
            border-radius: 10px;
            margin: 100px;
            font-size: 18px;
            text-decoration: none;
            overflow: hidden;
        }

        .progress {
    
    
            content: '';
            position: absolute;
            top: 90%;
            left: -100%;
            width: 100%;
            height: 100%;
            background: #4776E6;
            background: -webkit-linear-gradient(to right, #8E54E9, #4776E6);
            background: linear-gradient(to right, #8E54E9, #4776E6);
            transition: all 4s;
        }

        .text {
    
    
            z-index: 10;
        }

        i {
    
    
            margin: 0 8px 0 0;
            font-size: 16px;
            z-index: 10;
        }

        @keyframes tapDownload {
    
    
            0% {
    
    
                transform: translateY(2px);
            }

            100% {
    
    
                transform: translateY(0);
            }
        }

        .fa-arrow-down {
    
    
            animation: tapDownload 1s ease infinite;
        }

        @keyframes downloading {
    
    
            0% {
    
    
                transform: scale(.7);
            }

            100% {
    
    
                transform: scale(1);
            }
        }

        .fa-download {
    
    
            animation: downloading 1s ease infinite alternate-reverse;
        }

4、js

 const icon = document.querySelectorAll('.fa')[0];
const btn1 = document.querySelectorAll('.button')[0];
const pr = document.querySelectorAll('.progress')[0];
const text = document.querySelectorAll('.text')[0];
btn1.addEventListener('click', () => {
    
    
    pr.style.left = '0';
    icon.classList.remove('fa-arrow-down');
    icon.classList.add('fa-download');
    text.innerText = 'downloading';
    setTimeout(() => {
    
    
        pr.style.top = '0';
        pr.style.transitionDuration = '1s';
        text.style.color = 'white';
        text.innerText = 'downloaded';
        icon.style.color = 'white';
        icon.classList.remove('fa-download');
        icon.classList.add('fa-check');
    }, 4000);
});

5、icon引入

<link href="https://cdn.bootcdn.net/ajax/libs/font-awesome/5.15.3/css/all.css" rel="stylesheet">

6、完整html文件

<!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>前端 button 组件之按钮式进度条实现</title>
    <link href="https://cdn.bootcdn.net/ajax/libs/font-awesome/5.15.3/css/all.css" rel="stylesheet">
    <style>
        body,
        html {
      
      
            background-color: pink;
        }

        .button {
      
      
            -webkit-user-select: none;
            -moz-user-select: none;
            -ms-user-select: none;
            user-select: none;
            position: relative;
            display: flex;
            align-items: center;
            justify-content: center;
            width: 160px;
            height: 40px;
            color: black;
            background: white;
            border-radius: 10px;
            margin: 100px;
            font-size: 18px;
            text-decoration: none;
            overflow: hidden;
        }

        .progress {
      
      
            content: '';
            position: absolute;
            top: 90%;
            left: -100%;
            width: 100%;
            height: 100%;
            background: #4776E6;
            background: -webkit-linear-gradient(to right, #8E54E9, #4776E6);
            background: linear-gradient(to right, #8E54E9, #4776E6);
            transition: all 4s;
        }

        .text {
      
      
            z-index: 10;
        }

        i {
      
      
            margin: 0 8px 0 0;
            font-size: 16px;
            z-index: 10;
        }

        @keyframes tapDownload {
      
      
            0% {
      
      
                transform: translateY(2px);
            }

            100% {
      
      
                transform: translateY(0);
            }
        }

        .fa-arrow-down {
      
      
            animation: tapDownload 1s ease infinite;
        }

        @keyframes downloading {
      
      
            0% {
      
      
                transform: scale(.7);
            }

            100% {
      
      
                transform: scale(1);
            }
        }

        .fa-download {
      
      
            animation: downloading 1s ease infinite alternate-reverse;
        }
    </style>
</head>

<body>
    <div class="button">
        <i class="fa fa-arrow-down" aria-hidden="true"></i>
        <span class="text">download</span>
        <span class="progress"></span>
    </div>
    <script>
        const icon = document.querySelectorAll('.fa')[0];
        const btn1 = document.querySelectorAll('.button')[0];
        const pr = document.querySelectorAll('.progress')[0];
        const text = document.querySelectorAll('.text')[0];
        btn1.addEventListener('click', () => {
      
      
            pr.style.left = '0';
            icon.classList.remove('fa-arrow-down');
            icon.classList.add('fa-download');
            text.innerText = 'downloading';
            setTimeout(() => {
      
      
                pr.style.top = '0';
                pr.style.transitionDuration = '1s';
                text.style.color = 'white';
                text.innerText = 'downloaded';
                icon.style.color = 'white';
                icon.classList.remove('fa-download');
                icon.classList.add('fa-check');
            }, 4000);
        });
    </script>
</body>

</html>

7、转载链接点这里

猜你喜欢

转载自blog.csdn.net/qiaoqiaohong/article/details/120021727