原生JS实现LOADING效果_javascript技巧
程序员文章站
2022-03-30 14:17:11
...
纯文本loading效果,可以自己定义颜色和速度
/** 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
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)
}
复制代码 代码如下:
/** 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
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)
}
以上就是本文所述的全部内容了,希望对大家学习javascript能有所帮助。
上一篇: php能做什么项目?
下一篇: 怎样获取前端界面中的DOM元素绝对位置