首頁  >  文章  >  web前端  >  js如何實現彈幕效果

js如何實現彈幕效果

王林
王林轉載
2020-04-13 09:27:533413瀏覽

js如何實現彈幕效果

具體步驟如下:

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程式碼完成後部分截圖如下:

js如何實現彈幕效果

##3、編寫JavaScript程式碼,新增按鈕點擊事件

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

最終效果部分截圖如下:

js如何實現彈幕效果#相關教學推薦:
js教學#

以上是js如何實現彈幕效果的詳細內容。更多資訊請關注PHP中文網其他相關文章!

陳述:
本文轉載於:jb51.net。如有侵權,請聯絡admin@php.cn刪除