CSS 、JS實(shí)現(xiàn)浪漫流星雨動(dòng)畫

1,效果圖
2,源碼
HTML
< body > < div class = “container” > < div id = “mask” > </ div > < div id = “sky” > </ div > < div id = “moon” > </ div > < div id = “stars” > </ div > < div class = “cloud cloud-1” ></ div > <div class = “cloud cloud-2” > </ div > < div class = “cloud cloud-3” > </ div > </ div > </ body >
CSS
/* - - - - - - 重啟 - - - - - - */ * { 保證金:0 ; 填充:0 ; } html, body { width:100% ; 最小寬度:1000px ; 身高:100% ; 最小高度:400px ; 溢出:隱藏; } / * ------------畫布------------ * / .container { position:relative; 身高:100% ; } / *遮罩層* / #mask { position:absolute; 寬度:100% ; 身高:100% ; background:rgba(0,0,0,.8); z-index:900 ; } / *天空背景* / #sky { width:100% ; 身高:100% ; background:線性漸變(rgba(0,150,255,1),rgba(0,150,255,.8),rgba(0,150,255,.5)); } / *月亮* / #moon { position:absolute; 上:50px ; 右:200px ; 寬度:120px ; 身高:120px ; 背景:rgba(251,255,25,0.938); border-radius:50% ; box-shadow:0 0 20px rgba(251,255,25,0.5); z-index:9999 ; } / *閃爍星星* / .blink { position:absolute; background:rgb(255,255,255); border-radius:50% ; box-shadow:0 0 5px rgb(255,255,255); 不透明度:0 ; z-index:10000 ; } / *流星* / .star { position:absolute; 不透明度:0 ; z-index:10000 ; } .star :: after { content:“” ; 顯示:塊; 邊界:堅(jiān)固; border-width:2px 0 2px 80px ; / *流星隨長度逐漸縮小* / border-color:透明透明透明rgba(255,255,255,1); border-radius:2px 0 0 2px ; transform:rotate(-45deg); transform-origin:0 0 0 ; 盒子陰影:0 0 20px rgba(255,255,255,.3); } / *云* / .cloud { position:absolute; 寬度:100% ; 身高:100px ; } .cloud-1 { bottom: - 100px ; z-index:1000 ; 不透明度:1 ; 變換:規(guī)模(1.5); -webkit-transform:scale(1.5); -moz-transform:scale(1.5); -ms-transform:scale(1.5); -o-transform:scale(1.5); } .cloud-2 { left: - 100px ; 底部: - 50px ; z-index:999 ; 不透明度:。5 ; 變換:旋轉(zhuǎn)(7deg); -webkit-transform:rotate(7deg); -moz-transform:rotate(7deg); -ms-transform:rotate(7deg); -o-transform:rotate(7deg); } .cloud-3 { left:120px ; 底部: - 50px ; z-index:999 ; 不透明度:。1 ; transform:rotate(-10deg); -webkit-transform:rotate(-10deg); -moz-transform:rotate(-10deg); -ms-transform:rotate(-10deg); -o-transform:rotate(-10deg); } .circle { position:absolute; border-radius:50% ; 背景:#fff ; } .circle-1 { width:100px ; 身高:100px ; 上: - 50px ; 左:10px ; } .circle-2 { width:150px ; 身高:150px ; 上: - 50px ; 左:30px ; } .circle-3 { width:300px ; 身高:300px ; 上: - 100px ; 左:80px ; } .circle-4 { width:200px ; 身高:200px ; 上: - 60px ; 左:300px ; } .circle-5 { width:80px ; 身高:80px ; 上: - 30px ; 左:450px ; } .circle-6 { width:200px ; 身高:200px ; 上: - 50px ; 左:500px ; } .circle-7 { width:100px ; 身高:100px ; 上: - 10px ; 左:650px ; } .circle-8 { width:50px ; 身高:50px ; 上:30px ; 左:730px ; } .circle-9 { width:100px ; 身高:100px ; 上:30px ; 左:750px ; } .circle-10 { width:150px ; 身高:150px ; 上:10px ; 左:800px ; } .circle-11 { width:150px ; 身高:150px ; 上: - 30px ; 左:850px ; } .circle-12 { width:250px ; 身高:250px ; 上: - 50px ; 左:900px ; } .circle-13 { width:200px ; 身高:200px ; 上: - 40px ; 左:1000px ; } .circle-14 { width:300px ; 身高:300px ; 上: - 70px ; 左:1100px ; }
JS
//流星動(dòng)畫 setInterval(function() { const obj = addChild(“#sky”,“div”,2,“star”); for(let i = 0 ; i <obj.children.length; i ++){ const top = -50 + Math .random()* 200 + “px”, left = 200 + Math .random()* 1200 + “px”, scale = 0.3 + Math .random()* 0.5 ; const timer = 1000 + Math .random()* 1000 ; obj.children [i] .style.top = top; obj.children [i] .style.left = left; obj.children [i] .style.transform = `scale($ {scale})` ; requestAnimation({ ele:obj.children [i], attr:[ “top”,“left”,“opacity” ], 值:[ 150,-150,.8 ], time:timer, flag:false, fn:function() { requestAnimation({ ELE:obj.children [I], ATTR:“頂”,“左”,“不透明” ], 值:[ 150,-150,0 ], 時(shí)間:定時(shí)器, 標(biāo)志:假, FN:() => { obj.parent.removeChild(obj.children [I]); } }) } }); } },1000); //閃爍星星動(dòng)畫 setInterval(function() { const obj = addChild(“#stars”,“div”,2,“blink”); for(let i = 0 ; i <obj.children.length; i ++){ const top = -50 + Math .random()* 500 + “px”, left = 200 + Math .random()* 1200 + “px”, round = 1 + Math .random()* 2 + “px” ; const timer = 1000 + Math .random()* 4000 ; obj.children [i] .style.top = top; obj.children [i] .style.left = left; obj.children [i] .style.width = round; obj.children [i] .style.height = round; requestAnimation({ ele:obj.children [i], attr:“opacity”, 值:.5, time:timer, flag:false, fn:function() { requestAnimation({ ele:obj.children [i], attr:“opacity”, value:0, time:timer, flag:false, fn:function() { obj.parent.removeChild(obj.children [I]); } }); } }); } },1000); //月亮移動(dòng) requestAnimation({ ele:“#moon”, attr:“right”, 值:1200, 時(shí)間:10000000, }); //添加云 const clouds = addChild(“。cloud”,“div”,14,“circle”,true); for(let i = 0 ; i <clouds.children.length; i ++){ for(let j = 0 ; j <clouds.children [i] .length;){ clouds.children [i] [j] .classList.add(`circle- $ {++ j} `); } } //云動(dòng)畫 let flag = 1 ; 的setInterval( 功能() { const clouds = document .querySelectorAll(“。cloud”); const left = Math .random()* 5 ; bottom = Math .random()* 5 ; let timer = 0 ; for(let i = 0 ; i <clouds.length; i ++){ requestAnimation({ ele:clouds [i], attr:[ “left”,“bottom” ], value:flag%2?[-left,-bottom]:[left,bottom], time:timer + = 500, flag:false, fn:function() { requestAnimation({ ele:clouds [i], attr:[ “left”,“bottom” ], value:flag%2?[left,bottom]:[ - left,-bottom], time:timer, flag:false }) } }); } 標(biāo)志++; },2000)
封裝方法
// -------------------------------------------動(dòng)畫---- ----------------------------------------------- //運(yùn)動(dòng)動(dòng)畫,調(diào)用Tween.js // ele:dom | 班級| id | 標(biāo)簽節(jié)點(diǎn)| 類名| id名| 標(biāo)簽名,只支持選擇一個(gè)節(jié)點(diǎn),類類名以及標(biāo)簽名只能選擇頁面中第一個(gè) // attr:屬性屬性名 //值:目標(biāo)值目標(biāo)值 //時(shí)間:持續(xù)時(shí)間持續(xù)時(shí)間 //補(bǔ)間:定時(shí)function函數(shù)方程 // flag:Boolean判斷是按值移動(dòng)還是按位置移動(dòng),默認(rèn)按位置移動(dòng) // fn:callback回調(diào)函數(shù) //增加返回值:將內(nèi)部參數(shù)對象返回,可以通過設(shè)置返回對象的屬性stop為true打斷動(dòng)畫 函數(shù) requestAnimation(obj) { // -------------------------------------參數(shù)設(shè)置--------------------------------------------- //默認(rèn)屬性 const參數(shù)= { ele:null, attr:null, value:null, time:1000, tween:“linear”, flag:true, stop:false, fn:“” } //合并傳入屬性 Object .assign(parameter,obj); //覆蓋重名屬性 // -------------------------------------動(dòng)畫設(shè)置--------- ------------------------------------ //創(chuàng)建運(yùn)動(dòng)方程初始參數(shù),方便復(fù)用 let start = 0 ; //用于保存初始時(shí)間戳 let target =(typeof parameter.ele === “string”?document .querySelector(parameter.ele):parameter.ele),//目標(biāo)節(jié)點(diǎn) attr = parameter.attr,//目標(biāo)屬性 beginAttr = parseFloat(getComputedStyle(target)[attr]),// attr起始值 value = parameter.value,//運(yùn)動(dòng)目標(biāo)值 count = value - beginAttr,//實(shí)際運(yùn)動(dòng)值 time = parameter.time,//運(yùn)動(dòng)持續(xù)時(shí)間, tween = parameter.tween,//運(yùn)動(dòng)函數(shù) flag = parameter.flag, callback = parameter.fn,//回調(diào)函數(shù) curVal = 0 ; //運(yùn)動(dòng)當(dāng)前值 //判斷傳入函數(shù)是否為數(shù)組,多段運(yùn)動(dòng) (function() { if(attr instanceof Array){ beginAttr = []; count = []; 對于(讓我的 ATTR){ 常量 VAL = parseFloat(的getComputedStyle(目標(biāo))[I]); beginAttr.push(VAL); count.push(value - val); } } if(value instanceof Array){ for(let i in value){ count [i] = value [i] - beginAttr [i]; } } })(); //運(yùn)動(dòng)函數(shù) 功能 動(dòng)畫(時(shí)間戳) { 如果(parameter.stop)返回 ; //打斷 //存儲(chǔ)初始時(shí)間戳 if(!start)start = timestamp; //已運(yùn)動(dòng)時(shí)間 讓 t =時(shí)間戳 - 開始; //判斷多段運(yùn)動(dòng) if(beginAttr instanceof Array){ // const len = beginAttr.length //存數(shù)組長度,復(fù)用 //多段運(yùn)動(dòng)第1類 - 多屬性,同目標(biāo),同時(shí)間/不同時(shí)間 if(typeof count === “number”){ //同目標(biāo) //同時(shí)間 if(typeof time === “number”){ if(t> time)t = time; //判斷是否超出目標(biāo)值 //循環(huán)attr,分別賦值 為(let i in beginAttr){ if(flag)curVal = Tween [tween](t,beginAttr [i],count,time); //調(diào)用Tween,返回當(dāng)前屬性值,此時(shí)計(jì)算方法為移動(dòng)到 寫入位置else curVal = Tween [tween](t,beginAttr [i],count + beginAttr [i],time); //調(diào)用Tween,返回當(dāng)前屬性值,此時(shí)計(jì)算方法為移動(dòng)了 寫入距離if(attr [i] === “opacity”)target.style [attr [i]] = curVal; //給屬性賦值 else target.style [attr [i]] = curVal + “px” ; //給屬性賦值 if(t <time)requestAnimationFrame(animate); //判斷是否運(yùn)動(dòng)完 其他回調(diào)&& callback(); //調(diào)用回調(diào)函數(shù) } 回歸 ; } //不同時(shí)間 if(time instanceof Array){ //循環(huán)時(shí)間,attr,分別賦值 為(讓我在 beginAttr中){ //錯(cuò)誤判斷 if(!time [i] && time [i]!== 0){ throw new Error( “輸入時(shí)間的長度不等于屬性的長度”); } //判斷是否已經(jīng)完成動(dòng)畫,完成則跳過此次循環(huán) if(parseFloat(getComputedStyle(target)[attr [i]])===(typeof value === “number”?value:value [i]) ) 繼續(xù) ; // t =時(shí)間戳 - 開始; //每次循環(huán)初始化t if(t> time [i])t = time [i]; //判斷是否超出目標(biāo)值 if(flag || attr [i] === “opacity”)curVal = Tween [tween](t,beginAttr [i],count,i); //調(diào)用Tween,返回當(dāng)前屬性值,此時(shí)計(jì)算方法為移動(dòng)到 寫入位置else curVal = Tween [tween](t,beginAttr [i],count + beginAttr [i],i); //調(diào)用Tween,返回當(dāng)前屬性值,此時(shí)計(jì)算方法為移動(dòng)了 寫入距離if(attr [i] === “opacity”)target.style [attr [i]] = curVal; //給屬性賦值 else target.style [attr [i]] = curVal + “px” ; //給屬性賦值 } if(t < Math .max(... time))requestAnimationFrame(animate); //判斷函數(shù)是否運(yùn)動(dòng)完 其他回調(diào)&& callback(); //如果已經(jīng)執(zhí)行完時(shí)間最長的動(dòng)畫,則調(diào)查回調(diào)函數(shù) return ; } } //多段運(yùn)動(dòng)第2類 - 多屬性,不同目標(biāo),同時(shí)間/不同時(shí)間 if(count instanceof Array){ //同時(shí)間 if(typeof time === “number”){ if(t> time)t = time; //判斷是否超出目標(biāo)值 for(let i in beginAttr){ //循環(huán)attr,count,分別賦值 //錯(cuò)誤判斷 if(!count [i] && count [i]!== 0){ throw new Error( “輸入值的長度不是等于屬性的長度“); } if(flag || attr [i] === “opacity”)curVal = Tween [tween](t,beginAttr [i],count [i],time); //調(diào)用Tween,返回當(dāng)前屬性值,此時(shí)計(jì)算方法為移動(dòng)到 寫入位置else curVal = Tween [tween](t,beginAttr [i],count [i] + beginAttr [i],time); //調(diào)用Tween,返回當(dāng)前屬性值,此時(shí)計(jì)算方法為移動(dòng)了 寫入距離if(attr [i] === “opacity”)target.style [attr [i]] = curVal; //給屬性賦值 else target.style [attr [i]] = curVal + “px” ; //給屬性賦值 } if(t <time)requestAnimationFrame(animate); //判斷函數(shù)是否運(yùn)動(dòng)完 其他回調(diào)&& callback(); //如果已經(jīng)執(zhí)行完時(shí)間最長的動(dòng)畫,則調(diào)查回調(diào)函數(shù) return ; } //不同時(shí)間 if(time instanceof Array){ for(let i in beginAttr){ //錯(cuò)誤判斷 if(!time [i] && time [i]!== 0){ throw new Error( “輸入時(shí)間的長度)不等于屬性的長度“); } //判斷是否已經(jīng)完成動(dòng)畫,完成則跳過此次循環(huán) if(parseFloat(getComputedStyle(target)[attr [i]])===(typeof value === “number”?value:value [i]) ) 繼續(xù) ; if(t> time [i])t = time [i]; //判斷是否超出目標(biāo)值 //錯(cuò)誤判斷 if(!count [i] && count [i]!== 0){ throw new Error( “輸入值的長度不等于屬性的長度”); } if(flag || attr [i] === “opacity”)curVal = Tween [tween](t,beginAttr [i],count [i],time [i]); //調(diào)用Tween,返回當(dāng)前屬性值,此時(shí)計(jì)算方法為移動(dòng)到 寫入位置其他 curVal = Tween [tween](t,beginAttr [i],count [i] + beginAttr [i],time [i]) ; //調(diào)用Tween,返回當(dāng)前屬性值,此時(shí)計(jì)算方法為移動(dòng)了 寫入距離if(attr [i] === “opacity”)target.style [attr [i]] = curVal; 否則 target.style [attr [i]] = curVal + “px” ; } if(t < Math .max(... time))requestAnimationFrame(animate); else callback && callback(); 回歸 ; } } } //單運(yùn)動(dòng)模式 if(t> time)t = time; if(flag || attr === “opacity”)curVal = Tween [tween](t,beginAttr,count,time); //調(diào)用Tween,返回當(dāng)前屬性值,此時(shí)計(jì)算方法為移動(dòng)到 寫入位置else curVal = Tween [tween](t,beginAttr [i],count + beginAttr,time); //調(diào)用Tween,返回當(dāng)前屬性值,此時(shí)計(jì)算方法為移動(dòng)了 寫入距離if(attr === “opacity”)target.style [attr] = curVal; 否則 target.style [attr] = curVal + “px” ; if(t <time)requestAnimationFrame(animate); else callback && callback(); } requestAnimationFrame(動(dòng)畫); 返回參數(shù); //返回對象,供打斷或其他用途 } //Tween.js / * * t:時(shí)間已過時(shí)間 * b:開始起始值 * c:計(jì)算總的運(yùn)動(dòng)值 * d:持續(xù)時(shí)間持續(xù)時(shí)間 * *曲線方程 * * http://www.cnblogs.com/bluedream2009/archive/2010/06/19/1760909.html * * / 讓 Tween = { linear:function(t,b,c,d) { //勻速 返回 c * t / d + b; }, easeIn:function(t,b,c,d) { //加速曲線 return c *(t / = d)* t + b; }, easeOut:function(t,b,c,d) { //減速曲線 return -c *(t / = d)*(t - 2)+ b; }, easeBoth:function(t,b,c,d) { //加速減速曲線 if((t / = d / 2)< 1){ return c / 2 * t * t + b; } return -c / 2 *(( - t)*(t - 2) - 1)+ b; }, easeInStrong:function(t,b,c,d) { //加加速曲線 return c *(t / = d)* t * t * t + b; }, easeOutStrong:function(t,b,c,d) { //減減曲線 返回 -c *((t = t / d - 1)* t * t * t - 1)+ b; }, easeBothStrong:function(t,b,c,d) { //加速減減速線 如果((t / = d / 2)< 1){ return c / 2 * t * t * t * t + b; } return -c / 2 *((t - = 2)* t * t * t - 2)+ b; }, elasticIn:function(t,b,c,d,a,p) { //正弦衰減曲線(彈動(dòng)漸入) if(t === 0){ return b; } if((t / = d)== 1){ return b + c; } if(!p){ p = d * 0.3 ; } if(!a || a < Math .abs(c)){ a = c; var s = p / 4 ; } else { var s = p /(2 * Math .PI)* Math .asin(c / a); } 返回 - (A * 數(shù)學(xué) .pow(2,10 *(T - = 1))* 數(shù)學(xué) .sin((T * d - S)*(2 * 數(shù)學(xué) .PI)/ P))+ B; }, elasticOut:function(t,b,c,d,a,p) { //正弦增強(qiáng)曲線(彈動(dòng)漸出) if(t === 0){ return b; } if((t / = d)== 1){ return b + c; } if(!p){ p = d * 0.3 ; } if(!a || a < Math .abs(c)){ a = c; var s = p / 4 ; } else { var s = p /(2 * Math .PI)* Math .asin(c / a); } 返回 a * Math .pow(2,-10 * t)* Math .sin((t * d - s)*(2 * Math .PI)/ p)+ c + b; }, elasticBoth:function(t,b,c,d,a,p) { if(t === 0){ return b; } if((t / = d / 2)== 2){ return b + c; } if(!p){ p = d *(0.3 * 1.5); } if(!a || a < Math .abs(c)){ a = c; var s = p / 4 ; } else { var s = p /(2 * Math .PI)* Math .asin(c / a); } 如果(T < 1){ 返回 -0.5 *(A * 數(shù)學(xué) .pow(2,10 *(T - = 1))* 數(shù)學(xué) .sin((T * d - S)*(2 * 數(shù)學(xué) .PI)/ p))+ b; } 返回 a * Math .pow(2,-10 *(t - = 1))* Math .sin((t * d - s)*(2 * Math .PI)/ p)* 0.5 + c + b; }, backIn:function(t,b,c,d,s) { //回退加速(回退漸入) if(typeof s == 'undefined'){ s = 1.70158 ; } return c *(t / = d)* t *((s + 1)* t - s)+ b; }, backOut:function(t,b,c,d,s) { if(typeof s == 'undefined'){ s = 3.70158 ; //回縮的距離 } return c *((t = t / d - 1)* t *((s + 1)* t + s)+ 1)+ b; }, backBoth:function(t,b,c,d,s) { if(typeof s == 'undefined'){ s = 1.70158 ; } if((t / = d / 2)< 1){ return c / 2 *(t * t *(((s * =(1.525))+ 1)* t - s))+ b; } return c / 2 *((t - = 2)* t *(((s * =(1.525))+ 1)* t + s)+ 2)+ b; }, bounceIn:function(t,b,c,d) { //彈球漸出) 返回 c - Tween [ 'bounceOut' ](d - t,0,c,d)+ b; }, bounceOut:function(t,b,c,d) { if((t / = d)<(1 / 2.75)){ return c *(7.5625 * t * t)+ b; } else if(t <(2 / 2.75)){ return c *(7.5625 *(t - =(1.5 / 2.75))* t + 0.75)+ b; } else if(t <(2.5 / 2.75)){ return c *(7.5625 *(t - =(2.25 / 2.75))* t + 0.9375)+ b; } return c *(7.5625 *(t - =(2.625 / 2.75))* t + 0.984375)+ b; }, bounceBoth:函數(shù)(T,B,C,d) { 如果(T <d / 2){ 返回吐溫[ 'bounceIn' ](T * 2,0,C,d)* 0.5 + B; } return Tween [ 'bounceOut' ](t * 2 - d,0,c,d)* 0.5 + c * 0.5 + b; } } // ------------------------------------------- DOM操作--- ------------------------------------------------ //添加節(jié)點(diǎn) // ele:父節(jié)點(diǎn),支持輸入變量,id值,類值,標(biāo)簽值。除變量外,其余值必須為字符串 //節(jié)點(diǎn):添加的標(biāo)簽名,值為字符串 // n:節(jié)點(diǎn)添加個(gè)數(shù) // className:節(jié)點(diǎn)綁定的類名,值為字符串,多個(gè)類名用空格隔開 //布爾:是否選中所有目標(biāo)父節(jié)點(diǎn)??蛇x參數(shù),不輸入則判定為false,則只匹配選中的第一個(gè)節(jié)點(diǎn) 函數(shù) addChild(ele,node,n,className,boolean) { //獲取節(jié)點(diǎn) let parent = null ; if(typeof ele!== “string”)parent = ele; else if(ele [ 0 ] === “#”)parent = document .getElementById(ele.slice(1)); else if(ele [ 0 ] === “。”){ if(boolean === false)parent = document .getElementsByClassName(ele.slice(1))[ 0 ]; else parent = document .getElementsByClassName(ele.slice(1)); } else { if(boolean === false)parent = docuemnt.getElementsByTagName(ele)[ 0 ]; else parent = document .getElementsByTagNameNS(ele); } //聲明用于存儲(chǔ)父節(jié)點(diǎn)及子節(jié)點(diǎn)的對象 const obj = { “parent”:parent, “children”:[] }; //添加節(jié)點(diǎn) if(boolean){ for(let i = 0 ; i <parent.length; i ++){ //創(chuàng)建容器碎片 const fragment = document .createDocumentFragment(); //保存子節(jié)點(diǎn),用于返回值 obj.children [i] = []; for(let j = 0 ; j <n; j ++){ const target = document .createElement(node); target.className = className; fragment.appendChild(目標(biāo)); //添加子節(jié)點(diǎn)到數(shù)組,用于返回值 obj.children [i] [j] =目標(biāo); } 父[I] .appendChild(片段) } } else { //創(chuàng)建碎片容器 const fragment = document .createDocumentFragment(); for(let i = 0 ; i <n; i ++){ const target = document .createElement(node); target.className = className; fragment.appendChild(目標(biāo)); //添加子節(jié)點(diǎn),用于返回值 obj.children [i] =目標(biāo); } //將碎片容器一次性添加到父節(jié)點(diǎn) parent.appendChild(片段); } //返回參數(shù),供動(dòng)畫函數(shù)調(diào)用 return obj; }
3,案例解析
HTML
由于節(jié)點(diǎn)很多,并且我想盡量做得逼真有趣有點(diǎn),還給節(jié)點(diǎn)加了隨機(jī)位置。所以節(jié)點(diǎn)的輸出都是用JS控制的,HTML這邊只寫了幾個(gè)父元素盒子,加上相應(yīng)的ID名和類類名,結(jié)構(gòu)相對簡單。
CSS
CSS部分的難點(diǎn)就是流星的樣式和用圈圈畫云層,然后將云層堆疊出立體效果。
首先說一下流星的樣式:
#sky .star { position:absolute; 不透明度:0 ; z-index:10000 ; } .star :: after { content:“” ; 顯示:塊; 邊界:堅(jiān)固; border-width:2px 0 2px 80px ; / *流星隨長度逐漸縮小* / border-color:透明透明透明rgba(255,255,255,1); border-radius:2px 0 0 2px ; transform:rotate(-45deg); transform-origin:0 0 0 ; 盒子陰影:0 0 20px rgba(255,255,255,.3); }
先提取了公共樣式,添加定位屬性;
然后在星后通過后偽類添加流星,用邊界特性畫:
1)模型繪制:border-width的順序?yàn)樗倪卼op,right,bottom,left,同理border-color的順序也為四邊top,right,bottom,left。這樣將border-width與border-color一一對應(yīng)后,就能看出2px的是流星的寬度,80px是流星的長度,而0像素流星就是尾巴的這樣就形成了一個(gè)。頭部2px的寬,尾部0像素,長度80px的流星模型 ;
2)稍微逼真:通過邊界半徑?給流星的頭部增加個(gè)圓角,讓它看起來更逼真最后通過roteta旋轉(zhuǎn)一個(gè)角度,讓它看起來像是往下掉;
3)增加閃光:通過箱陰影給流星增加一點(diǎn)光暈,讓它看起來有閃光的效果;
通過以上3步,一個(gè)流星就畫好了。
然后是畫云:
因?yàn)樵频拇a比較長,這里就不貼出來了方法無非是通過一個(gè)一個(gè)的圓,相互疊加覆蓋,完成一個(gè)云朵的形狀。
完成一個(gè)云層之后,copy一個(gè),然后多個(gè)云層通過rotate,opacity,left定位等,做出一個(gè)漸隱疊加的立體效果;
JS
JS部分以流星舉例說明
setInterval(function() { const obj = addChild(“#sky”,“div”,2,“star”); //插入流星 for(let i = 0 ; i <obj.children.length; i ++){ //隨機(jī)位置 const top = -50 + Math .random()* 200 + “px”, left = 200 + Math .random()* 1200 + “px”, scale = 0.3 + Math .random()* 0.5 ; const timer = 1000 + Math .random()* 1000 ; obj.children [i] .style.top = top; obj.children [i] .style.left = left; obj.children [i] .style.transform = `scale($ {scale})` ; //添加動(dòng)畫 requestAnimation({ ele:obj.children [i], attr:[ “top”,“left”,“opacity” ], 值:[ 150,-150,.8 ], time:timer, flag:false, fn:function() { requestAnimation({ ELE:obj.children [I], ATTR:“頂”,“左”,“不透明” ], 值:[ 150,-150,0 ], 時(shí)間:定時(shí)器, 標(biāo)志:假, FN:() => { obj.parent.removeChild(obj.children [I]); //動(dòng)畫結(jié)束刪除節(jié)點(diǎn) } }) } }); } },1000);
這里邊用到了我自己封裝的兩個(gè)方法,一個(gè)是基于requestAnimationFrame的requestAnimation,以及基于appendChild的addChild。
為了達(dá)成星星位置隨機(jī)的效果,通過定時(shí)器的setInterval的不停插入與刪除流星:
首先,每次添加2個(gè)流星到頁面,但是定時(shí)器的間隔時(shí)間小于流星的動(dòng)畫時(shí)間,這樣就能保證頁面中的流星的數(shù)量不是一個(gè)固定值,但肯定是大于2的。不然一次2個(gè)流星略顯冷清;
然后,通過對循環(huán)(也可以用為式,換的,都行。對于-的最簡單)給每個(gè)新添加到頁面中的流星一個(gè)隨機(jī)的位置(頂部,左側(cè)),隨機(jī)的大?。ㄒ?guī)模),隨機(jī)的動(dòng)畫執(zhí)行時(shí)間(定時(shí)器);
最后,在用于循環(huán)中,給每個(gè)新添加到頁面中的流星加上動(dòng)畫,并通過回調(diào)函數(shù)在執(zhí)行完動(dòng)畫后刪除節(jié)點(diǎn)。這里要注意的是,要?jiǎng)赢嫹殖蓛蓚€(gè)階段(出現(xiàn)與消失,主要是opacity控制)。另外我這里的處理,每個(gè)流星都移動(dòng)相同的距離300px,這個(gè)距離我覺得也可以通過隨機(jī)數(shù)控制,但我犯了個(gè)懶,就沒有做。
4,小問題
目前我發(fā)現(xiàn)的問題有2個(gè):
一是DOM操作本身的問題頁面不停的添加與刪除節(jié)點(diǎn),造成不停地?;亓髋c重繪,很耗性能;
二是requestAnimationFrame本身的問題因?yàn)槎〞r(shí)器不斷在添加節(jié)點(diǎn),而requestAnimationFrame的特性- 當(dāng)離開當(dāng)前頁面去瀏覽其他頁面時(shí),動(dòng)畫會(huì)暫停。這就造成了一個(gè)問題,節(jié)點(diǎn)一直在加,但動(dòng)畫全在停那沒有執(zhí)行那么下次再回到這個(gè)頁面的時(shí)候,就吊桿!!!動(dòng)畫就炸了,你會(huì)看到畫面一卡,很多小蝌蚪集體出動(dòng)去找媽媽。
5,結(jié)語
這個(gè)小案例雖然從難度上來看很簡單,但是它可拓展性很高 - 比如表個(gè)白啊,寄個(gè)相思,耍個(gè)浪漫啊等等,用純CSS也可以實(shí)現(xiàn)。
以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
- CSS Grid 是一種二維布局系統(tǒng),可以同時(shí)控制行和列,相比 Flex(一維布局),更適合用在整體頁面布局或復(fù)雜模塊結(jié)構(gòu)中,這篇文章主要介紹了前端CSS Grid 布局詳解,需要的朋2025-04-16
CSS Padding 和 Margin 區(qū)別全解析
CSS 中的 padding 和 margin 是兩個(gè)非常基礎(chǔ)且重要的屬性,它們用于控制元素周圍的空白區(qū)域,本文將詳細(xì)介紹 padding 和 margin 的概念、區(qū)別以及如何在實(shí)際項(xiàng)目中使用它們2025-04-07- will-change 是一個(gè) CSS 屬性,用于告訴瀏覽器某個(gè)元素在未來可能會(huì)發(fā)生哪些變化,本文給大家介紹CSS will-change 屬性詳解,感興趣的朋友一起看看吧2025-04-07
- 本文給大家分享在 CSS 中,去除a標(biāo)簽(超鏈接)的下劃線的幾種方法,本文給大家介紹的非常詳細(xì),感興趣的朋友一起看看吧2025-04-07
- 在前端開發(fā)中,CSS(層疊樣式表)不僅是用來控制網(wǎng)頁的外觀和布局,更是實(shí)現(xiàn)復(fù)雜交互和動(dòng)態(tài)效果的關(guān)鍵技術(shù)之一,隨著前端技術(shù)的不斷發(fā)展,CSS的用法也日益豐富和高級,本文將2025-04-07
css中的 vertical-align與line-height作用詳解
文章詳細(xì)介紹了CSS中的`vertical-align`和`line-height`屬性,包括它們的作用、適用元素、屬性值、常見使用場景、常見問題及解決方案,感興趣的朋友跟隨小編一起看看吧2025-03-26淺析CSS 中z - index屬性的作用及在什么情況下會(huì)失效
z-index屬性用于控制元素的堆疊順序,值越大,元素越顯示在上層,它需要元素具有定位屬性(如relative、absolute、fixed或sticky),本文給大家介紹CSS 中z - index屬性的作用2025-03-21- 文章詳細(xì)介紹了CSS中的打印媒體查詢@mediaprint包括基本語法、常見使用場景和代碼示例,如隱藏非必要元素、調(diào)整字體和顏色、處理鏈接的URL顯示、分頁控制、調(diào)整邊距和背景等2025-03-18
CSS模擬 html 的 title 屬性(鼠標(biāo)懸浮顯示提示文字效果)
本文介紹了如何使用CSS模擬HTML的title屬性,通過鼠標(biāo)懸浮顯示提示文字效果,通過設(shè)置`.tipBox`和`.tipBox.tipContent`的樣式,實(shí)現(xiàn)了提示內(nèi)容的隱藏和顯示,感興趣的朋友一起2025-03-10前端 CSS 動(dòng)態(tài)設(shè)置樣式::class、:style 等技巧(推薦)
本文介紹了Vue.js中動(dòng)態(tài)綁定類名和內(nèi)聯(lián)樣式的兩種方法:對象語法和數(shù)組語法,通過對象語法,可以根據(jù)條件動(dòng)態(tài)切換類名或樣式;通過數(shù)組語法,可以同時(shí)綁定多個(gè)類名或樣式,此外2025-02-26