国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 編程 > JavaScript > 正文

簡單實現jQuery彈幕效果

2019-11-19 16:39:21
字體:
來源:轉載
供稿:網友

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

  • 獲取到要發送到彈幕上的內容,即獲取到文本框輸入的內容。通過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);//按鈕綁定方法  //按下回車  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>

效果圖如下:

以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持武林網。

發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 郧西县| 汉源县| 蒙阴县| 吉林省| 永和县| 白沙| 苍梧县| 凤山市| 建宁县| 广元市| 阿鲁科尔沁旗| 页游| 康平县| 丰城市| 青海省| 玉屏| 筠连县| 九龙城区| 皋兰县| 温泉县| 泗水县| 山阳县| 麻江县| 会泽县| 临城县| 曲麻莱县| 德州市| 临桂县| 永川市| 台北市| 阿鲁科尔沁旗| 怀宁县| 盘锦市| 清涧县| 通城县| 林周县| 松原市| 牡丹江市| 如皋市| 凌云县| 靖远县|