【问题标题】:How to show blocks for progressbar using CSS如何使用 CSS 显示进度条的块
【发布时间】:2013-08-16 16:32:54
【问题描述】:

我的页面上有一个宽度为 100% 的 div。 现在我需要在<div> 中显示块,就像 windows 进度条一样,谁能告诉我如何在我的 div 中创建这些块?

看起来像这样:

【问题讨论】:

  • 有多种方法可以实现这一目标,但您已经尝试过什么?
  • 你也可以使用progress标签..jsfiddle.net/RpGwL
  • 是的,我可以使用进度标签。但我想在任何浏览器的 div 标签中显示 xp 风格的进度条块,并且可以使用任何操作系统。因为那个背景图片是最好的解决方案。
  • 你的想法需要js。纯 CSS 是不可能的,因为 CSS 仅用于样式设置,例如图像填充 div 的百分比。

标签: html css


【解决方案1】:

我复制了@Anshuman Dwibhashi 的答案,但我将背景更改为您发布的一张图片。现在您只需增加或减少.sub-block 的百分比宽度即可更改加载条进度。

<div class="main" style="border:solid;background-color:white;width:500px;height:25px;">
    <div class="sub-block" style="background:url('http://i.imgur.com/PRBmb4s.png');width:30%;height:25px;" ></div>
</div>

【讨论】:

  • 您的代码会显示半块,XP 不会这样做。对 OP 帮助不大
  • 但是只有当你知道一整块会被改变时,你仍然可以增加进度。
【解决方案2】:

这样

DEMO

CSS

  .progress-striped .bar {
    background-color: #149BDF;
    background-image: linear-gradient(45deg, rgba(255, 255, 255, 0.15) 25%, transparent 25%, transparent 50%, rgba(255, 255, 255, 0.15) 50%, rgba(255, 255, 255, 0.15) 75%, transparent 75%, transparent);
    background-size: 40px 40px;
}
.progress .bar {
    -moz-box-sizing: border-box;
    background-color: #0E90D2;
    background-image: linear-gradient(to bottom, #149BDF, #0480BE);
    background-repeat: repeat-x;
    box-shadow: 0 -1px 0 rgba(0, 0, 0, 0.15) inset;
    color: #FFFFFF;
    float: left;
    font-size: 12px;
    height: 100%;
    text-align: center;
    text-shadow: 0 -1px 0 rgba(0, 0, 0, 0.25);
    transition: width 0.6s ease 0s;
    width: 0;
}
.progress {
    background-color: #F7F7F7;
    background-image: linear-gradient(to bottom, #F5F5F5, #F9F9F9);
    background-repeat: repeat-x;
    border-radius: 4px 4px 4px 4px;
    box-shadow: 0 1px 2px rgba(0, 0, 0, 0.1) inset;
    height: 20px;
    margin-bottom: 20px;
    overflow: hidden;
}
.progress-striped .bar {
    background-color: #149BDF;
    background-image: linear-gradient(45deg, rgba(255, 255, 255, 0.15) 25%, transparent 25%, transparent 50%, rgba(255, 255, 255, 0.15) 50%, rgba(255, 255, 255, 0.15) 75%, transparent 75%, transparent);
    background-size: 40px 40px;
}
.progress-success.progress-striped .bar, .progress-striped .bar-success {
    background-color: #62C462;
    background-image: linear-gradient(45deg, rgba(255, 255, 255, 0.15) 25%, transparent 25%, transparent 50%, rgba(255, 255, 255, 0.15) 50%, rgba(255, 255, 255, 0.15) 75%, transparent 75%, transparent);
}

【讨论】:

  • 你应该附上一张图片,那东西很漂亮。
  • @Fags 你的进度条是从 Bootstrap 引用的,你应该转发一个指向 Bootstrap 页面的链接! getbootstrap.com
【解决方案3】:

试试这个:

DEMO

CSS

.main {
    border: solid;
    background-color: white;
    width: 500px;
    height: 25px;
}
.sub-block {
    background-color: green;
    display:inline-block;
    width: 20px;
    height: 25px;
}

HTML

<div class="main" >
<div class="sub-block" ></div>
</div>

根据需要添加更多的子块;

【讨论】:

    【解决方案4】:

    创建该块的图像文件并将其用作进度内部元素的背景。给背景重复-x。

    例如:

     background: url("Block.png") left top repeat-x transparent;
    

    进一步阅读:

    【讨论】:

    • 我认为 OP 的问题是他是否可以使用 CSS 显示进度,以块显示。我认为仅使用 CSS 是不可能的。它需要 JS。
    • @DanielCheung 谢谢你的回复,但我相信问题是如何自己创建块,而不是如何操作它们
    【解决方案5】:

    在您的 HTML 中

    <div class="block-container">
    <div class="sub-block" ></div>
    <div class="sub-block" ></div>
    <div class="sub-block" ></div>
    .
    .
    .
    </div>
    

    在您的 css 文件中

    .block-container
    {
    background-color:white;
    width: 500px;
    height: 25px;
    }
    
    .sub-block {
    background-color: green;
    width: 20px;
    height: 25px;
    padding-left: 3px;
    }
    

    或

    <div class='fix size'>
    <div style="width:100%;">
    <div>
    10 sub-div 10% each
    </div>
    </div>
    </div> 
    

    【讨论】:

    • 我添加了填充。并建议保持样式隔离,因为内联 css 不是一个好习惯,并且也给出了第二选择。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-03-24
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多