本篇文章教大家如何给自己的网页添加一个字体闪烁(变色)特效。
添加步骤
给要添加特效的元素添加class:magic-color
将以下代码复制到元素后面(多个元素则放到最后一个元素后面)
<script>
function magicColor(mode,t){
t=t||10;
let elem=document.getElementsByClassName("magic-color");
if(!elem){
setTimeout(function(){
magicColor(mode,t-1);
},400);
return;
}
if(window.mcHandler==undefined){
window.mcHandler={elements:[]};
window.mcHandler.colorIndex=0;
window.mcHandler.run=function(mode){
let color=mode=="random"?("rgb("+Math.floor(Math.random()*256)+","+Math.floor(Math.random()*256)+","+Math.floor(Math.random()*256)+",1)"):["#CC0000","#9999CC","#CC3366","#669999","#FFCC00","#00CCCC","#CC00CC"][(window.mcHandler.colorIndex++)%7];
for(var i=0,L=window.mcHandler.elements.length;i<L;i++)window.mcHandler.elements[i].style.color=color;
}
}
window.mcHandler.elements=elem;
if(window.mcHandler.timer==undefined){
window.mcHandler.timer=setInterval(()=>{window.mcHandler.run(mode)},500);
}
}
magicColor(random);//random为随机颜色,否则为固定颜色随机;上方“闪烁(变色)”字样为固定颜色随机
</script>
{callout color=”#ef6d4d”}
转载来源: {abtn color=”#ff0d00″ href=”https://chenyu.me/1420.html” content=”醉书生”/}
{/callout}
© 版权声明
文章版权归作者所有,未经允许请勿转载。
THE END
请登录后查看评论内容