< d..."/>
溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊(cè)×
其他方式登錄
點(diǎn)擊 登錄注冊(cè) 即表示同意《億速云用戶服務(wù)條款》

JS+CSS怎么弄流星雨的動(dòng)畫(huà)效果

發(fā)布時(shí)間:2020-10-19 14:54:57 來(lái)源:億速云 閱讀:144 作者:小新 欄目:web開(kāi)發(fā)

這篇文章給大家分享的是有關(guān)JS+CSS怎么弄流星雨的動(dòng)畫(huà)效果的內(nèi)容。小編覺(jué)得挺實(shí)用的,因此分享給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧。

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 ;
     溢出:隱藏;
 }

 / * ------------畫(huà)布------------ * / 
 .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 ;
     / *流星隨長(zhǎng)度逐漸縮小* / 
     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)畫(huà) 
setInterval(function() {
     const obj = addChild(“#sky”,“p”,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”,“l(fā)eft”,“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)畫(huà) 
setInterval(function() {
     const obj = addChild(“#stars”,“p”,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”,“p”,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)畫(huà)

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:[ “l(fā)eft”,“bottom” ],
                 value:flag%2?[-left,-bottom]:[left,bottom],
                 time:timer + = 500,
                 flag:false,
                 fn:function() {
                    requestAnimation({
                        ele:clouds [i],
                         attr:[ “l(fā)eft”,“bottom” ],
                         value:flag%2?[left,bottom]:[ -  left,-bottom],
                         time:timer,
                         flag:false
                    })
                }
            });
        }

        標(biāo)志++;
    },2000)

封裝方法

// -------------------------------------------動(dòng)畫(huà)---- ----------------------------------------------- 
//運(yùn)動(dòng)動(dòng)畫(huà),調(diào)用Tween.js 
// ele:dom | 班級(jí)| id | 標(biāo)簽節(jié)點(diǎn)| 類名| id名| 標(biāo)簽名,只支持選擇一個(gè)節(jié)點(diǎn),類類名以及標(biāo)簽名只能選擇頁(yè)面中第一個(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ù)對(duì)象返回,可以通過(guò)設(shè)置返回對(duì)象的屬性stop為true打斷動(dòng)畫(huà)
函數(shù) requestAnimation(obj) {
     // -------------------------------------參數(shù)設(shè)置--------------------------------------------- 
    //默認(rèn)屬性
    const參數(shù)= {
         ele:null,
         attr:null,
         value:null,
         time:1000,
         tween:“l(fā)inear”,
         flag:true,
         stop:false,
         fn:“”
    }

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

    // -------------------------------------動(dòng)畫(huà)設(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 = [];
            對(duì)于(讓我的 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)畫(huà)(時(shí)間戳) {
         如果(parameter.stop)返回 ; //打斷
        //存儲(chǔ)初始時(shí)間戳
        if(!start)start = timestamp;
        //已運(yùn)動(dòng)時(shí)間
        讓 t =時(shí)間戳 - 開(kāi)始;
        //判斷多段運(yùn)動(dòng)
        if(beginAttr instanceof  Array){
             // const len = beginAttr.length //存數(shù)組長(zhǎng)度,復(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)到
                        寫(xiě)入位置else curVal = Tween [tween](t,beginAttr [i],count + beginAttr [i],time); //調(diào)用Tween,返回當(dāng)前屬性值,此時(shí)計(jì)算方法為移動(dòng)了
                        寫(xiě)入距離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í)間的長(zhǎng)度不等于屬性的長(zhǎng)度”);
                        }

                        //判斷是否已經(jīng)完成動(dòng)畫(huà),完成則跳過(guò)此次循環(huán)
                        if(parseFloat(getComputedStyle(target)[attr [i]])===(typeof value === “number”?value:value [i]) )
                             繼續(xù) ;
                        // t =時(shí)間戳 - 開(kāi)始; //每次循環(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)到
                        寫(xiě)入位置else curVal = Tween [tween](t,beginAttr [i],count + beginAttr [i],i); //調(diào)用Tween,返回當(dāng)前屬性值,此時(shí)計(jì)算方法為移動(dòng)了
                        寫(xiě)入距離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í)間最長(zhǎng)的動(dòng)畫(huà),則調(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(
                                 “輸入值的長(zhǎng)度不是等于屬性的長(zhǎng)度“);
                        }

                        if(flag || attr [i] === “opacity”)curVal = Tween [tween](t,beginAttr [i],count [i],time); //調(diào)用Tween,返回當(dāng)前屬性值,此時(shí)計(jì)算方法為移動(dòng)到
                        寫(xiě)入位置else curVal = Tween [tween](t,beginAttr [i],count [i] + beginAttr [i],time); //調(diào)用Tween,返回當(dāng)前屬性值,此時(shí)計(jì)算方法為移動(dòng)了
                        寫(xiě)入距離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í)間最長(zhǎng)的動(dòng)畫(huà),則調(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í)間的長(zhǎng)度)不等于屬性的長(zhǎng)度“);
                        }

                        //判斷是否已經(jīng)完成動(dòng)畫(huà),完成則跳過(guò)此次循環(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(
                                 “輸入值的長(zhǎng)度不等于屬性的長(zhǎng)度”);
                        }

                        if(flag || attr [i] === “opacity”)curVal = Tween [tween](t,beginAttr [i],count [i],time [i]); //調(diào)用Tween,返回當(dāng)前屬性值,此時(shí)計(jì)算方法為移動(dòng)到
                        寫(xiě)入位置其他 curVal = Tween [tween](t,beginAttr [i],count [i] + beginAttr [i],time [i]) ; //調(diào)用Tween,返回當(dāng)前屬性值,此時(shí)計(jì)算方法為移動(dòng)了
                        寫(xiě)入距離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)到
        寫(xiě)入位置else curVal = Tween [tween](t,beginAttr [i],count + beginAttr,time); //調(diào)用Tween,返回當(dāng)前屬性值,此時(shí)計(jì)算方法為移動(dòng)了
        寫(xiě)入距離if(attr === “opacity”)target.style [attr] = curVal;
        否則 target.style [attr] = curVal + “px” ;

        if(t <time)requestAnimationFrame(animate);
        else callback && callback();

    }

    requestAnimationFrame(動(dòng)畫(huà));
    返回參數(shù); //返回對(duì)象,供打斷或其他用途
}
//Tween.js 
/ *
 * t:時(shí)間已過(guò)時(shí)間
 * b:開(kāi)始起始值
 * 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è)類名用空格隔開(kāi)
//布爾:是否選中所有目標(biāo)父節(jié)點(diǎn)。可選參數(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 ] === “?!保﹑arent = 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)的對(duì)象
    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)畫(huà)函數(shù)調(diào)用
    return obj;
}

案例解析

HTML

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

CSS

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

首先說(shuō)一下流星的樣式:

#sky  .star {
      position:absolute;
     不透明度:0 ;
     z-index:10000 ;
 }
 
 .star :: after {
      content:“” ;
     顯示:塊;
     邊界:堅(jiān)固;
     border-width:2px  0  2px  80px ;
     / *流星隨長(zhǎng)度逐漸縮小* / 
     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);
 }

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

然后在星后通過(guò)后偽類添加流星,用邊界特性畫(huà):

1)模型繪制:border-width的順序?yàn)樗倪卼op,right,bottom,left,同理border-color的順序也為四邊top,right,bottom,left。這樣將border-width與border-color一一對(duì)應(yīng)后,就能看出2px的是流星的寬度,80px是流星的長(zhǎng)度,而0像素流星就是尾巴的這樣就形成了一個(gè)。頭部2px的寬,尾部0像素,長(zhǎng)度80px的流星模型 ;

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

3)增加閃光:通過(guò)箱陰影給流星增加一點(diǎn)光暈,讓它看起來(lái)有閃光的效果;

通過(guò)以上3步,一個(gè)流星就畫(huà)好了。

然后是畫(huà)云:

因?yàn)樵频拇a比較長(zhǎng),這里就不貼出來(lái)了方法無(wú)非是通過(guò)一個(gè)一個(gè)的圓,相互疊加覆蓋,完成一個(gè)云朵的形狀。
完成一個(gè)云層之后,copy一個(gè),然后多個(gè)云層通過(guò)rotate,opacity,left定位等,做出一個(gè)漸隱疊加的立體效果;

JS

JS部分以流星舉例說(shuō)明

setInterval(function() {
     const obj = addChild(“#sky”,“p”,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)畫(huà)
        requestAnimation({
            ele:obj.children [i],
             attr:[ “top”,“l(fā)eft”,“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)畫(huà)結(jié)束刪除節(jié)點(diǎn)
                    }
                })
            }
        });
    }

},1000);

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

為了達(dá)成星星位置隨機(jī)的效果,通過(guò)定時(shí)器的setInterval的不停插入與刪除流星:

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

然后,通過(guò)對(duì)循環(huán)(也可以用為式,換的,都行。對(duì)于-的最簡(jiǎn)單)給每個(gè)新添加到頁(yè)面中的流星一個(gè)隨機(jī)的位置(頂部,左側(cè)),隨機(jī)的大?。ㄒ?guī)模),隨機(jī)的動(dòng)畫(huà)執(zhí)行時(shí)間(定時(shí)器);

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

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

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

二是requestAnimationFrame本身的問(wèn)題因?yàn)槎〞r(shí)器不斷在添加節(jié)點(diǎn),而requestAnimationFrame的特性- 當(dāng)離開(kāi)當(dāng)前頁(yè)面去瀏覽其他頁(yè)面時(shí),動(dòng)畫(huà)會(huì)暫停。這就造成了一個(gè)問(wèn)題,節(jié)點(diǎn)一直在加,但動(dòng)畫(huà)全在停那沒(méi)有執(zhí)行那么下次再回到這個(gè)頁(yè)面的時(shí)候,就吊桿!!!動(dòng)畫(huà)就炸了,你會(huì)看到畫(huà)面一卡,很多小蝌蚪集體出動(dòng)去找媽媽。

感謝各位的閱讀!關(guān)于JS+CSS怎么弄流星雨的動(dòng)畫(huà)效果就分享到這里了,希望以上內(nèi)容可以對(duì)大家有一定的幫助,讓大家可以學(xué)到更多知識(shí)。如果覺(jué)得文章不錯(cuò),可以把它分享出去讓更多的人看到吧!

向AI問(wèn)一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI