首页 > web前端 > js教程 > 正文

JS实现动态雪花飘落

php中世界最好的语言
发布: 2018-04-18 09:12:47
原创
2953人浏览过

这次给大家带来JS实现动态雪花飘落,JS实现动态雪花飘落的注意事项有哪些,下面就是实战案例,一起来看一下。

<!DOCTYPE html>
<html>
<head>
    <meta content="text/html" charset="utf-8">
    <title>飘雪</title>
    <style type="text/css">
        *{
            margin:0;
            padding:0;
            font-family:"微软雅黑";
            font-size:13px;
            color:#ffffff;
        }
        body{
            background:#00a0fc;
            overflow:hidden;
        }
    </style>
</head>
<body>
</body>
<script type="text/javascript" src="js/jquery-1.8.3.js"></script>
<script type="text/javascript">
//    窗口大小
    var oPageX=window.screen.width;
    var oPageY=window.screen.height;
    var aSnow=[];
//    雪花模板
    function snow(x,y,xspeed,yspeed,xsin,size){//x水平位置,y垂直位置,xspeed水平步距,yspeed垂直步距,xsin振幅,size雪花大小
        this.node=document.createElement('p');
        this.x=x;
        this.y=y;
        this.xspeed=xspeed;
        this.yspeed=yspeed;
        this.xsin=xsin;
        this.size=size;
        this.createSnow=function(){
            this.node.style.position='absolute';
            this.node.style.top=this.y+'px';
            this.node.style.left=this.x+'px';
            this.node.innerHTML='*';
            document.body.appendChild(this.node);
        };
        this.createSnow();
        this.snowMove=function(){
            this.node.style.top=parseInt(this.node.style.top)+this.yspeed+'px';//垂直方向运动
            this.node.style.left=parseInt(this.node.style.left)+this.xsin*Math.sin(this.xspeed)+'px';
            this.node.style.fontSize=this.size+'px';
//            this.node.style.color='rgb('+Math.ceil(Math.random()*255)+','+Math.ceil(Math.random()*255)+','+Math.ceil(Math.random()*255)+')';
        };
    }
//    创建雪花
    function createSnow(){
        aSnow.push(new snow(Math.random()*oPageX,-50,0.02+Math.random()/10,1+Math.random(),Math.random()*30,20+Math.random()*30));
    }
    setInterval(createSnow,1000);//一秒钟创建一个雪花
//    雪花移动函数
    function snowMove(){
        for(var j=0;j<aSnow.length;j++){
            aSnow[j].snowMove();
            if(parseInt(aSnow[j].node.style.top)>oPageY || parseInt(aSnow[j].node.style.left)>oPageX){
                aSnow[j].node.parentNode.removeChild(aSnow[j].node);
                aSnow.splice(j,1);
            }
        }
    }
    setInterval(snowMove,10);
</script>
</html>
登录后复制

相信看了本文案例你已经掌握了方法,更多精彩请关注php中文网其它相关文章!

推荐阅读:

JS+canvas绘制饼状统计图

ViiTor实时翻译
ViiTor实时翻译

AI实时多语言翻译专家!强大的语音识别、AR翻译功能。

ViiTor实时翻译 116
查看详情 ViiTor实时翻译

react-native-fs插件使用案列详解

以上就是JS实现动态雪花飘落的详细内容,更多请关注php中文网其它相关文章!

相关标签:
最佳 Windows 性能的顶级免费优化软件
最佳 Windows 性能的顶级免费优化软件

每个人都需要一台速度更快、更稳定的 PC。随着时间的推移,垃圾文件、旧注册表数据和不必要的后台进程会占用资源并降低性能。幸运的是,许多工具可以让 Windows 保持平稳运行。

下载
来源:php中文网
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn
最新问题
开源免费商场系统广告
热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板
关于我们 免责申明 举报中心 意见反馈 讲师合作 广告合作 最新更新 English
php中文网:公益在线php培训,帮助PHP学习者快速成长!
关注服务号 技术交流群
PHP中文网订阅号
每天精选资源文章推送
PHP中文网APP
随时随地碎片化学习

Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号