Native JS to achieve home page progress loading animation _javascript skills

Source: Internet
Author: User

JS Progress Loading Animation program is my personal work, write a bad, you can refer to, but without my permission, please do not use for other purposes!

The morning to write a homepage progress loading animation, I would like to use on my blog, test found that the blog is too fast to load, not see the animation effect, directly loaded ' Complete ', forget it, or do not make the blog too bloated!

So I wrote a demo page, in the body Riga an IFRAME to load a larger site, so you can see the effect!

With Safari to open a seemingly CSS animation playback time into sync, do not know what the reason, the local test is no problem (please great God guidance!) Chrome, Firefox is OK, and ie I haven't tested

Load time Statistics I used the performance property of the Windows object, which is a method specifically used to compute the exact time, which is a description of MDN about the performance property

The implementation of this example is relatively simple, but not true by file size to show the load progress, only when triggering document.onreadystatechange events, according to the state of document.readystate to change the display progress. In fact, there is a more reliable method, with the XMLHttpRequest object instance of the Progress event, detailed interpretation of XMLHttpRequest, such as:

 var request = new XMLHttpRequest ();
 request.onprogress = function (e) {
 if (e.lengthcomputable) {//lengthcomputable refers to whether a file has a size, a value of true, False
 progress.innerhtml = Math.Round (100* e.loaded/e.total) + \ '%\ '; Loaded, total indicates the size and overall size already loaded
 }
 } 

Using the above method to achieve it is also very troublesome, but also can not achieve 100% real load progress, so I simply fake a little, do not have him!

This example also uses the Document.stylesheets[0].insertrule () method, here has my summary of it: Using native JS to read and write CSS style method summary

About the implementation of CSS animation, we look at the code, very simple code, if you look laborious, suggest to complement the CSS Foundation, introduce a dry goods, CSS Chinese Reference manual website, I have limited ability to express, not here to chew the tongue

Here is the complete code + Demo:

<! DOCTYPE html>  

The above is the entire content of this article, I hope to help you learn, but also hope that we support the cloud habitat community.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.