Native JS achieves LOADING effect

Source: Internet
Author: User

Native JS achieves LOADING effect

This article mainly introduces the LOADING effect Code implemented by native JS, which has very good results and can customize the color and speed. We recommend it to you and hope you will like it.

 

 

Pure text loading effect, you can customize the color and speed

 

The Code is as follows:


/** Loading animation
* Created by と on 15/03/11.
*/

Function loading (element, lightColor, darkColor, speed, callback ){
If (! Element &&(! Element. innerText |! Element. textContent) return
Element = typeof element = "string "? Document. getElementById (element): element
LightColor = lightColor | "# fff", darkColor = darkColor | "#000", speed = speed || 300

Var arr_spanEles = new Array ()

! Function (arr_elementText ){
Element. innerText = element. textContent = ""
For (var I = 0; I <arr_elementText.length; I ++ ){
Var span = document. createElement ("span ")
Element. innerText? Span. innerText = arr_elementText [I]: span. textContent = arr_elementText [I]
Element. appendChild (span)
Arr_spanEles.push (span)
}
} (Element. innerText | element. textContent). split (""))

Var index =-1, length = arr_spanEles.length
Var loadingTimer = setInterval (function (){
Arr_spanEles [Math. max (index, 0)]. style. color = darkColor
If (index = length-1 ){
Index =-1
Callback & callback ()
}
++ Index
Arr_spanEles [index]. style. color = lightColor
}, Speed)
}

 

The above is all the content described in this article. I hope it will be helpful for you to learn javascript.

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.