0
点赞
收藏
分享

微信扫一扫

jQuery实现下雪雪花漂亮案例

效果图:

 源码如下:

<!doctype html>
<html>
<head>
    <meta charset="utf-8">
    <title>jquery实现下雪功能</title>
    <script src="http://libs.baidu.com/jquery/1.11.3/jquery.min.js"></script>
    <style>
        * {
            margin: 0;
            padding: 0;
        }
        body {
            position: relative;
            height: 1000px;
            width: 100%;
            overflow: hidden;
            background-color: #666;
        }
        span {
            display: block;
            opacity: 0.7;
        }
    </style>
</head>
<body>
<script>
    $(function () {
        setInterval(function () {
            const maxW = document.body.clientWidth,
                maxH = document.body.clientHeight,
                left = Math.random() * maxW,
                bottom = left - (Math.random() - 0.5) * 0.2 * maxW, //保证落下的位置水平有变化,但不大
                opacity = 0.7 + 0.3 * Math.random();
            const speed = 30;
            const size = 20 + 10 * Math.random(), //字体20-30
                color = '#fff';
            // num = Math.floor(Math.random() * 10) //产生0-9随机数,当然你们可以自己设置
            const num = '*';
            const style = 'position:absolute;top:0px;font-size:' + size + 'px;color:' + color + ';left:' + left + 'px;opacity:' + opacity;
            const div = '<span class = "dd" style="' + style + '">' + num + '</span>';
            $('body').append(div)
            $('span').animate({
                top: maxH,
                left: bottom
            }, 3000, function () {
                $(this).remove() //这一步很关键,要把落下的去掉,不然会越积越多
            });
        }, 20) //20ms产生一个
    })
</script>
</body>
</html>
举报

相关推荐

0 条评论