用js做闪烁的星星

提示:

         1.随机数

         2.定时器

//第一种方法:
//用css动画+js写

<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>闪烁的星星</title>
    <style>
        *{
            margin: 0;
            padding: 0;
            list-style: none;
}

body{
            background-color: #000;
        }
span{
            width: 30px;
            height: 30px;
            /* 星星图片的地址 */
            background: url("aa.gif") no-repeat;   
            position: absolute;
            background-size:100% 100%;
            animation: flash 1s alternate infinite;
        }
        @keyframes flash{
            0% { opacity: 0; }
            100% { opacity: 1; }
        }
        span:hover{
            transform: scale(3, 3) rotate(180deg)!important;
            transition: all 1s;
        }       
    </style>
</head>

<body>
    <script>


 window.onload=function(){
// 1. 求出屏幕的尺寸

var screenW = document.documentElement.clientWidth;

var screenH = document.documentElement.clientHeight;

// 2. 动态创建星星

for (var i = 0; i < 150; i++){

    // 2.1 创建星星

    var span = document.createElement('span');

    document.body.appendChild(span);

    // 2.2 随机的坐标

    var x = parseInt(Math.random() * screenW);

    var y = parseInt(Math.random() * screenH);

    span.style.left = x + 'px';

    span.style.top = y + 'px';

    // 2.3 随机缩放

    var scale = Math.random() * 1.5;

    span.style.transform = 'scale(' + scale + ', ' + scale + ')';

    // 2.4 频率

    var rate = Math.random() * 1.5;

    span.style.animationDelay = rate + 's';

}
}

    </script>
</body>

</html>
//第二种方法:
//直接用js写

<style>
        body {
            background-color: black;
        }
    </style>
    <script>
        var num = 1;

        function xingxing() {
            var x = Math.floor(Math.random() * 1024);
            var y = Math.floor(Math.random() * 768);
            document.getElementById('one').innerHTML = `<div style="position: absolute;left:${x}px;top:${y}px;z-index: ${num};">
            <img src="./xing.gif" width="30" height="30" alt="">
        </div>`
            x = Math.floor(Math.random() * 1024);
            y = Math.floor(Math.random() * 768);
            document.getElementById('two').innerHTML = `<div style="position: absolute;left:${x}px;top:${y}px;z-index: ${num};">
            <img src="./xing.gif" width="30" height="30" alt="">
        </div>`

            x = Math.floor(Math.random() * 1024);
            y = Math.floor(Math.random() * 768);
            document.getElementById('three').innerHTML = `<div style="position: absolute;left:${x}px;top:${y}px;z-index: ${num};">
            <img src="./xing.gif" width="30" height="30" alt="">
        </div>`
            x = Math.floor(Math.random() * 1024);
            y = Math.floor(Math.random() * 768);
            document.getElementById('four').innerHTML = `<div style="position: absolute;left:${x}px;top:${y}px;z-index: ${num};">
            <img src="./xing.gif" width="30" height="30" alt="">
        </div>`
            x = Math.floor(Math.random() * 1024);
            y = Math.floor(Math.random() * 768);
            document.getElementById('five').innerHTML = `<div style="position: absolute;left:${x}px;top:${y}px;z-index: ${num};">
            <img src="./xing.gif" width="30" height="30" alt="">
        </div>`
            num++;
            setTimeout(xingxing,500);
        }
    </script>
</head>

<body onload="xingxing()">
    <div id="one">
    </div>
    <div id="two">

    </div>
    <div id="three">

    </div>
    <div id="four">

    </div>
    <div id="five">

    </div>

</body>

 

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
好的,下面是一个使用画布和CSS实现星星闪烁的背景图的示例代码: HTML: ``` <canvas id="stars"></canvas> <div class="background"></div> ``` CSS: ``` body { margin: 0; padding: 0; overflow: hidden; } #stars { position: absolute; top: 0; left: 0; z-index: -1; } .background { position: absolute; top: 0; left: 0; width: 100%; height: 100%; background-color: #000; opacity: 0.8; } ``` JavaScript: ``` var canvas = document.getElementById('stars'); var ctx = canvas.getContext('2d'); canvas.width = window.innerWidth; canvas.height = window.innerHeight; var stars = []; for (var i = 0; i < 100; i++) { stars.push({ x: Math.random() * canvas.width, y: Math.random() * canvas.height, radius: Math.random() * 1.5 + 0.5, alpha: Math.random() * 0.5 + 0.5, direction: Math.random() < 0.5 ? -1 : 1 }); } function draw() { ctx.clearRect(0, 0, canvas.width, canvas.height); for (var i = 0; i < stars.length; i++) { var star = stars[i]; ctx.beginPath(); ctx.arc(star.x, star.y, star.radius, 0, Math.PI * 2); ctx.fillStyle = 'rgba(255, 255, 255,' + star.alpha + ')'; ctx.fill(); star.x += star.direction * 0.1; if (star.x < 0 || star.x > canvas.width) { star.direction = -star.direction; } } requestAnimationFrame(draw); } draw(); ``` 这个代码将创建一个画布和一个半透明的黑色背景,并在画布上绘制100个随机闪烁星星星星将根据随机方向以0.1的速度移动,当星星到达画布的边缘时将改变方向。通过动画循环绘制星星,使其闪烁。 请注意,此示例代码的效果可能因浏览器和设备而异,具体效果可能需要根据您的具体需求进行调整。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值