簡(jiǎn)單實(shí)現(xiàn)jQuery彈幕效果
在要寫一個(gè)彈幕案例的時(shí)候,首先要清楚每一步要干什么。
首先搭好框架之后在要發(fā)送彈幕時(shí)應(yīng)該準(zhǔn)備進(jìn)行如下步驟:
- 獲取到要發(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);
//為了頁(yè)面友好,清空剛剛輸入的值
$(".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)用的方法)
//頁(yè)面上有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" style="width:100%; height:100%" />
<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>
效果圖如下:

以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
jquery加載頁(yè)面的方法(頁(yè)面加載完成就執(zhí)行)
jquery加載頁(yè)面的方法(頁(yè)面加載完成就執(zhí)行),建議大家看下windows.onload與$(document).ready之間的區(qū)別。2011-06-06
jQuery實(shí)現(xiàn)的Div窗口震動(dòng)效果實(shí)例
這篇文章主要介紹了jQuery實(shí)現(xiàn)的Div窗口震動(dòng)效果,可實(shí)現(xiàn)點(diǎn)擊提交后窗口出現(xiàn)震動(dòng)效果,需要的朋友可以參考下2015-08-08
jquery.serialize() 函數(shù)語(yǔ)法及簡(jiǎn)單實(shí)例
下面小編就為大家?guī)硪黄猨query.serialize() 函數(shù)語(yǔ)法及簡(jiǎn)單實(shí)例。小編覺得挺不錯(cuò)的,現(xiàn)在就分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧2016-07-07
如何用jQuery實(shí)現(xiàn)ASP.NET GridView折疊伸展效果
我們今天就一個(gè)具體的需求進(jìn)行分析,引出如何用jQuery實(shí)現(xiàn)ASP.NET GridView折疊伸展效果,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2015-09-09
jQuery 常見學(xué)習(xí)網(wǎng)站與參考書
打算學(xué)習(xí)jquery的朋友可以參下如下網(wǎng)上,參考書嗎,可以看下 鋒利的jquery腳本之家提供電子版下載。2009-11-11
快速學(xué)習(xí)jQuery插件 Form表單插件使用方法
快速學(xué)習(xí)jQuery插件中的Form表單插件使用方法,可以非常容易地、無侵入地升級(jí)HTML表單以支持Ajax,感興趣的小伙伴們可以參考一下2015-12-12

