JS题目8

题八:滚动弹幕

要求:

1.页面上漂浮字体大小不一、颜色不一,从左向右滚动的弹幕;

2.底部中间有一个发送功能,可以发送新的弹幕;

3.底部的发送部分可以向下收起和弹出。

<!DOCTYPE html>

<html lang="en">

<head>

  <meta charset="UTF-8">

  <meta name="viewport" content="width=device-width, initial-scale=1.0">

  <title>Document</title>

</head>

<style>

  *{

    margin: 0;

    padding: 0;

  }

  .n1{

    width: 70%;

    background-color: black;

    height: 500px;

  }

  .n2{

    width: 100%;

    height: 100px;

  }

  .content{

    margin-left: 300px;

  }

  input{

    width: 50%;

    height: 30px;

    background-color: rgb(29, 228, 66);

  }

  .btn{

    height: 34px;

    width: 100px;

    margin-top: 3px;

    margin-left: -5px;

    background-color: rgb(5, 228, 91);

  }

</style>

<body>

  <div class="n1" id="n1">

    <div class="n2" id="n2">

      <div class="content">

        <input type="text" class="text" id="text" />

        <button type="button" class="btn" id="btn">发送</button>

      </div>

    </div>

  </div>

  <script>

  const body = document.querySelector('.n1');

  const btnObj = document.querySelector('#btn');

  const textObj = document.querySelector('#text');

  btnObj.onclick = function () {

    const ranColor = '#' + Math.random().toString(16).slice(-6);

    const ranTop = parseInt(Math.random() * 400);

    const spanObj = document.createElement('span');

    spanObj.style.color = ranColor;

    spanObj.style.top = ranTop + 'px';

    spanObj.style.left = 1500 + 'px';

    const textObjval = textObj.value;

    spanObj.innerHTML = textObjval;

    body.appendChild(spanObj);

    const timer = setInterval(() => {

      spanObj.style.left = spanObj.offsetLeft - 10 + 'px';

      if (spanObj.offsetLeft <= -10) {

        clearInterval(timer);

        spanObj.remove();

      }

    }, 30);

  }

  textObj.addEventListener('keyup', function (event) {

    let e = event || window.event;

    if (e.keyCode == 13) {

      btnObj.onclick();

    }

  });

  </script>

</body>

</html>

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值