jQuery写发送弹幕效果

要求:在输入框输入弹幕内容,点击发送,随机字体大小,随机颜色,随机位置,在显示区生成弹幕。

所需技术:jquery

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

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <style>
        .show {
            width: 800px;
            height: 500px;
            margin: 20px auto;
            border: 1px solid black;
            position: relative;
            overflow: hidden;
        }
        
        .send {
            text-align: center;
            width: 800px;
            margin: auto;
        }
        /* .text {
            position: absolute;
        } */
        
        .send input {
            width: 400px;
            height: 40px;
        }
        
        .send button {
            width: 80px;
            height: 40px;
        }
    </style>
</head>

<body>
    <div class="show">

    </div>
    <div class="send">
        <input type="text">
        <button>发送</button>
    </div>
    <script src="js/jquery.min.js"></script>
    <script>
        $(function() {
            // 获取展示的盒子宽高
            var h = parseInt($(".show").height());
            var w = $(".show").width();
            $("button").click(function() {
                //接收返回的对象
                var obj = random();
                var inner = $("input").val();
                // console.log(inner);
                $("<span></span>").appendTo(".show");
                $("span").text(inner).css({
                        'color': obj.bg,
                        'font-size': obj.size,
                        'position': 'absolute',
                        'top': obj.top + "px",
                        'right': -200

                    }).animate({
                        'right': 20000
                    }, 30000, 'linear', function() {
                        $(this).remove();
                    })
                    // 清空input里的值
                $("input").val("");
            })

            //添加一个键盘事件
            // $('[type ="text"]').keyup(function(event) {
            //     if (event.keyCode == 13) {
            //         $('button').click();
            //     }
            // })

            function random() {
                var r = Math.floor(Math.random() * 256);
                var g = Math.floor(Math.random() * 256);
                var b = Math.floor(Math.random() * 256);
                var a = Math.random();
                // 随机颜色
                var bg = `rgb(${r},${g},${b},${a})`;
                //字体最小20  最大39
                var size = Math.floor(Math.random() * 20 + 20);
                // 随机高度
                var top = Math.floor(Math.random() * h);
                var obj = {
                    bg: bg,
                    top: top,
                    size: size
                }
                return obj;
            }
        })
    </script>
</body>
</html>

在这里插入图片描述

  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值