带有 HTML 和 CSS 的进度条

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/7190898/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-29 10:16:59  来源:igfitidea点击:

Progress Bar with HTML and CSS

htmlcssprogress-bar

提问by Shahin

I want to create a progress bar like in the below image:

我想创建一个如下图所示的进度条:

Progress Bar Example

进度条示例

I have no idea about creating this. Should I use HTML5 techniques?

我不知道创建这个。我应该使用 HTML5 技术吗?

Would you please give me some help about creating this progress bar?

你能给我一些关于创建这个进度条的帮助吗?

回答by RoToRa

#progressbar {
  background-color: black;
  border-radius: 13px;
  /* (height of inner div) / 2 + padding */
  padding: 3px;
}

#progressbar>div {
  background-color: orange;
  width: 40%;
  /* Adjust with JavaScript */
  height: 20px;
  border-radius: 10px;
}
<div id="progressbar">
  <div></div>
</div>

Fiddle

小提琴

(EDIT: Changed Syntax highlight; changed descendant to child selector)

(编辑:更改语法突出显示;将后代更改为子选择器)

回答by Madara's Ghost

http://jsfiddle.net/cwZSW/1406/

http://jsfiddle.net/cwZSW/1406/

#progress {
    background: #333;
    border-radius: 13px;
    height: 20px;
    width: 300px;
    padding: 3px;
}

#progress:after {
    content: '';
    display: block;
    background: orange;
    width: 50%;
    height: 100%;
    border-radius: 9px;
}
<div id="progress"></div>

回答by Andrelo

2014 answer: Since 2014 HTML now 5 includes a <progress> elementthat does not need JavaScript. The percent value moves with the progress using inline content. Tested only in webkit. Hope it helps:

2014 年回答:自 2014 年以来,HTML 现在 5 包含<progress> element不需要 JavaScript 的。百分比值随着使用内联内容的进度而移动。仅在 webkit 中测试。希望能帮助到你:

jsFiddle

js小提琴

CSS:

CSS:

progress {
 display:inline-block;
 width:190px;
 height:20px;
 padding:15px 0 0 0;
 margin:0;
 background:none;
 border: 0;
 border-radius: 15px;
 text-align: left;
 position:relative;
 font-family: Arial, Helvetica, sans-serif;
 font-size: 0.8em;
}
progress::-webkit-progress-bar {
 height:11px;
 width:150px;
 margin:0 auto;
 background-color: #CCC;
 border-radius: 15px;
 box-shadow:0px 0px 6px #777 inset;
}
progress::-webkit-progress-value {
 display:inline-block;
 float:left;
 height:11px;
 margin:0px -10px 0 0;
 background: #F70;
 border-radius: 15px;
 box-shadow:0px 0px 6px #777 inset;
}
progress:after {
 margin:-26px 0 0 -7px;
 padding:0;
 display:inline-block;
 float:left;
 content: attr(value) '%';
}
<progress id="progressBar" max="100" value="77"></progress>

回答by Sanxofon

In modern browsers you could use a CSS3 & HTML5 progress Element!

在现代浏览器中,您可以使用 CSS3 和 HTML5 进度元素!

progress {
  width: 40%;
  display: block; /* default: inline-block */
  margin: 2em auto;
  padding: 3px;
  border: 0 none;
  background: #444;
  border-radius: 14px;
}
progress::-moz-progress-bar {
  border-radius: 12px;
  background: orange;

}
/* webkit */
@media screen and (-webkit-min-device-pixel-ratio:0) {
  progress {
    height: 25px;
  }
}
progress::-webkit-progress-bar {
    background: transparent;
}  
progress::-webkit-progress-value {  
  border-radius: 12px;
  background: orange;
} 
<progress max="100" value="40"></progress>

回答by DaJF

Same as @RoToRa's answer, with a some slight adjustments (correct colors and dimensions):

与@RoToRa 的回答相同,稍作调整(正确的颜色和尺寸):

body {
  background-color: #636363;
  padding: 1em;
}

#progressbar {
  background-color: #20201F;
  border-radius: 20px; /* (heightOfInnerDiv / 2) + padding */
  padding: 4px;
}

#progressbar>div {
  background-color: #F7901E;
  width: 48%;
  /* Adjust with JavaScript */
  height: 16px;
  border-radius: 10px;
}
<div id="progressbar">
  <div></div>
</div>

Here's the fiddle: jsFiddle

这是小提琴:jsFiddle

And here's what it looks like: jsFiddle-screenshot

这是它的样子: jsFiddle-截图

回答by Dani?l Tulp

I like this one:

我喜欢这个:

very slick with only this as HTML and the rest CSS3 that is backwards compatible (although it will have less eyecandy)

非常巧妙,只有这个作为 HTML,其余的 CSS3 是向后兼容的(尽管它会少一些吸引人的眼球)

EditAdded code below, but taken directly from the page above, all credit to that author

编辑在下面添加了代码,但直接取自上面的页面,所有功劳都归功于该作者

.meter {
  height: 20px;
  /* Can be anything */
  position: relative;
  background: #555;
  -moz-border-radius: 25px;
  -webkit-border-radius: 25px;
  border-radius: 25px;
  padding: 10px;
  -webkit-box-shadow: inset 0 -1px 1px rgba(255, 255, 255, 0.3);
  -moz-box-shadow: inset 0 -1px 1px rgba(255, 255, 255, 0.3);
  box-shadow: inset 0 -1px 1px rgba(255, 255, 255, 0.3);
}

.meter>span {
  display: block;
  height: 100%;
  -webkit-border-top-right-radius: 8px;
  -webkit-border-bottom-right-radius: 8px;
  -moz-border-radius-topright: 8px;
  -moz-border-radius-bottomright: 8px;
  border-top-right-radius: 8px;
  border-bottom-right-radius: 8px;
  -webkit-border-top-left-radius: 20px;
  -webkit-border-bottom-left-radius: 20px;
  -moz-border-radius-topleft: 20px;
  -moz-border-radius-bottomleft: 20px;
  border-top-left-radius: 20px;
  border-bottom-left-radius: 20px;
  background-color: #f1a165;
  background-image: -webkit-gradient(linear, left top, left bottom, color-stop(0, #f1a165), color-stop(1, #f36d0a));
  background-image: -webkit-linear-gradient(top, #f1a165, #f36d0a);
  background-image: -moz-linear-gradient(top, #f1a165, #f36d0a);
  background-image: -ms-linear-gradient(top, #f1a165, #f36d0a);
  background-image: -o-linear-gradient(top, #f1a165, #f36d0a);
  -webkit-box-shadow: inset 0 2px 9px rgba(255, 255, 255, 0.3), inset 0 -2px 6px rgba(0, 0, 0, 0.4);
  -moz-box-shadow: inset 0 2px 9px rgba(255, 255, 255, 0.3), inset 0 -2px 6px rgba(0, 0, 0, 0.4);
  position: relative;
  overflow: hidden;
}
<div class="meter">
  <span style="width: 33%"></span>
  <!-- I use my viewmodel in MVC to calculate the progress and then use @Model.progress to place it in my HTML with Razor -->
</div>

回答by James

Progress Bar without nested divs... for every element where the css linear-gradient works.

没有嵌套 div 的进度条......适用于 css 线性渐变工作的每个元素。

Here the JSFiddle http://jsfiddle.net/oj1L3y6t/2/

这里是 JSFiddle http://jsfiddle.net/oj1L3y6t/2/

function show_progress(i) {
  var progress1 = i;
  var progress2 = progress1 + 1;
  var progress3 = progress1 + 2;

  var magic = "linear-gradient(to right, #FFC2CE " + progress1 + "% ,red " + progress2 + "% , #FFFFFF " + progress3 + "%)";
  document.getElementById("progress-0").style.background = magic;

  var magic = "linear-gradient(to right, lightblue " + progress1 + "% , lightgreen " + progress2 + "%)";
  document.getElementById("progress-1").style.background = magic;

  var magic = "linear-gradient(to right, lightblue " + progress1 + "% , #FFFFFF 100%)";
  document.getElementById("progress-2").style.background = magic;

  var magic = "linear-gradient(#FFC2CE " + progress1 + "% ,red " + progress2 + "% , #FFFFFF " + progress3 + "%)";
  document.getElementById("progress-3").style.background = magic;
}

function timeout() {
  t = setTimeout(function() {
    show_progress(t)
    timeout();
  }, 50);
  if (t == 78) {
    clearTimeout(t);
  }
  console.log(t);
}

timeout();
#progress-0 {
  border: 1px solid black;
  width: 500px;
  background: #999;
  text-align: center;
}

#progress-1 {
  border: 1px solid black;
  width: 500px;
  background: #999;
  text-align: center;
  margin-top: 10px;
  border-radius: 10px;
}

#progress-2 {
  border: 1px solid black;
  width: 500px;
  background: #999;
  text-align: center;
  margin-top: 10px;
}

#progress-3 {
  border: 1px solid black;
  width: 100px;
  height: 100px;
  background: #999;
  line-height: 100px;
  text-align: center;
  margin-top: 10px;
  border-radius: 200px;
}
<div id="progress-0">Loading</div>
<input id="progress-1" value="Loading"></input>
<button id="progress-2">Loading</button>
<p id="progress-3">Loading</p>

回答by Veger

Create an element which shows the left part of the bar (the round part), also create an element for the right part. For the actual progress bar, create a third element with a repeating background and a width which depends on the actual progress. Put it all on top of the background image (containing the empty progress bar).

创建一个显示条形左侧部分(圆形部分)的元素,同时为右侧部分创建一个元素。对于实际进度条,创建具有重复背景和宽度的第三个元素,该宽度取决于实际进度。将其全部放在背景图像的顶部(包含空的进度条)。

But I suppose you already knew that...

但我想你已经知道了......

Edit: When creating a progress bar which do not use textual backgrounds. You can use the border-radiusto get the round effect, as shown by Rikudo Senninand RoToRa!

编辑:创建不使用文本背景的进度条时。您可以使用border-radius来获得圆形效果,如Rikudo SenninRoToRa 所示

回答by user2579594

.loading {
  position: relative;
  width: 50%;
  height: 200px;
  border: 1px solid rgba(160, 160, 164, 0.2);
  background-color: rgba(160, 160, 164, 0.2);
  border-radius: 3px;
}

span.loader {
  position: absolute;
  top: 40%;
  left: 10%;
  width: 250px;
  height: 20px;
  border-radius: 8px;
  border: 2px solid rgba(160, 160, 164, 0.8);
  padding: 0;
}

span.loader span.innerLoad {
  text-align: center;
  width: 140px;
  font-size: 15px;
  font-stretch: extra-expanded;
  color: #2A00FF;
  padding: 1px 18px 3px 80px;
  border-radius: 8px;
  background: rgb(250, 198, 149);
  background: -moz-linear-gradient(left, rgba(250, 198, 149, 1) 0%, rgba(245, 171, 102, 1) 47%, rgba(239, 141, 49, 1) 100%);
  background: -webkit-gradient(linear, left top, right top, color-stop(0%, rgba(250, 198, 149, 1)), color-stop(47%, rgba(245, 171, 102, 1)), color-stop(100%, rgba(239, 141, 49, 1)));
  background: -webkit-linear-gradient(left, rgba(250, 198, 149, 1) 0%, rgba(245, 171, 102, 1) 47%, rgba(239, 141, 49, 1) 100%);
  background: -o-linear-gradient(left, rgba(250, 198, 149, 1) 0%, rgba(245, 171, 102, 1) 47%, rgba(239, 141, 49, 1) 100%);
  background: -ms-linear-gradient(left, rgba(250, 198, 149, 1) 0%, rgba(245, 171, 102, 1) 47%, rgba(239, 141, 49, 1) 100%);
  background: linear-gradient(to right, rgba(250, 198, 149, 1) 0%, rgba(245, 171, 102, 1) 47%, rgba(239, 141, 49, 1) 100%);
  filter: progid:DXImageTransform.Microsoft.gradient( startColorstr='#fac695', endColorstr='#ef8d31', GradientType=1);
}
<div class="loading">
  <span class="loader">
      <span class="innerLoad">Loading...</span>
  </span>
</div>

回答by Mujahid Hussain

    .black-strip
{   width:100%;
    height: 30px;       
    background-color:black; 
}

.green-strip
{   width:0%;           
    height: 30px;       
    background-color:lime;
    animation-name: progress-bar;           
    animation-duration: 4s;  
    animation-iteration-count: infinite; 
}

@keyframes progress-bar { 
    from{width:0%} 
    to{width:100%} 
}

    <div class="black-strip">
        <div class="green-strip">
        </div>
   </div>