溫馨提示×

溫馨提示×

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

密碼登錄×
登錄注冊×
其他方式登錄
點擊 登錄注冊 即表示同意《億速云用戶服務條款》

html+css+js怎么實現(xiàn)星空旋轉和文字淡入效果

發(fā)布時間:2021-07-14 09:57:58 來源:億速云 閱讀:287 作者:chen 欄目:web開發(fā)

這篇文章主要講解了“html+css+js怎么實現(xiàn)星空旋轉和文字淡入效果”,文中的講解內容簡單清晰,易于學習與理解,下面請大家跟著小編的思路慢慢深入,一起來研究和學習“html+css+js怎么實現(xiàn)星空旋轉和文字淡入效果”吧!

廢話不多說直接上代碼,效果是一個星空旋轉和文字逐漸出現(xiàn)的效果,文字是逐漸出現(xiàn)的,星空中的小球是旋轉的

<!DOCTYPE html><html lang="en"><head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>星空特效</title>
    <style>
        *{
            margin: 0;
            padding: 0;
        }
        body{
            overflow: hidden;
            background-color: #000;
        }
        h2{
            position: absolute;
            line-height: 50px;
            letter-spacing: 5px;
            color: #fff;
            width: 300px;
            top: 40%;
            left: 50%;
            margin-left: -200px;
            font-size: 30px;
        }
    </style></head><body>
    <h2>
      
    </h2>
    <canvas></canvas>
    <script>
        var canvas=document.querySelector("canvas");
            ctx=canvas.getContext("2d");
            w=canvas.width=window.innerWidth;
            h=canvas.height=window.innerHeight;
            var canvas2=document.createElement("canvas");
                ctx2=canvas2.getContext("2d");
                canvas2.width=100;
                canvas2.height=100;
            var a=canvas2.width/2;
        var grandient=ctx.createRadialGradient(a,a,0,a,a,a);
            grandient.addColorStop(0.025,'#fff');
            grandient.addColorStop(0.1, 'hsl(220,59%,18%)');
            grandient.addColorStop(0.025, 'hsl(220,60%,33%)');
            grandient.addColorStop(1,"transparent");
            ctx2.fillStyle=grandient;
            ctx2.beginPath();
            ctx2.arc(a,a,a,0,Math.PI*2);
            ctx2.fill();
            ctx2.closePath();
            var stars=[];
            var count=0;
            function Star(){
                this.pos=Math.floor(Math.random()* w/2-100);
                this.r=Math.floor(Math.random()*100);
                this.dx=w/2;
                this.dy=h/2;
                this.rand=Math.floor(Math.random()*360);
                this.speed=this.pos/100000;
                stars[count]=this;
                count ++;
            }
            Star.prototype.draw=function(){
                var x=Math.sin(this.rand+1)* this.pos+this.dx;
                    y=Math.cos(this.rand)*this.pos/2+this.dy;
                ctx.drawImage(canvas2,x-this.r/2,y-this.r/2,this.r,this.r);
                this.rand=this.rand+this.speed;
            }
            for(var i=0;i<1000;i++){
                new Star();
            }
            function anmit(){
                ctx.clearRect(0,0,w,h);
                for(var i=0;i<stars.length;i++){
                    stars[i].draw();
                }
                requestAnimationFrame(anmit);
            }
            anmit();
            var oH=document.getElementsByTagName("h2")[0];
            var arr=["這世間過于俗氣","不像你一般","浩瀚星辰,溫柔婉轉"],
                index=0,
                arrLen=arr.length,
                strLen=arr[0].length;
                pos=0,
                row=0,
                str="",
                timer=null;
            function print() {
                while(row<index){
                    str=str+arr[row]+"<br>";
                    row++;
                }
                oH.innerHTML=str+arr[index].substring(0,pos);
                if(pos==strLen){
                    index++;
                    pos =0;
                    if(index<arr.length){
                        strLen=arr[index].length;
                        timer=setTimeout(print,250);
                    }
                }else{
                    pos++;
                    timer=setTimeout(print,250);
                }
            }
            setTimeout(print,250);
    </script></body></html>

效果圖:

html+css+js怎么實現(xiàn)星空旋轉和文字淡入效果

感謝各位的閱讀,以上就是“html+css+js怎么實現(xiàn)星空旋轉和文字淡入效果”的內容了,經過本文的學習后,相信大家對html+css+js怎么實現(xiàn)星空旋轉和文字淡入效果這一問題有了更深刻的體會,具體使用情況還需要大家實踐驗證。這里是億速云,小編將為大家推送更多相關知識點的文章,歡迎關注!

向AI問一下細節(jié)

免責聲明:本站發(fā)布的內容(圖片、視頻和文字)以原創(chuàng)、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯(lián)系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。

AI