html+css+js实现星空旋转和文字淡入效果(附代码)


本文摘自PHP中文网,作者奋力向前,侵删。

本篇文章给大家通过代码示例介绍一下使用html+css+js如何实现一个星空旋转和文字逐渐出现的效果。有一定的参考价值,有需要的朋友可以参考一下,希望对你们有所助。

废话不多说直接上代码,效果是一个星空旋转和文字逐渐出现的效果,文字是逐渐出现的,星空中的小球是旋转的

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

43

44

45

46

47

48

49

50

51

52

53

54

55

56

57

58

59

60

61

62

63

64

65

66

67

68

69

70

71

72

73

74

75

76

77

78

79

80

81

82

83

84

85

86

87

88

89

90

91

92

93

94

95

96

97

98

99

100

101

102

103

104

105

106

107

<!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;

        }

        h1{

            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>

    <h1>

       

    </h1>

    <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("h1")[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>

效果图:

阅读剩余部分

相关阅读 >>

css 的位置怎么设置

Html的空格代码怎么写?教你如何使用空格nbsp代码

css flex-wrap属性怎么用

css border-top-color属性怎么用

css如何正确命名

css怎么让图片旋转90度

css怎么设置p标签不换行

怎么用css设置字体颜色

分享div+css前端命名的规则

利用Html+css+js实现简单的点赞效果

更多相关阅读请进入《Html》频道 >>




打赏

取消

感谢您的支持,我会继续努力的!

扫码支持
扫码打赏,您说多少就多少

打开支付宝扫一扫,即可进行扫码打赏哦

分享从这里开始,精彩与您同在

评论

管理员已关闭评论功能...