百分百源码网-让建站变得如此简单! 登录 注册 签到领金币!

主页 | 如何升级VIP | TAG标签

当前位置: 主页>网站教程>JS教程> js怎样实现弹幕结果
分享文章到:

js怎样实现弹幕结果

发布时间:09/01 来源:未知 浏览: 关键词:

详细步骤如下:

1、编写HTML代码:

创立一个弹幕显示墙,乃至两个按钮,离别为“发送”和“清屏”,并在文本框中设定placeholder为“说点什么吧?”以提醒会员在此输入弹幕信息。

<body>
    <div class="con">      
      <div id="screen">
        <div class="dm_show">
          <!-- <div>text message</div> -->
        </div>
      </div>
      <div class="edit">
        <p>
          <input placeholder="说点什么吧?" class="content" type="text" />
        </p>
        <p>
          <input type="button" class="send" value="发送" />
          <input type="button" class="clear" value="清屏" />
        </p>
      </div>   
    </div>
  </body>

2、设定各标签的CSS样式:

<style>
      .con {
        background-color: floralwhite;
        padding: 40px 80px 80px;
      }
      #screen {
        background-color: #fff;
        width: 100%;
        height: 380px;
        border: 1px solid rgb(229, 229, 229);
        font-size: 14px;
      }

      .content {
        border: 1px solid rgb(204, 204, 204);
        border-radius: 3px;
        width: 320px;
        height: 35px;
        font-size: 14px;
        margin-top: 30px;

      }

      .send {
        border: 1px solid rgb(230, 80, 30);
        color: rgb(230, 80, 0);
        border-radius: 3px;
        text-align: center;
        padding: 0;
        height: 35px;
        line-height: 35px;
        font-size: 14px;
        width: 159px;
        background-color: white;
      }

      .clear {
        border: 1px solid;
        color: darkgray;
        border-radius: 3px;
        text-align: center;
        padding: 0;
        height: 35px;
        line-height: 35px;
        font-size: 14px;
        width: 159px;
        background-color: white;
      }

      .edit {
        margin: 20px;
        text-align: center;
      }

      .text {
        position: absolute;
      }
      *{
        margin: 0;
        padding: 0;
      }

      .dm_show{
        margin: 30px;
      }
</style>

CSS代码完成后部分截图如下:

499aafd194084861a5b2833068c6baf.png

3、编写JavaScript代码,增加按钮点击事件

<script type="text/javascript" src="../jquery-easyui-1.3.5/jquery.min.js"></script>
<script>
      $(function() {
        //设定“发送”按钮点击事件,将弹幕体显示在弹幕墙上
        $('.send').click(function() {
          //猎取文本输入框的内容
          var val = $('.content').val();
          //将文本框的内容赋值给val后,将文本框的内容清除,以便会员下一次输入
          $('.content').val('');
          //将文本框内容用div包裹起来,便于后续处置
          var $content = $('<div class="text">' + val + '</div>');
          //猎取弹幕墙对象
          $screen = $(document.getElementById("screen"));
          //设定弹幕体显现时的上边距,为任意值
          var top = Math.random() * $screen.height()+40;
          //设定弹幕体的上边距和左边距
          $content.css({
            top: top + "px",
            left: 80
          });
          //将弹幕体增加到弹幕墙中
          $('.dm_show').append($content);
          //弹幕体从左端移动到最右侧,时间为8秒,然后直接删除该元素
          $content.animate({
            left: $screen.width()+80-$content.width()
          }, 8000, function() {
            $(this).remove();
          });
        });
        //设定“清屏”点击事件,清除弹幕墙中的所有内容
        $('.clear').click(function() {
          $('.dm_show').empty();
        });
      });
</script>

终究结果部分截图如下:

1da0d2305f51a7aa1b6a4826c5a49ef.png
相关教程引荐:js教程

以上就是js怎样实现弹幕结果的具体内容,更多请关注百分百源码网其它相关文章!

打赏

打赏

取消

感谢您的支持,我会继续努力的!

扫码支持
扫码打赏,你说多少就多少

打开支付宝扫一扫,即可进行扫码打赏哦

百分百源码网 建议打赏1~10元,土豪随意,感谢您的阅读!

共有150人阅读,期待你的评论!发表评论
昵称: 网址: 验证码: 点击我更换图片
最新评论

本文标签

广告赞助

能出一分力是一分吧!

订阅获得更多模板

本文标签

广告赞助

订阅获得更多模板