JS 发送弹幕

JS实现弹幕的发送

 <div class="box1">
        <div class="box2" style="width: 600px;height:400px">
            
        </div>
        <input placeholder="发送弹幕"/><button>发送</button>
    </div>
<style>
        
        .box1{
            width: 600px;
            text-align: left;
        }
        .box2{
            background-color: black;
            margin-bottom: 20px;
        }
        input{
            width: 300px;
            height: 30px;
            font-size: 21px;
            margin-right: 20px;
            margin-left: 20px;
        }
        button{
            width: 100px;
            height: 35px;
            vertical-align: top;
        }
    </style>
  <script>
        
        let box2 = document.getElementsByClassName("box2")[0];
        let input = document.getElementsByTagName("input")[0];
        let button = document.getElementsByTagName("button")[0];

        let rand = function(start,end){
            let num = Math.floor(Math.random()*(end-start+1)+start);
            return num;
        }
        
        let move = function(){
            let span = document.createElement("span");
            span.innerText = input.value;
            input.value = '';
            let speed = rand(5,10);
            span.style.display = "inline-block";
            span.style.height = 25+"px";
            span.style.position = "absolute";   
            span.style.left = box2.style.width;
            span.style.top = rand(1,400-parseInt(span.style.height))+"px";
            span.style.color = "white";
            box2.appendChild(span);
            let stop = setInterval(function(){
                    span.style.left = parseInt(span.style.left)-speed+"px";
                    if(parseInt(span.style.left)<0){
                        clearInterval(stop);
                        box2.removeChild(span);
                    }
                },50);
            }
            button.onclick = move;
            document.onkeydown = function(e){
                if(e.keyCode==13){
                    move();
                }
            }


    </script>

 实现图:

 

转载于:https://www.cnblogs.com/CccK-html/p/11432692.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值