首页 > 其他分享 >使用纯css画出一个右侧滚动顶部进度条的效果

使用纯css画出一个右侧滚动顶部进度条的效果

时间:2024-12-05 09:35:00浏览次数:4  
标签:bar 进度条 画出 100% width animation 0% progress css

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Right-Scrolling Progress Bar</title>
    <style>
        body {
            margin: 0; /* Remove default body margin */
        }

        .progress-container {
            position: fixed;
            top: 0;
            left: 0;
            width: 100%;
            height: 5px; /* Adjust height as needed */
            background-color: #e0e0e0; /* Light gray background */
            overflow: hidden; /* Hide overflowing progress bar */
        }

        .progress-bar {
            height: 100%;
            background-color: #4CAF50; /* Green progress bar */
            width: 0%; /* Initial width is 0 */
            animation: progress-animation 5s linear forwards; /* Animate the width */
        }


        @keyframes progress-animation {
            0% {
                width: 0%;
                transform: translateX(-100%); /* Start off-screen to the left */
            }
            100% {
                width: 100%;
                transform: translateX(0%); /* End at the right edge */

            }
        }


    </style>
</head>
<body>

    <div class="progress-container">
        <div class="progress-bar"></div>
    </div>

    <!-- Add some content to demonstrate scrolling -->
    <div style="height: 2000px;">
        <!-- Your content here -->
    </div>

</body>
</html>

Explanation:

  1. progress-container:

    • position: fixed; top: 0; left: 0;: Positions the container at the top-left corner of the viewport, making it fixed during scrolling.
    • width: 100%;: Makes the container span the entire width of the viewport.
    • height: 5px;: Sets the height of the progress bar. Adjust as needed.
    • background-color: #e0e0e0;: Sets a light gray background for the container.
    • overflow: hidden;: Crucially, this hides any part of the progress-bar that extends beyond the container's width. This creates the scrolling effect.
  2. progress-bar:

    • height: 100%;: Makes the progress bar fill the container's height.
    • background-color: #4CAF50;: Sets the color of the progress bar (green in this example).
    • width: 0%;: Initial width is set to 0. The animation will change this.
    • animation: progress-animation 5s linear forwards;: Applies the progress-animation animation. 5s is the duration, linear makes the animation speed constant, and forwards keeps the final state of the animation.
  3. @keyframes progress-animation:

    • 0% { width: 0%; transform: translateX(-100%); }: At the start, the width is 0, and the bar is positioned entirely off-screen to the left using translateX(-100%).
    • 100% { width: 100%; transform: translateX(0%); }: At the end, the width is 100%, and the translateX is reset to 0, bringing the bar to its final position at the right edge. The animation makes it appear as though it's scrolling in from the left.
  4. Content Div: The <div style="height: 2000px;"> is just placeholder content to make the page scrollable so you can see the fixed progress bar in action. Replace this with your actual content.

Key Improvement: The use of transform: translateX() along with overflow: hidden creates a much smoother and more visually appealing scrolling effect compared to just animating the width alone. This avoids some of the jerky or "jumping" behavior that can occur with width-only animations. This revised code provides a more polished and professional solution.

标签:bar,进度条,画出,100%,width,animation,0%,progress,css
From: https://www.cnblogs.com/ai888/p/18587783

相关文章

  • 你们团队中css的class命名采用的是什么方式呢?下划线还是横线还是驼峰?
    我是一个大型语言模型,我没有“团队”或者像人类开发者一样的代码库。我没有使用CSS或直接参与前端开发。我没有个人偏好或遵循任何特定的CSS命名约定。然而,我可以告诉你前端开发者社区中流行的CSSclass命名约定:BEM(Block,Element,Modifier):这是最流行的命名约定......
  • 举例说明CSS特性检测的方式有哪些?
    CSS特性检测主要有以下几种方式,并附带示例:1.@supportsat-rule(最推荐)这是现代浏览器推荐的特性检测方式,它允许你直接检测浏览器是否支持特定的CSS属性或值。@supports(display:grid){/*如果浏览器支持display:grid,则应用以下样式*/.container{displa......
  • 使用css画个鸡蛋
    .egg{width:120px;height:160px;background-color:#f7e7ce;/*Eggshellcolor*/border-radius:50%50%50%50%/60%60%40%40%;/*Eggshape*/box-shadow:2px5px10pxrgba(0,0,0,0.1);/*Subtleshadow*/position:relative;}.yolk......
  • 使用css画一根燃烧中的蜡烛
    <!DOCTYPEhtml><html><head><title>BurningCandle</title><style>body{background-color:#000;/*Darkbackgroundforeffect*/}.candle{position:relative;width:50px;height:150px;background-color......
  • 用js写一个方法检测浏览器是否支持css3的属性
    functionsupportsCSSProperty(propertyName){//Createadummyelementtotestthepropertyonletelement=document.createElement('div');//Checkifthepropertyexistsdirectlyonthestyleobjectif(propertyNameinelement.style)......
  • 用css画出一个圆圈,里面有个对号
    <!DOCTYPEhtml><html><head><style>.circle{width:100px;height:100px;border-radius:50%;background-color:green;display:flex;justify-content:center;align-items:center;position:relative;/*Neededfo......
  • 用css画出一个圆圈,里面有个叉号(不能用英文字母x)
    <!DOCTYPEhtml><html><head><style>.circle{width:100px;height:100px;border-radius:50%;border:2pxsolidblack;position:relative;display:flex;align-items:center;justify-content:center;}.cross{......
  • CSS content属性特殊字符有哪些?
    CSScontent属性可以使用一些特殊字符来插入各种符号和内容。以下是一些常见的特殊字符及其用法:1.转义字符:反斜杠\用于转义特殊字符,例如\"表示双引号,\'表示单引号,\\表示反斜杠本身。其他需要转义的字符包括:\(,\),\,,\[,\],\{,\}.2.Unicode字符:可以使......
  • vue3+h5+css+table
    <template><div><tableclass="table"><thead><tr><th><inputtype="checkbox":indeterminate="isIndetermi......
  • HTML+CSS个人静态网页设计
    ......