簡單實現jQuery彈幕效果,jquery彈幕

來源:互聯網
上載者:User

簡單實現jQuery彈幕效果,jquery彈幕

在要寫一個彈幕案例的時候,首先要清楚每一步要幹什麼。
首先搭好架構之後在要發送彈幕時應該準備進行如下步驟:

  • 擷取到要發送到彈幕上的內容,即擷取到文字框輸入的內容。通過jquery的var str = $(“#文字框的id”).val();
  • 產生一個元素:利用jQuery的 var createSpan =$(““)產生一個span元素,以便發送。
  • 給剛建立的span賦值,即擷取到的文字框中的值 createSpan.text(str );
  • 設定元素的動畫效果,是元素動起來。利用jQuery的animate(css樣式值,時間, 執行完動畫調用的方法)。執行完動畫得手動移除剛剛所添加的元素。

裡面還有許多細節,仔細看就會有收穫!

<!DOCTYPE html><html><head><meta charset = "utf-8" /> <title>彈幕案例</title> <script src = "http://libs.baidu.com/jquery/1.10.2/jquery.min.js"></script> <script> $(function(){  var boxDom = $("#boxDom");  //var domContent = $("#domContent");  var top, right;  var pageWidth = parseInt($(document).width());  var pageHeight =parseInt($(document).height());  //點擊按鈕  $("#btn").bind("click",auto);//按鈕Binder 方法  //按下斷行符號  document.onkeydown = function(){   if(event.keyCode == 13){    auto();   }  }  function auto(){  //1.擷取輸入的字串  var str = $(".text").val();  //2.產生一個元素  var createSpan = $("<span class = 'string' ></span>");  //3.給產生的元素賦值  createSpan.text(str);  //為了頁面友好,清空剛剛輸入的值  $(".text").val(""); //產生元素一個隨機的位置,為了使每條彈幕都出現在螢幕上不同的位置  top = Math.floor(Math.random()*pageHeight); createSpan.css({"top":top, "right": -400, "color": getRandomColor()});  boxDom.append(createSpan);  //4.設定元素的動畫效果,animate(css樣式值,時間, 執行完動畫調用的方法)  //頁面上有N個span,只讓最後一個動起來  var spandom = $("#boxDom>span:last-child");//找到最後一個span  spandom.animate({"right":pageWidth+300},10000,function(){   //移除元素   $(this).remove();  });  }  //定義一個可以產生隨機顏色的方法,可以使每條彈幕的顏色不同  function getRandomColor(){   var colorArr = ['1','2','3','4','5','6','7','8','9','A','B','C','D','E','F'];   var color = "";   for(var i = 0; i < 6; i++){    color += colorArr[Math.floor(Math.random()*16)];    }   return "#"+color;  } }); </script> <style type="text/css"> html,body{  margin: 0px;  padding: 0px;  width: 100%;  height: 100%;  font-family: "微軟雅黑";  background: #ccc; } .boxDom{  width: 100%;  height: 100%;  position: relative;  overflow: hidden; } .idDom{  width: 100%;  height: 60px;  background:#666;  position: fixed;  bottom: 0px; } .contet{  width: 500px;  height: 40px;  position: absolute;  top: 0px;  right: 0px;  bottom: 0px;  left: 0px;  margin: auto; } .title{  display: inline;  font-size: 24px;  vertical-align: bottom;  color: #ffffff;  padding-left: 300px; } .text{  width: 300px;  height: 30px;  border:none;  border-radius:5px;  font-size: 20px;  margin-left:60px; } .btn{  width: 60px;  height: 30px;  color: #ffffff;  background-color: red;  border:none;  font-size:16px;  margin-left:60px;  margin-top: 20px; } .string {   width: 300px;   height: 40px;   margin-top: 20px;   position: absolute;   color: #000;   font-size: 20px;   font-family: "微軟雅黑"; } </style></head><body><div class = "boxDom" id = "boxDom"> <img src="../images/bg_2.jpg" style="width:100%; height:100%" /> <div id = "idDom" class = "idDom">  <div class = "content">   <p class = "title"> 說點什麼:</p>   <input type = "text" class = "text"/>   <button type = "button" class = "btn" id = "btn" >發送</button>  </div> </div></div></body></html>

如下:

以上就是本文的全部內容,希望對大家的學習有所協助,也希望大家多多支援幫客之家。

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.