jQuery實現(xiàn)的頁面彈幕效果【測試可用】
本文實例講述了jQuery實現(xiàn)的頁面彈幕效果。分享給大家供大家參考,具體如下:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>juqery彈幕</title>
<style>
*{
margin: 0px;
padding: 0px;
}
html,body{
width: 100%;
height: 100%;
font-family: "微軟雅黑";
background-color: #ccc;
margin: 0;
padding: 0;
}
.boxDom{
width: 100%;
height: 800px;
position: relative;
overflow: hidden;
}
.boxDom img{
width: 100%;
height: 100%;
}
.idDom{
width: 100%;
height: 50px;
background-color: #666;
position: fixed;
bottom: 0px;
}
.content{
width: 600px;
height: 50px;
position: absolute;
left: 500px;
top:10px;
}
.title{
font-size: 25px;
display: inline;
vertical-align: bottom;
color: #fff;
}
.text{
width: 300px;
height: 30px;
border:none;
border-radius: 5px;
text-indent: 2em;
margin-left: 60px;
}
.btn{
width: 100px;
height: 30px;
margin-left: 20px;
font-size: 20px;
font-weight: 700;
color: #fff;
background-color: red;
border:none;
border-radius: 5px;
cursor: pointer;
}
.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="7.jpg" alt=""> -->
<div class="idDom">
<div class="content">
<p class="title">發(fā)送彈幕:</p>
<input type="text" class="text">
<button class="btn" id="btn" type="button">發(fā)送</button>
</div>
</div>
</div>
<script src="http://libs.baidu.com/jquery/2.0.0/jquery.min.js"></script>
<script>
$(function () {
var boxDom = $("#boxDom");
var top, right;
var pageWidth = parseInt($(document).width());
var pageHeight = parseInt($(document).height());
$("#btn").bind("click", auto);
//綁定回車鍵按鈕
document.onkeydown = function(event){
if(event.keyCode == 13){
auto();
}
}
function auto() {
//獲取輸入的字符串
var str = $(".text").val();
//生成一個元素
var createSpan = $("<span class ='string'></span>");
//給元素賦值
createSpan.text(str);
//為了頁面友好,清空剛輸入的內(nèi)容
$(".text").val("");
//生成元素一個隨機的位置 為了使每一條彈幕都出現(xiàn)不同的位置
top = Math.floor(Math.random()*pageHeight);
createSpan.css({ "top": top, "right": -400, "color": getRandomColor() });
boxDom.append(createSpan);
//元素在dom運動起來
//首先有一個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;
}
})
$(function () {
var boxDom = $("#boxDom");
var top, right;
var pageWidth = parseInt($(document).width());
var pageHeight = parseInt($(document).height());
$("#btn").bind("click", auto);
//綁定回車鍵按鈕
document.onkeydown = function(event){
if(event.keyCode == 13){
auto();
}
}
function auto() {
//獲取輸入的字符串
var str = $(".text").val();
//生成一個元素
var createSpan = $("<span class ='string'></span>");
//給元素賦值
createSpan.text(str);
//為了頁面友好,清空剛輸入的內(nèi)容
$(".text").val("");
//生成元素一個隨機的位置 為了使每一條彈幕都出現(xiàn)不同的位置
top = Math.floor(Math.random()*pageHeight);
createSpan.css({ "top": top, "right": -400, "color": getRandomColor() });
boxDom.append(createSpan);
//元素在dom運動起來
//首先有一個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>
</body>
</html>
運行效果如下圖所示:

感興趣的朋友還可以使用在線HTML/CSS/JavaScript代碼運行工具:http://tools.jb51.net/code/HtmlJsRun,測試一下運行效果。
更多關(guān)于jQuery相關(guān)內(nèi)容感興趣的讀者可查看本站專題:《jQuery切換特效與技巧總結(jié)》、《jQuery拖拽特效與技巧總結(jié)》、《jQuery擴展技巧總結(jié)》、《jQuery常見經(jīng)典特效匯總》、《jQuery動畫與特效用法總結(jié)》、《jquery選擇器用法總結(jié)》及《jQuery常用插件及用法總結(jié)》
希望本文所述對大家jQuery程序設(shè)計有所幫助。
相關(guān)文章
jQuery實現(xiàn)購物車多物品數(shù)量的加減+總價計算
這篇文章主要介紹了jQuery實現(xiàn)購物車多物品數(shù)量的加減+總價計算,需要的朋友可以參考下2014-06-06
jQuery控制input只能輸入數(shù)字和兩位小數(shù)的方法
這篇文章主要介紹了jQuery控制input只能輸入數(shù)字和兩位小數(shù)的相關(guān)知識,本文給大家介紹的非常詳細,具有一定的參考借鑒價值,需要的朋友可以參考下2019-05-05
jquery使用ul模擬select實現(xiàn)表單美化的方法
這篇文章主要介紹了jquery使用ul模擬select實現(xiàn)表單美化的方法,涉及jquery鼠標事件及頁面元素樣式的相關(guān)操作技巧,具有一定參考借鑒價值,需要的朋友可以參考下2015-08-08
關(guān)于jQuery對象數(shù)據(jù)緩存Cache原理以及jQuery.data詳解
網(wǎng)上有很多教你怎么使用jQuery.data(..)來實現(xiàn)數(shù)據(jù)緩存,但有兩個用戶經(jīng)常使用的data([key],[value])和jQuery.data(element,[key],[value])幾乎沒有什么文章說清楚它們兩的區(qū)別,所以我用到了,研究下分享給大家。2013-04-04
推薦11款jQuery開發(fā)的復(fù)選框和單選框美化插件
web開發(fā)中所有的輸入控件中復(fù)選框和單選框的樣式是最難去設(shè)計的,因為不同的瀏覽器及其操作系統(tǒng)對于樣式的渲染展現(xiàn)是不一樣的。2011-08-08
Jquery圖形報表插件 jqplot簡介及參數(shù)詳解
jqPlot是 一款基于jquery類庫的圖標繪制插件。通過jqPlot可以再網(wǎng)頁中繪制線狀、柱狀、餅狀等多種樣式圖表。而且,jqPlot具有插件可擴展性 (Pluggability),你可以編寫自己的圖表樣式2012-10-10

