Home  >  Article  >  Web Front-end  >  Progress bar that Bootstrap must learn every day_javascript skills

Progress bar that Bootstrap must learn every day_javascript skills

PHPz
PHPzOriginal
2016-05-16 15:29:062248browse

1. Progress bar

In web pages, the effects of progress bars are not uncommon, such as a scoring system, such as loading status, etc.

The progress bar is the same as other independent components. Developers can choose the corresponding version according to their own needs:

☑ LESS version: Source code File progress-bars.less

☑ Sass version: Source file _progress-bars.scss

☑ Compile Later version: Line 4500~Line 4575 of bootstrap.css file

And the Bootstrap framework provides you with various styles of progress bars for everyone to use.

2. Progress bar – basic style

The Bootstrap framework provides a basic style for the progress bar, a background color of 100% width, and a highlighted color to indicate the completion progress. In fact, it is very easy to make such a progress bar. Generally, two containers are used. The outer container has a certain width and sets a background color. Its child elements set a width, for example, the completion degree is 30% (that is, the width of the parent container). scale value) and set a highlight background color to it.

1) How to use:

The Bootstrap framework is also implemented in this way. It provides two containers, the outer container Use the "progress" style, and subcontainers use the "progress-bar" style. Among them, progress is used to set the container style of the progress bar, and progress-bar is used to limit the progress of the progress bar. The method of use is very simple:

2), implementation principle

As mentioned before, such a basic progress bar is mainly divided into two parts:

The progress style mainly sets the background color, container height, spacing, etc. of the progress bar container:

/bootstrap.css file line 4516~line 4524/

.progress {
 height: 20px;
 margin-bottom: 20px;
 overflow: hidden;
 background-color: #f5f5f5;
 border-radius: 4px;
 -webkit-box-shadow: inset 0 1px 2px rgba(0, 0, 0, .1);
  box-shadow: inset 0 1px 2px rgba(0, 0, 0, .1);
}

While the progress-bar style sets the progress direction, it is important to set the background color and transition effect of the progress bar:

/lines 4525~4538 of the bootstrap.css file/

.progress-bar {
 float: left;
 width: 0;
 height: 100%;
 font-size: 12px;
 line-height: 20px;
 color: #fff;
 text-align: center;
 background-color: #428bca;
 -webkit-box-shadow: inset 0 -1px 0 rgba(0, 0, 0, .15);
  box-shadow: inset 0 -1px 0 rgba(0, 0, 0, .15);
 -webkit-transition: width .6s ease;
  transition: width .6s ease;
}

3), structural optimization:

Although this achieves the basic progress bar effect, it is a bit difficult for disabled people to browse the web. , so we can make the structure better (more semantically friendly):

40% Complete

1>, role attribute function: tell the search engine that the function of this div is a progress bar .

2>, aria-valuenow="40" attribute effect: the current progress bar progress is 40%.

3>, aria-valuemin="0" attribute function: the minimum value of the progress bar is 0%.

4>, aria-valuemax="100" attribute function: the maximum value of the progress bar is 100%.

3. Progress bar – colored progress bar

The progress bar in the Bootstrap framework is the same as the warning message box. In order to give the user a better experience, it is also based on Different states are configured with different progress bar colors. It is called colored progress bar here, which mainly includes the following four types:

☑ progress-bar-info: represents the information progress bar, and the color of the progress bar is blue Color

☑ progress-bar-success: indicates a successful progress bar, and the color of the progress bar is green

☑ progress -bar-warning: indicates a warning progress bar, the color of the progress bar is yellow

☑ progress-bar-danger: indicates an error progress bar , the color of the progress bar is red

1) How to use:

The specific use is very simple, you only need to follow the basic progress Add the corresponding class name.

2), implementation principle:

Compared with the basic progress bar, the color progress bar has certain changes in the color of the progress bar. The corresponding style code is as follows:

/bootstrap.css file line 4548 ~ line 4550/

.progress-bar-success {
 background-color: #5cb85c;
}
/*bootstrap.css文件第4555行~第4557行*/
.progress-bar-info {
 background-color: #5bc0de;
}
/*bootstrap.css文件第4562行~第4564行*/
.progress-bar-warning {
 background-color: #f0ad4e;
}
/*bootstrap.css文件第4569行~第4571行*/
.progress-bar-danger {
 background-color: #d9534f;
}

4. Progress bar – stripes Progress bar

In addition to providing a colored progress bar, the Bootstrap framework also provides a striped progress bar. This striped progress bar is implemented using CSS3 linear gradients without any help. picture. To use the striped progress bar in the Bootstrap framework, you only need to add the class name "progress-striped" to the container "progress" of the progress bar. Of course, if you want your progress bar stripes to have a color effect like colored progress, you You only need to add the corresponding color class name to the progress bar, as mentioned in the previous color progress bar.

Let’s take a look at the structure of making a striped progress bar: ,

1), original implementation:

As mentioned before, implementing stripes The progress bar mainly uses the linear gradient of CSS3. The specific code is as follows:

/bootstrap.css file line 4539~line 4547/

.progress-striped .progress-bar {
 background-image: -webkit-linear-gradient(45deg, rgba(255, 255, 255, .15) 25%, transparent 25%, transparent 50%, rgba(255, 255, 255, .15) 50%, rgba(255, 255, 255, .15) 75%, transparent 75%, transparent);
 background-image:linear-gradient(45deg, rgba(255, 255, 255, .15) 25%, transparent 25%, transparent 50%, rgba(255, 255, 255, .15) 50%, rgba(255, 255, 255, .15) 75%, transparent 75%, transparent);
 background-size: 40px 40px;
}

同样的,条纹进度条对应的每种状态也有不同的颜色,使用方法与彩色进度条一样。只是样式上做了一定的调整:

/bootstrap.css文件第4551行~第4554行/

.progress-striped .progress-bar-success {
 background-image: -webkit-linear-gradient(45deg, rgba(255, 255, 255, .15) 25%, transparent 25%, transparent 50%, rgba(255, 255, 255, .15) 50%, rgba(255, 255, 255, .15) 75%, transparent 75%, transparent);
 background-image:linear-gradient(45deg, rgba(255, 255, 255, .15) 25%, transparent 25%, transparent 50%, rgba(255, 255, 255, .15) 50%, rgba(255, 255, 255, .15) 75%, transparent 75%, transparent);
}

/bootstrap.css文件第4558行~第4561行/

.progress-striped .progress-bar-info {
 background-image: -webkit-linear-gradient(45deg, rgba(255, 255, 255, .15) 25%, transparent 25%, transparent 50%, rgba(255, 255, 255, .15) 50%, rgba(255, 255, 255, .15) 75%, transparent 75%, transparent);
 background-image:linear-gradient(45deg, rgba(255, 255, 255, .15) 25%, transparent 25%, transparent 50%, rgba(255, 255, 255, .15) 50%, rgba(255, 255, 255, .15) 75%, transparent 75%, transparent);
}

/bootstrap.css文件第4565行~第4568行/

.progress-striped .progress-bar-warning {
 background-image: -webkit-linear-gradient(45deg, rgba(255, 255, 255, .15) 25%, transparent 25%, transparent 50%, rgba(255, 255, 255, .15) 50%, rgba(255, 255, 255, .15) 75%, transparent 75%, transparent);
 background-image:linear-gradient(45deg, rgba(255, 255, 255, .15) 25%, transparent 25%, transparent 50%, rgba(255, 255, 255, .15) 50%, rgba(255, 255, 255, .15) 75%, transparent 75%, transparent);
}
/*bootstrap.css文件第4572行~第4575行*/
.progress-striped .progress-bar-danger {
 background-image: -webkit-linear-gradient(45deg, rgba(255, 255, 255, .15) 25%, transparent 25%, transparent 50%, rgba(255, 255, 255, .15) 50%, rgba(255, 255, 255, .15) 75%, transparent 75%, transparent);
 background-image:linear-gradient(45deg, rgba(255, 255, 255, .15) 25%, transparent 25%, transparent 50%, rgba(255, 255, 255, .15) 50%, rgba(255, 255, 255, .15) 75%, transparent 75%, transparent);
}

5、进度条–动态条纹进度条

使用方法:

在进度条“progress progress-striped”两个类的基础上再加入“active”类名。如下代码:

<br/>

1)、实现原理:

为了让条纹进度条动起来,Bootstrap框架还提供了一种动态条纹进度条。其实现原理主要通过CSS3的animation来完成。首先通过@keyframes创建了一个progress-bar-stripes的动画,这个动画主要做了一件事,就是改变背景图像的位置,也就是background-position的值。因为条纹进度条是通过CSS3的线性渐变来制作的,而linear-gradient实现的正是对应背景中的背景图片。

/bootstrap.css文件第4500行~第4515行/

@-webkit-keyframes progress-bar-stripes {
 from {
 background-position: 40px 0;
 }
 to {
 background-position: 0 0;
 }
}
@keyframes progress-bar-stripes {
 from {
 background-position: 40px 0;
 }
 to {
 background-position: 0 0;
 }
}

了解CSS3的同学都知道,@keyframes仅仅是创建了一个动画效果,如果要让进度条真正的动起来,我们需要通过一定的方式调用@keyframes创建的动画“progress-bar-stripes”,并且通过一个事件触发动画生效。在Bootstrap框架中,通过给进度条容器“progress”添加一个类名“active”,并让文档加载完成就触“progress-bar-stripes”动画生效。


调用动画对应的样式代码如下:

/bootstrap.css文件第4544行~第4547行/

.progress.active .progress-bar {
 -webkit-animation: progress-bar-stripes 2s linear infinite;
  animation: progress-bar-stripes 2s linear infinite;
}

运行效果如下:

特别注意:要让条纹进度条动起来,就需要让“progress-striped”和“active”同时运用,不然条纹进度条是不具备动效效果。

6、进度条–层叠进度条

Bootstrap框架除了提供上述几种进度条之外,还提供了一种层叠进度条,层叠进度条,可以将不同状态的进度条放置在一起,按水平方式排列。具体使用如下:

除了层叠彩色进度条之外,还可以层叠条纹进度条,或者说条纹进度条和彩色进度条混合层叠,仅需要在“progress”容器中添加对应的进度条,同样要注意,层叠的进度条之和不能大于100%。来简单的看一个示例:

运行效果如下:

7、进度条–带Label的进度条

上面介绍的各种进度条,都仅仅是通过颜色进度向用户传递进度值。但实际中,有很多时候是需要在进度条中直接用相关的数值向用户传递完成的进度值,在Bootstrap就为大家考虑了这种使用场景。

1)、实现方法:

只需要在进度条中添加你需要的值,如:

20%

 还有一种特殊情形,当进度条处于开始位置,也就是进度条的值为0%时,内容是否会撑开一定的宽度,让进度条具有颜色呢?如果是,这不是我们需要的效果,如果不是,又是怎么实现的呢?我们先来看一个这样的示例:

0%

2)、原理分析:

效果告诉我们,当进度为0%,进度条颜色并没有显示出来,那是因为Bootstrap在样式上做了一定的处理。

/bootstrap.css文件第4748行~第4759行/

.progress-bar[aria-valuenow="1"],
.progress-bar[aria-valuenow="2"] {
 min-width: 30px;
}
.progress-bar[aria-valuenow="0"] {
 min-width: 30px;
 color: #777;
 background-color: transparent;
 background-image: none;
 -webkit-box-shadow: none;
  box-shadow: none;
}

注:这段代码BootstrapV3.2版本才有。在Bootstrap V3.1.1版本是不具有这段代码,同时也说明,Bootstrap在不断的完善之中。

以上就是关于Bootstrap进度条的全部内容介绍,并有详细的Bootstrap教程,希望对大家的学习有所帮助。

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn