"Native JS plugin" Loadingbar page top loading progress bar

Source: Internet
Author: User

Let's start by showing the results that have been achieved:

See the browser on the phone built-in page loading progress bar, want to use on the PC.

Online search, see several ways of page loading:

1. Add the loading element to the body header and write the script in the body footer to let the loading element disappear.

2. Based on jquery, insert the script at different locations on the page to set the width of the scroll bar.

Simple analysis:

The first one is obviously not what I want.

The second to load jquery before the body, and then to use the animation method of jquery, performance is certainly not optimal state.

Own solution: Native JS+CSS3

The above Method 2 is actually a method to use, but I do not want to load jquery in front of the page, what to do?

It's easy to write one yourself in the native way.

Add CSS3 animated properties to the element so that he can animate when the width is changed.

Transition:all 1s;-moz-transition:all 1s;-webkit-transition:all 1s;-o-transition:all 1s;

Insert a style on the page with elements of CSS and a CSS3 animation suspended class

. animation_paused{-webkit-animation-play-state:paused;-moz-animation-play-state:paused;- ms-animation-play-state:paused;animation-play-state:paused;}

Then call the method in different places on the page, set the width of the scroll bar, and note that the reference to the method is in

<div id= "Top" ></div><script>loadingbar.setwidth (1) </script><div id= "NAV" ></div ><script>loadingbar.setwidth (</script><div id= "banner" ></div><script> Loadingbar.setwidth (+) </script><div id= "main" ></div><script>loadingbar.setwidth (60) </script><div id= "Right" ></div><script>loadingbar.setwidth </script><div id= "Foot" ></div><script>loadingbar.setwidth (</script>)

Plugin Source:

/*========================================================================loadingbar page Load progress bar @auther Liuming@blog Http://www.cnblogs.com/dtdxrkdemo fill in the body with the progress required to load loadingbar.setwidth (number) ======================= =================================================*/varLoadingbar = {    /*Initialize*/Init:function(){         This. Creatstyle ();  This. Creatloaddiv (); },    /*Record Current width*/Width:0,    /*page Loadingbar Div*/Oloaddiv:false,    /*Start*/SetWidth:function(w) {if(! This. oloaddiv) { This. Init ();} varOloaddiv = This. Oloaddiv, Width= Number (W) | | 100; /*prevents the width of the subsequent write to be less than the width of the previous write*/(Width< This. width)? Width= This. Width: This. width =width; Oloaddiv.classname= ' animation_paused '; OLoadDiv.style.width= width + '% '; Oloaddiv.classname= ' '; if(width = = 100) { This. Over (Oloaddiv);} },    /*Page Loading Complete*/Over :function(obj) {setTimeout (function() {Obj.style.display= ' None '; },1000); },    /*Create a load bar*/Creatloaddiv:function(){        vardiv = document.createelement (' div '); Div.id= ' Loadingbar ';        Document.body.appendChild (DIV);  This. Oloaddiv = document.getElementById (' Loadingbar '); },    /*Create Style*/Creatstyle:function(){        varnod = document.createelement (' style ')), str= ' #LoadingBar {transition:all 1s;-moz-transition:all 1s;-webkit-transition:all 1s;-o-transition:all 1s; Background-color: #f90; height:3px;width:0; Position:fixed;top:0;z-index:99999;left:0;font-size:0;z-index:9999999;_position:absolute;_top:expression (eval ( DOCUMENT.DOCUMENTELEMENT.SCROLLTOP));}. Animation_paused{-webkit-animation-play-state:paused;-moz-animation-play-state:paused;-ms-animation-play-state :p aused;animation-play-state:paused;}; 'Nod.type= ' Text/css '; //ie underNod.stylesheet? Nod.styleSheet.cssText = Str:nod.innerHTML =str; document.getElementsByTagName (' head ') [0].appendchild (NOD); }}

"Native JS plugin" Loadingbar page top loading progress bar

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.