国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 開發(fā) > CSS > 正文

CSS 、JS實現(xiàn)浪漫流星雨動畫

2024-07-11 08:59:37
字體:
來源:轉載
供稿:網(wǎ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:“” ;     顯示:塊;     邊界:堅固;     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 ;     變換:旋轉(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

//流星動畫 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 ],                     時間:定時器,                     標志:假,                     FN:() => {                        obj.parent.removeChild(obj.children [I]);                    }                })            }        });    }},1000);//閃爍星星動畫 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);//月亮移動requestAnimation({    ele:“#moon”,     attr:“right”,     值:1200,     時間: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} `);    }}//云動畫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                    })                }            });        }        標志++;    },2000)

封裝方法

// -------------------------------------------動畫---- ----------------------------------------------- //運動動畫,調用Tween.js // ele:dom | 班級| id | 標簽節(jié)點| 類名| id名| 標簽名,只支持選擇一個節(jié)點,類類名以及標簽名只能選擇頁面中第一個// attr:屬性屬性名//值:目標值目標值//時間:持續(xù)時間持續(xù)時間//補間:定時function函數(shù)方程// flag:Boolean判斷是按值移動還是按位置移動,默認按位置移動// fn:callback回調函數(shù)//增加返回值:將內部參數(shù)對象返回,可以通過設置返回對象的屬性stop為true打斷動畫函數(shù) requestAnimation(obj) {     // -------------------------------------參數(shù)設置---------------------------------------------     //默認屬性    const參數(shù)= {         ele:null,         attr:null,         value:null,         time:1000,         tween:“linear”,         flag:true,         stop:false,         fn:“”    }    //合并傳入屬性    Object .assign(parameter,obj); //覆蓋重名屬性    // -------------------------------------動畫設置--------- ------------------------------------     //創(chuàng)建運動方程初始參數(shù),方便復用    let start = 0 ; //用于保存初始時間戳    let target =(typeof parameter.ele === “string”?document .querySelector(parameter.ele):parameter.ele),//目標節(jié)點         attr = parameter.attr,//目標屬性         beginAttr = parseFloat(getComputedStyle(target)[attr]),// attr起始值         value = parameter.value,//運動目標值         count = value  -  beginAttr,//實際運動值         time = parameter.time,//運動持續(xù)時間,        tween = parameter.tween,//運動函數(shù)        flag = parameter.flag,        callback = parameter.fn,//回調函數(shù)         curVal = 0 ; //運動當前值    //判斷傳入函數(shù)是否為數(shù)組,多段運動     (function() {         if(attr instanceof  Array){            beginAttr = [];            count = [];            對于(讓我的 ATTR){                 常量 VAL = parseFloat(的getComputedStyle(目標)[I]);                beginAttr.push(VAL);                count.push(value  -  val);            }        }        if(value instanceof  Array){             for(let i in value){                count [i] = value [i]  -  beginAttr [i];            }        }    })();    //運動函數(shù)    功能 動畫(時間戳) {         如果(parameter.stop)返回 ; //打斷        //存儲初始時間戳        if(!start)start = timestamp;        //已運動時間        讓 t =時間戳 - 開始;        //判斷多段運動        if(beginAttr instanceof  Array){             // const len = beginAttr.length //存數(shù)組長度,復用            //多段運動第1類 - 多屬性,同目標,同時間/不同時間            if(typeof count === “number”){ //同目標                //同時間                if(typeof time === “number”){                     if(t> time)t = time; //判斷是否超出目標值                    //循環(huán)attr,分別賦值                    為(let i in beginAttr){                         if(flag)curVal = Tween [tween](t,beginAttr [i],count,time); //調用Tween,返回當前屬性值,此時計算方法為移動到                        寫入位置else curVal = Tween [tween](t,beginAttr [i],count + beginAttr [i],time); //調用Tween,返回當前屬性值,此時計算方法為移動了                        寫入距離if(attr [i] === “opacity”)target.style [attr [i]] = curVal; //給屬性賦值                        else target.style [attr [i]] = curVal + “px” ; //給屬性賦值                        if(t <time)requestAnimationFrame(animate); //判斷是否運動完                        其他回調&& callback(); //調用回調函數(shù)                    }                    回歸 ;                }                //不同時間                if(time instanceof  Array){                     //循環(huán)時間,attr,分別賦值                    為(讓我在 beginAttr中){                         //錯誤判斷                        if(!time [i] && time [i]!== 0){                             throw  new  Error(                                 “輸入時間的長度不等于屬性的長度”);                        }                        //判斷是否已經(jīng)完成動畫,完成則跳過此次循環(huán)                        if(parseFloat(getComputedStyle(target)[attr [i]])===(typeof value === “number”?value:value [i]) )                             繼續(xù) ;                        // t =時間戳 - 開始; //每次循環(huán)初始化t                         if(t> time [i])t = time [i]; //判斷是否超出目標值                        if(flag || attr [i] === “opacity”)curVal = Tween [tween](t,beginAttr [i],count,i); //調用Tween,返回當前屬性值,此時計算方法為移動到                        寫入位置else curVal = Tween [tween](t,beginAttr [i],count + beginAttr [i],i); //調用Tween,返回當前屬性值,此時計算方法為移動了                        寫入距離if(attr [i] === “opacity”)target.style [attr [i]] = curVal; //給屬性賦值                        else target.style [attr [i]] = curVal + “px” ; //給屬性賦值                    }                    if(t < Math .max(... time))requestAnimationFrame(animate); //判斷函數(shù)是否運動完                    其他回調&& callback(); //如果已經(jīng)執(zhí)行完時間最長的動畫,則調查回調函數(shù)                    return ;                }            }            //多段運動第2類 - 多屬性,不同目標,同時間/不同時間            if(count instanceof  Array){                 //同時間                if(typeof time === “number”){                    if(t> time)t = time; //判斷是否超出目標值                    for(let i in beginAttr){ //循環(huán)attr,count,分別賦值                        //錯誤判斷                        if(!count [i] && count [i]!== 0){                             throw  new  Error(                                 “輸入值的長度不是等于屬性的長度“);                        }                        if(flag || attr [i] === “opacity”)curVal = Tween [tween](t,beginAttr [i],count [i],time); //調用Tween,返回當前屬性值,此時計算方法為移動到                        寫入位置else curVal = Tween [tween](t,beginAttr [i],count [i] + beginAttr [i],time); //調用Tween,返回當前屬性值,此時計算方法為移動了                        寫入距離if(attr [i] === “opacity”)target.style [attr [i]] = curVal; //給屬性賦值                        else target.style [attr [i]] = curVal + “px” ; //給屬性賦值                    }                    if(t <time)requestAnimationFrame(animate); //判斷函數(shù)是否運動完                    其他回調&& callback(); //如果已經(jīng)執(zhí)行完時間最長的動畫,則調查回調函數(shù)                    return ;                }                //不同時間                if(time instanceof  Array){                     for(let i in beginAttr){                         //錯誤判斷                        if(!time [i] && time [i]!== 0){                             throw  new  Error(                                 “輸入時間的長度)不等于屬性的長度“);                        }                        //判斷是否已經(jīng)完成動畫,完成則跳過此次循環(huán)                        if(parseFloat(getComputedStyle(target)[attr [i]])===(typeof value === “number”?value:value [i]) )                             繼續(xù) ;                        if(t> time [i])t = time [i]; //判斷是否超出目標值                        //錯誤判斷                        if(!count [i] && count [i]!== 0){                             throw  new  Error(                                 “輸入值的長度不等于屬性的長度”);                        }                        if(flag || attr [i] === “opacity”)curVal = Tween [tween](t,beginAttr [i],count [i],time [i]); //調用Tween,返回當前屬性值,此時計算方法為移動到                        寫入位置其他 curVal = Tween [tween](t,beginAttr [i],count [i] + beginAttr [i],time [i]) ; //調用Tween,返回當前屬性值,此時計算方法為移動了                        寫入距離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();                    回歸 ;                }            }        }        //單運動模式        if(t> time)t = time;        if(flag || attr === “opacity”)curVal = Tween [tween](t,beginAttr,count,time); //調用Tween,返回當前屬性值,此時計算方法為移動到        寫入位置else curVal = Tween [tween](t,beginAttr [i],count + beginAttr,time); //調用Tween,返回當前屬性值,此時計算方法為移動了        寫入距離if(attr === “opacity”)target.style [attr] = curVal;        否則 target.style [attr] = curVal + “px” ;        if(t <time)requestAnimationFrame(animate);        else callback && callback();    }    requestAnimationFrame(動畫);    返回參數(shù); //返回對象,供打斷或其他用途}//Tween.js / * * t:時間已過時間 * b:開始起始值 * c:計算總的運動值 * d:持續(xù)時間持續(xù)時間 * *曲線方程 * * 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) { //正弦衰減曲線(彈動漸入)        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ù)學 .pow(2,10 *(T - = 1))* 數(shù)學 .sin((T * d - S)*(2 * 數(shù)學 .PI)/ P))+ B;    },    elasticOut:function(t,b,c,d,a,p) { //正弦增強曲線(彈動漸出)        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ù)學 .pow(2,10 *(T - = 1))*                 數(shù)學 .sin((T * d - S)*(2 * 數(shù)學 .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é)點// ele:父節(jié)點,支持輸入變量,id值,類值,標簽值。除變量外,其余值必須為字符串//節(jié)點:添加的標簽名,值為字符串// n:節(jié)點添加個數(shù)// className:節(jié)點綁定的類名,值為字符串,多個類名用空格隔開//布爾:是否選中所有目標父節(jié)點。可選參數(shù),不輸入則判定為false,則只匹配選中的第一個節(jié)點函數(shù) addChild(ele,node,n,className,boolean) {     //獲取節(jié)點    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);    }    //聲明用于存儲父節(jié)點及子節(jié)點的對象    const obj = {         “parent”:parent,         “children”:[]    };    //添加節(jié)點    if(boolean){         for(let i = 0 ; i <parent.length; i ++){             //創(chuàng)建容器碎片            const fragment = document .createDocumentFragment();            //保存子節(jié)點,用于返回值            obj.children [i] = [];            for(let j = 0 ; j <n; j ++){                 const target = document .createElement(node);                target.className = className;                fragment.appendChild(目標);                //添加子節(jié)點到數(shù)組,用于返回值                obj.children [i] [j] =目標;            }            父[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(目標);            //添加子節(jié)點,用于返回值            obj.children [i] =目標;        }        //將碎片容器一次性添加到父節(jié)點        parent.appendChild(片段);    }    //返回參數(shù),供動畫函數(shù)調用    return obj;}

3,案例解析

HTML

由于節(jié)點很多,并且我想盡量做得逼真有趣有點,還給節(jié)點加了隨機位置。所以節(jié)點的輸出都是用JS控制的,HTML這邊只寫了幾個父元素盒子,加上相應的ID名和類類名,結構相對簡單。

CSS

CSS部分的難點就是流星的樣式和用圈圈畫云層,然后將云層堆疊出立體效果。

首先說一下流星的樣式:

#sky  .star {      position:absolute;     不透明度:0 ;     z-index:10000 ; }  .star :: after {      content:“” ;     顯示:塊;     邊界:堅固;     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的順序為四邊top,right,bottom,left,同理border-color的順序也為四邊top,right,bottom,left。這樣將border-width與border-color一一對應后,就能看出2px的是流星的寬度,80px是流星的長度,而0像素流星就是尾巴的這樣就形成了一個。頭部2px的寬,尾部0像素,長度80px的流星模型 ;

2)稍微逼真:通過邊界半徑?給流星的頭部增加個圓角,讓它看起來更逼真最后通過roteta旋轉一個角度,讓它看起來像是往下掉;

3)增加閃光:通過箱陰影給流星增加一點光暈,讓它看起來有閃光的效果;

通過以上3步,一個流星就畫好了。

然后是畫云:

因為云的代碼比較長,這里就不貼出來了方法無非是通過一個一個的圓,相互疊加覆蓋,完成一個云朵的形狀。
完成一個云層之后,copy一個,然后多個云層通過rotate,opacity,left定位等,做出一個漸隱疊加的立體效果;

JS

JS部分以流星舉例說明

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 ],                     時間:定時器,                     標志:假,                     FN:() => {                        obj.parent.removeChild(obj.children [I]); //動畫結束刪除節(jié)點                    }                })            }        });    }},1000);

這里邊用到了我自己封裝的兩個方法,一個是基于requestAnimationFrame的requestAnimation,以及基于appendChild的addChild。

為了達成星星位置隨機的效果,通過定時器的setInterval的不停插入與刪除流星:

首先,每次添加2個流星到頁面,但是定時器的間隔時間小于流星的動畫時間,這樣就能保證頁面中的流星的數(shù)量不是一個固定值,但肯定是大于2的。不然一次2個流星略顯冷清;

然后,通過對循環(huán)(也可以用為式,換的,都行。對于-的最簡單)給每個新添加到頁面中的流星一個隨機的位置(頂部,左側),隨機的大小(規(guī)模),隨機的動畫執(zhí)行時間(定時器);

最后,在用于循環(huán)中,給每個新添加到頁面中的流星加上動畫,并通過回調函數(shù)在執(zhí)行完動畫后刪除節(jié)點。這里要注意的是,要動畫分成兩個階段(出現(xiàn)與消失,主要是opacity控制)。另外我這里的處理,每個流星都移動相同的距離300px,這個距離我覺得也可以通過隨機數(shù)控制,但我犯了個懶,就沒有做。

4,小問題

目前我發(fā)現(xiàn)的問題有2個:

一是DOM操作本身的問題頁面不停的添加與刪除節(jié)點,造成不停地。回流與重繪,很耗性能;

二是requestAnimationFrame本身的問題因為定時器不斷在添加節(jié)點,而requestAnimationFrame的特性- 當離開當前頁面去瀏覽其他頁面時,動畫會暫停。這就造成了一個問題,節(jié)點一直在加,但動畫全在停那沒有執(zhí)行那么下次再回到這個頁面的時候,就吊桿!!!動畫就炸了,你會看到畫面一卡,很多小蝌蚪集體出動去找媽媽。

5,結語

這個小案例雖然從難度上來看很簡單,但是它可拓展性很高 - 比如表個白啊,寄個相思,耍個浪漫啊等等,用純CSS也可以實現(xiàn)。

以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持武林網(wǎng)。

發(fā)表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發(fā)表
主站蜘蛛池模板: 四平市| 平阳县| 徐闻县| 巨鹿县| 容城县| 宜兰市| 小金县| 枣阳市| 西藏| 玛沁县| 衢州市| 贵州省| 探索| 开鲁县| 临沭县| 林甸县| 砀山县| 东明县| 确山县| 新郑市| 盖州市| 镇赉县| 外汇| 新丰县| 浮梁县| 沽源县| 上思县| 金山区| 集贤县| 桑植县| 京山县| 天峨县| 崇义县| 绥棱县| 调兵山市| 苏尼特左旗| 武义县| 墨竹工卡县| 达孜县| 桐乡市| 塔河县|