Introduction to several methods of implementing progress bars in JavaScript

黄舟
Release: 2017-11-21 09:54:03
Original
8343 people have browsed it

In our previous article, we introduced to you examples of JavaScript implementation of progress bars. We all know that in actual projects, progress bars are still very useful. Today we will continue to introduce the benefits of JavaScript implementation of progress bars. Which ways!

Let’s take a look at the final effect first:

Introduction to several methods of implementing progress bars in JavaScript

The first step is to basically
Build the basic code and see the effect demonstration :

   制作进度条的两种方法   

1,构建基本的HTML结构和CSS样式

0%的状态:
50%的状态:
80%的状态:
100%的状态:
这步非常简单,相信大家都能看明白。
Copy after login

The second step is to add text to the progress bar to display

   制作进度条的两种方法   

2,给进度条增加文字显示

一个进度条需要包含对应的进度文字显示,本例中,我希望文字显示在进度条的中间。 接下来,我们在进度条元素内增加一个显示 文字的div。 很显然,直接添加div后,div会占据原先绿色进度条的位置。看下面例子:
50%
那么有什么办法使得文字 和 绿色进度条 同时显示在 一起呢? 估计你也想到了,那就是绝对定位。通过对父元素添加相对定位,然后给子元素添加绝对定位,设置子元素的左边距和上边距都为0。 看下面的例子:
50%
这时你会发现文字被遮盖了,相信你第一时间就会想到用 z-index 去设置他们的层级关系。 对,我也选择的是它。通过给他们设置不同的z-index值来确定层级关系,值越大越显示在上面。看下面例子:
50%
80%
100%
这是第一种方式,也是目前最为流行的 制作进度条的方式。 他的原理就是采用 相对定位和绝对定位,然后通过背景不同来控制进度。 接下来我们看第二种方式制作进度条。
Copy after login

him The principle is to use relative positioning and absolute positioning, and then control the progress through different backgrounds. Next we look at the second way to create a progress bar.
The third step, the second way to make a progress bar---directly use pictures + background pictures

      

3,制作进度条的第二种方式---直接使用图片+背景图片

0% 20% 50% 80% 100% 如果你想在上面进度条上添加进度显示的文字,你应该知道怎么办了(如果你仔细阅读过前面的例子的话)。 对,就是使用绝对定位+相对定位来控制。
0%
0%
20%
20%
50%
50%
80%
80%
100%
100%
这种方式的原理就是 利用 一张img图片,然后给img图片又设置背景图,通过背景图的background position 属性 来显示不同的部分。 当然这种方式 对图片有点要求 。你仔细一点就会发现,宽度是img的2倍。 另外图片必须是gif透明的,也就是说空心的。呵呵。看不到进度条。 总的来说,这应该算一个技巧,在实际项目中,也是能使用的。

Dev By jb51.net

Copy after login

The fourth step, apply
Finally, let’s look at a simple application:

        

4,应用

0%
0%
我使用的是第二种方式来做控制,通过animate来控制图片的background-position来达到进度条效果。 当然你也可以使用第一种方式做,第一种方式需要改变的是进度条的 width 属性。

Dev By jb51.net

Copy after login

Use animate to control the background-position of the image to achieve the progress bar effect.
Of course you can also use the first method. What needs to be changed in the first method is the width attribute of the progress bar.

Summary:

Through the study of this article, I believe that my friends will have a better understanding of the method of implementing the progress bar in JavaScript. Each method They are all different, friends can choose the appropriate method according to their own needs!

Related recommendations:

JavaScript native code to implement progress bar


JS native upload large file display progress bar-php upload file


Example of JavaScript implementing progress bar based on timer

The above is the detailed content of Introduction to several methods of implementing progress bars in JavaScript. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
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
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!