web前端入門到實戰:CSS 、JS實現浪漫流星雨動畫

1,效果圖

web前端入門到實戰:CSS 、JS實現浪漫流星雨動畫

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 >
web前端開發學習Q-q-u-n: 731771211,分享學習的方法和需要注意的小細節,不停更新最新的教程和學習方法(詳細的前端項目實戰教學視頻)

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 ;
     變換:規模(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 | 標籤節點| 類名| id名| 標籤名,只支持選擇一個節點,類類名以及標籤名只能選擇頁面中第一個
// attr:屬性屬性名
//值:目標值目標值
//時間:持續時間持續時間
//補間:定時function函數方程
// flag:Boolean判斷是按值移動還是按位置移動,默認按位置移動
// fn:callback回調函數
//增加返回值:將內部參數對象返回,可以通過設置返回對象的屬性stop爲true打斷動畫
函數 requestAnimation(obj) {
     // -------------------------------------參數設置--------------------------------------------- 
    //默認屬性
    const參數= {
         ele:null,
         attr:null,
         value:null,
         time:1000,
         tween:“linear”,
         flag:true,
         stop:false,
         fn:“”
    }

    //合併傳入屬性
    Object .assign(parameter,obj); //覆蓋重名屬性

    // -------------------------------------動畫設置--------- ------------------------------------ 
    //創建運動方程初始參數,方便複用
    let start = 0 ; //用於保存初始時間戳
    let target =(typeof parameter.ele === “string”?document .querySelector(parameter.ele):parameter.ele),//目標節點 
        attr = parameter.attr,//目標屬性 
        beginAttr = parseFloat(getComputedStyle(target)[attr]),// attr起始值 
        value = parameter.value,//運動目標值 
        count = value  -  beginAttr,//實際運動值 
        time = parameter.time,//運動持續時間,
        tween = parameter.tween,//運動函數
        flag = parameter.flag,
        callback = parameter.fn,//回調函數 
        curVal = 0 ; //運動當前值

    //判斷傳入函數是否爲數組,多段運動 
    (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];
            }
        }
    })();

    //運動函數
    功能 動畫(時間戳) {
         如果(parameter.stop)返回 ; //打斷
        //存儲初始時間戳
        if(!start)start = timestamp;
        //已運動時間
        讓 t =時間戳 - 開始;
        //判斷多段運動
        if(beginAttr instanceof  Array){
             // const len = beginAttr.length //存數組長度,複用

            //多段運動第1類 - 多屬性,同目標,同時間/不同時間
            if(typeof count === “number”){ //同目標
                //同時間
                if(typeof time === “number”){
                     if(t> time)t = time; //判斷是否超出目標值

                    //循環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(); //調用回調函數
                    }
                    迴歸 ;
                }

                //不同時間
                if(time instanceof  Array){
                     //循環時間,attr,分別賦值
                    爲(讓我在 beginAttr中){
                         //錯誤判斷
                        if(!time [i] && time [i]!== 0){
                             throw  new  Error(
                                 “輸入時間的長度不等於屬性的長度”);
                        }

                        //判斷是否已經完成動畫,完成則跳過此次循環
                        if(parseFloat(getComputedStyle(target)[attr [i]])===(typeof value === “number”?value:value [i]) )
                             繼續 ;
                        // t =時間戳 - 開始; //每次循環初始化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); //判斷函數是否運動完
                    其他回調&& callback(); //如果已經執行完時間最長的動畫,則調查回調函數
                    return ;
                }
            }

            //多段運動第2類 - 多屬性,不同目標,同時間/不同時間
            if(count instanceof  Array){
                 //同時間
                if(typeof time === “number”){

                    if(t> time)t = time; //判斷是否超出目標值

                    for(let i in beginAttr){ //循環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); //判斷函數是否運動完
                    其他回調&& callback(); //如果已經執行完時間最長的動畫,則調查回調函數
                    return ;
                }

                //不同時間
                if(time instanceof  Array){
                     for(let i in beginAttr){
                         //錯誤判斷
                        if(!time [i] && time [i]!== 0){
                             throw  new  Error(
                                 “輸入時間的長度)不等於屬性的長度“);
                        }

                        //判斷是否已經完成動畫,完成則跳過此次循環
                        if(parseFloat(getComputedStyle(target)[attr [i]])===(typeof value === “number”?value:value [i]) )
                             繼續 ;

                        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(動畫);
    返回參數; //返回對象,供打斷或其他用途
}
//Tween.js 
/ *
 * t:時間已過時間
 * b:開始起始值
 * c:計算總的運動值
 * d:持續時間持續時間
 *
 *曲線方程
 *
 * 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 * 數學 .pow(2,10 *(T - = 1))* 數學 .sin((T * d - S)*(2 * 數學 .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 * 數學 .pow(2,10 *(T - = 1))*
                 數學 .sin((T * d - S)*(2 * 數學 .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:函數(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操作--- ------------------------------------------------ 
//添加節點
// ele:父節點,支持輸入變量,id值,類值,標籤值。除變量外,其餘值必須爲字符串
//節點:添加的標籤名,值爲字符串
// n:節點添加個數
// className:節點綁定的類名,值爲字符串,多個類名用空格隔開
//布爾:是否選中所有目標父節點。可選參數,不輸入則判定爲false,則只匹配選中的第一個節點
函數 addChild(ele,node,n,className,boolean) {
     //獲取節點
    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);
    }

    //聲明用於存儲父節點及子節點的對象
    const obj = {
         “parent”:parent,
         “children”:[]
    };

    //添加節點
    if(boolean){
         for(let i = 0 ; i <parent.length; i ++){
             //創建容器碎片
            const fragment = document .createDocumentFragment();
            //保存子節點,用於返回值
            obj.children [i] = [];

            for(let j = 0 ; j <n; j ++){
                 const target = document .createElement(node);
                target.className = className;
                fragment.appendChild(目標);
                //添加子節點到數組,用於返回值
                obj.children [i] [j] =目標;
            }

            父[I] .appendChild(片段)
        }
    } else {
         //創建碎片容器
        const fragment = document .createDocumentFragment();

        for(let i = 0 ; i <n; i ++){
             const target = document .createElement(node);
            target.className = className;
            fragment.appendChild(目標);
            //添加子節點,用於返回值
            obj.children [i] =目標;
        }
        //將碎片容器一次性添加到父節點
        parent.appendChild(片段);
    }

    //返回參數,供動畫函數調用
    return obj;
}

3,案例解析

HTML

由於節點很多,並且我想盡量做得逼真有趣有點,還給節點加了隨機位置。所以節點的輸出都是用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);
 }
web前端開發學習Q-q-u-n: 784783012 ,分享學習的方法和需要注意的小細節,不停更新最新的教程和學習方法(詳細的前端項目實戰教學視頻)

先提取了公共樣式,添加定位屬性;

然後在星後通過後僞類添加流星,用邊界特性畫:

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]); //動畫結束刪除節點
                    }
                })
            }
        });
    }

},1000);
web前端開發學習Q-q-u-n: 731771211,分享學習的方法和需要注意的小細節,不停更新最新的教程和學習方法(詳細的前端項目實戰教學視頻)

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

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

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

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

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

4,小問題

目前我發現的問題有2個:

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

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

5,結語

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

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章