jQuery案例學習【傳送彈幕】
阿新 • • 發佈:2020-12-28
技術標籤:前端學習版塊jqueryhtml5css彈幕案例js
文章目錄
一、效果演示
二、程式碼
1、html程式碼
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<link rel="stylesheet" href="./彈幕.css">
<!--<script src="./jquery-3.5.1.js"></script>-->
<script src="https://apps.bdimg.com/libs/jquery/2.1.4/jquery.min.js"></script >
<title>彈幕案例</title>
</head>
<body>
<div class="boxDom" id="boxDom">
<div class="idDom" id="idDom">
<div class="content">
<p class="title">彈幕:</ p>
<input type="text" class="text" id="text" placeholder="請輸入彈幕"/>
<button type="button" class="btn" id="btn">發射</button>
</div>
</div>
</div>
</body>
</html>
2、css程式碼
html,
body {
margin: 0px;
padding: 0px;
width: 100%;
height: 100%;
font-family: "微軟雅黑";
font-size: 62.5%;
}
.boxDom {
width: 100%;
height: 100%;
position: relative;
overflow: hidden;
}
.idDom {
width: 100%;
height: 50px;
background: #666;
position: fixed;
bottom: 0px;
}
.content {
display: inline-block;
width: 430px;
height: 40px;
position: absolute;
left: 0px;
right: 0px;
top: 0px;
bottom: 0px;
margin: auto;
}
.title {
display: inline;
font-size: 4em;
vertical-align: bottom;
color: #fff;
vertical-align: middle;
}
.text {
border: none;
width: 300px;
height: 40px;
border-radius: 5px;
font-size: 2.4em;
}
.btn {
width: 60px;
height: 40px;
background: #f90000;
border: none;
color: #fff;
font-size: 2.4em;
line-height: 40px;
border-radius: 3px;
}
span {
width: 300px;
height: 40px;
position: absolute;
overflow: hidden;
color: #000;
font-size: 4em;
line-height: 1.5em;
cursor: pointer;
white-space: nowrap;
}
3、jQuery程式碼
<script>
//頁面載入函式
$(function(){
//給回車按鈕新增點選事件
$("#text").keyup(function(e){
if(e.keyCode == 13){
send();
}
});
//給發射按鈕新增點選事件
$("#btn").click(function(){
send();
});
})
//傳送彈幕
function send(){
//獲取當前網頁寬高,確定彈幕初始left值與生成彈幕的高度區間
var page_width = window.document.body.offsetWidth;
var page_height = window.document.body.offsetHeight;
//宣告一個顏色陣列
var colors = ['pink','red','green','orange','black','blue','#660000','#9966CC','#FF9900','#0099CC','#CC3366','#999933'];
//生生一個隨機的顏色下標
var randomColorIndex = parseInt(Math.random() * colors.length);
//生成隨機top值(相當於在距離螢幕頂部距離60%範圍內新增彈幕)
var randomY = parseFloat(Math.random() * page_height * 0.6)
//使用鏈式程式設計生成一個span標籤
console.log(parseInt(Math.random() * colors.length))
console.log(colors[randomColorIndex])
var span = $("<span></span>")
//設定span內容(text和html方法均可)
.text($("#text").val())
//設定span字型顏色
.css('color',colors[randomColorIndex])
//設定span距離頂部的距離
.css('top',randomY)
//設定span最開始距離螢幕左側的距離
.css('left',page_width)
//設定span標籤的自定義動畫效果: 耗時15s時間 距離左側位置變為-100px 勻速運動 回撥函式-運動到-100px時自動移除該元素
.animate({left:'-100px'}
,15000,'linear'
,function(){
$(this).remove()
}
)
//將span標籤新增到到boxDom中展示
$("#boxDom").append(span);
//清空輸入框內容
$("#text").val("")
}
</script>
4、完整程式碼
4.1 彈幕.html檔案
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<link rel="stylesheet" href="./彈幕.css">
<!--<script src="./jquery-3.5.1.js"></script>-->
<script src="https://apps.bdimg.com/libs/jquery/2.1.4/jquery.min.js"></script>
<title>彈幕案例</title>
</head>
<script>
//頁面載入函式
$(function(){
//給回車按鈕新增點選事件
$("#text").keyup(function(e){
if(e.keyCode == 13){
send();
}
});
//給發射按鈕新增點選事件
$("#btn").click(function(){
send();
});
})
//傳送彈幕
function send(){
//獲取當前網頁寬高,確定彈幕初始left值與生成彈幕的高度區間
var page_width = window.document.body.offsetWidth;
var page_height = window.document.body.offsetHeight;
//宣告一個顏色陣列
var colors = ['pink','red','green','orange','black','blue','#660000','#9966CC','#FF9900','#0099CC','#CC3366','#999933'];
//生生一個隨機的顏色下標
var randomColorIndex = parseInt(Math.random() * colors.length);
//生成隨機top值(相當於在距離螢幕頂部距離60%範圍內新增彈幕)
var randomY = parseFloat(Math.random() * page_height * 0.6)
//使用鏈式程式設計生成一個span標籤
console.log(parseInt(Math.random() * colors.length))
console.log(colors[randomColorIndex])
var span = $("<span></span>")
//設定span內容(text和html方法均可)
.text($("#text").val())
//設定span字型顏色
.css('color',colors[randomColorIndex])
//設定span距離頂部的距離
.css('top',randomY)
//設定span最開始距離螢幕左側的距離
.css('left',page_width)
//設定span標籤的自定義動畫效果: 耗時15s時間 距離左側位置變為-100px 勻速運動 回撥函式-運動到-100px時自動移除該元素
.animate({left:'-100px'}
,15000,'linear'
,function(){
$(this).remove()
}
)
//將span標籤新增到到boxDom中展示
$("#boxDom").append(span);
//清空輸入框內容
$("#text").val("")
}
</script>
<body>
<div class="boxDom" id="boxDom">
<div class="idDom" id="idDom">
<div class="content">
<p class="title">彈幕:</p>
<input type="text" class="text" id="text" placeholder="請輸入彈幕"/>
<button type="button" class="btn" id="btn">發射</button>
</div>
</div>
</div>
</body>
</html>
4.2 彈幕.css檔案
html,
body {
margin: 0px;
padding: 0px;
width: 100%;
height: 100%;
font-family: "微軟雅黑";
font-size: 62.5%;
}
.boxDom {
width: 100%;
height: 100%;
position: relative;
overflow: hidden;
}
.idDom {
width: 100%;
height: 50px;
background: #666;
position: fixed;
bottom: 0px;
}
.content {
display: inline-block;
width: 430px;
height: 40px;
position: absolute;
left: 0px;
right: 0px;
top: 0px;
bottom: 0px;
margin: auto;
}
.title {
display: inline;
font-size: 4em;
vertical-align: bottom;
color: #fff;
vertical-align: middle;
}
.text {
border: none;
width: 300px;
height: 40px;
border-radius: 5px;
font-size: 2.4em;
}
.btn {
width: 60px;
height: 40px;
background: #f90000;
border: none;
color: #fff;
font-size: 2.4em;
line-height: 40px;
border-radius: 3px;
}
span {
width: 300px;
height: 40px;
position: absolute;
overflow: hidden;
color: #000;
font-size: 4em;
line-height: 1.5em;
cursor: pointer;
white-space: nowrap;
}
三、注意事項
- 回車鍵 和 傳送按鈕均可以傳送彈幕
- jquery樣式採用網路引入的形式
- 彈幕.html和彈幕.css檔案需要在同級目錄下,否則需要更改html中引入的樣式路徑