【HTML+CSS+JavaScript】实现萤火虫闪烁效果

【HTML+CSS+JavaScript】实现萤火虫闪烁效果

本文主要讲解屏幕不断闪烁星光效果(配合夜晚深林的背景图片看上去就像一个个萤火虫在闪烁一样),当然温馨提示:小编在文章末尾附有完整的代码和图片获取链接。

一. 效果图

请添加图片描述

二. HTML部分代码

<!-- 引入外部js文件 -->
<script type="text/javascript" src="buffermove1.js"></script>

三. CSS部分代码

* {
    padding: 0px;
    margin: 0px;
}

body {
    width: 100%;
    height: 100%;
    background-image: url(bg.jpg);
    background-size: cover;
    position: relative;
    overflow: hidden;
}

img {
    width: 20px;
    height: 20px;
    position: absolute;
}

四. JavaScript部分代码

function Firework() {
    this.cw = document.documentElement.clientWidth;
    this.ch = document.documentElement.clientHeight;
}
//创建图片
Firework.prototype.createfirework = function() {
    this.fire = document.createElement('img');
    this.fire.src = '1.jpg';
    this.fire.style.left = this.rannum(0, this.cw - this.fire.offsetWidth) + 'px';
    this.fire.style.top = this.rannum(0, this.ch - this.fire.offsetHeight) + 'px';
    document.body.appendChild(this.fire);
    this.firemove();
}
//图片运动
Firework.prototype.firemove = function() {
    var _this = this;
    buffermove(this.fire, {
        left: this.rannum(0, this.cw - this.fire.offsetWidth),
        top: this.rannum(0, this.ch - this.fire.offsetHeight)
    }, function() {
        _this.firemove();
    });
}
//随机数
Firework.prototype.rannum = function(min, max) {
    return Math.round(Math.random() * (max - min)) + min;
}
for (var i = 0; i < 60; i++) {
    new Firework().createfirework();
}

五. 外部js文件代码

function getstyle(obj, attr) {
    if (window.getComputedStyle) {
        //标准
        return getComputedStyle(obj)[attr]
    } else {
        //IE
        return obj.currentStyle[attr]
    }
}

function buffermove(obj, json, fn) {
    var speed = 0
    clearInterval(obj.timer)
    obj.timer = setInterval(function() {
        var bstop = true
        for (var attr in json) {
            var currentvalue = 0
            if (attr === 'opacity') {
                currentvalue = Math.round(getstyle(obj, attr) * 100)
            } else {
                currentvalue = parseInt(getstyle(obj, attr))
            }
            speed = (json[attr] - currentvalue) / 100
            speed = speed > 0 ? Math.ceil(speed) : Math.floor(speed)
            if (currentvalue != json[attr]) {
                if (attr === 'opacity') {
                    obj.style.opacity = (currentvalue + speed) / 100
                    obj.style.filter = 'alpha(opacity:' + (currentvalue + speed) + ')'
                } else {
                    obj.style[attr] = currentvalue + speed + 'px'
                }
                bstop = false
            }
        }
        if (bstop) {
            clearInterval(obj.timer)
            fn && fn()
        }
    }, 50)
}

六. 完整的代码和图片获取

完整的代码和图片获取方式

链接:https://pan.baidu.com/s/19vvZXOTmAHnqZIxwNS_Igg?pwd=yhc6 
提取码:yhc6
  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

繁星学编程

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值