簡單實(shí)現(xiàn)jQuery彈幕效果

在要寫一個(gè)彈幕案例的時(shí)候,首先要清楚每一步要干什么。
首先搭好框架之后在要發(fā)送彈幕時(shí)應(yīng)該準(zhǔn)備進(jìn)行如下步驟:

目前創(chuàng)新互聯(lián)已為成百上千的企業(yè)提供了網(wǎng)站建設(shè)、域名、網(wǎng)站空間、綿陽服務(wù)器托管、企業(yè)網(wǎng)站設(shè)計(jì)、武穴網(wǎng)站維護(hù)等服務(wù),公司將堅(jiān)持客戶導(dǎo)向、應(yīng)用為本的策略,正道將秉承"和諧、參與、激情"的文化,與客戶和合作伙伴齊心協(xié)力一起成長,共同發(fā)展。

  • 獲取到要發(fā)送到彈幕上的內(nèi)容,即獲取到文本框輸入的內(nèi)容。通過jquery的var str = $(“#文本框的id”).val();
  • 生成一個(gè)元素:利用jQuery的 var createSpan =$(““)生成一個(gè)span元素,以便發(fā)送。
  • 給剛創(chuàng)建的span賦值,即獲取到的文本框中的值 createSpan.text(str );
  • 設(shè)置元素的動(dòng)畫效果,是元素動(dòng)起來。利用jQuery的animate(css樣式值,時(shí)間, 執(zhí)行完動(dòng)畫調(diào)用的方法)。執(zhí)行完動(dòng)畫得手動(dòng)移除剛剛所添加的元素。

里面還有許多細(xì)節(jié),仔細(xì)看就會(huì)有收獲!

<!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());

 //點(diǎn)擊按鈕
 $("#btn").bind("click",auto);//按鈕綁定方法
 //按下回車
 document.onkeydown = function(){
  if(event.keyCode == 13){
  auto();
  }
 }
 function auto(){
 //1.獲取輸入的字符串
 var str = $(".text").val();
 //2.生成一個(gè)元素
 var createSpan = $("<span class = 'string' ></span>");

 //3.給生成的元素賦值

 createSpan.text(str);

 //為了頁面友好,清空剛剛輸入的值
 $(".text").val("");

 //生成元素一個(gè)隨機(jī)的位置,為了使每條彈幕都出現(xiàn)在屏幕上不同的位置
 top = Math.floor(Math.random()*pageHeight);


 createSpan.css({"top":top, "right": -400, "color": getRandomColor()});
 boxDom.append(createSpan);

 //4.設(shè)置元素的動(dòng)畫效果,animate(css樣式值,時(shí)間, 執(zhí)行完動(dòng)畫調(diào)用的方法)

 //頁面上有N個(gè)span,只讓最后一個(gè)動(dòng)起來
 var spandom = $("#boxDom>span:last-child");//找到最后一個(gè)span
 spandom.animate({"right":pageWidth+300},10000,function(){
  //移除元素
  $(this).remove();

 });
 }
 //定義一個(gè)可以生成隨機(jī)顏色的方法,可以使每條彈幕的顏色不同
 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"  />
 <div id = "idDom" class = "idDom">
 <div class = "content">
  <p class = "title"> 說點(diǎn)什么:</p>
  <input type = "text" class = "text"/>
  <button type = "button" class = "btn" id = "btn" >發(fā)送</button>
 </div>
 </div>
</div>
</body>
</html>

效果圖如下:

簡單實(shí)現(xiàn)jQuery彈幕效果

以上就是本文的全部內(nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持創(chuàng)新互聯(lián)。

網(wǎng)站題目:簡單實(shí)現(xiàn)jQuery彈幕效果
標(biāo)題路徑:http://muchs.cn/article20/ihsjco.html

成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供軟件開發(fā)響應(yīng)式網(wǎng)站、移動(dòng)網(wǎng)站建設(shè)、網(wǎng)站維護(hù)網(wǎng)站建設(shè)、電子商務(wù)

廣告

聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請(qǐng)盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如需處理請(qǐng)聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來源: 創(chuàng)新互聯(lián)

網(wǎng)站托管運(yùn)營