我的第一個小特效哦
寫每一個效果前都得先構思,先有個大概布局,再開始埋頭苦敲,這樣才能快速解決問題。。。
1、寫彈幕時最好把所有的彈幕都放在“幕布”上,而不是直接放在內容上;
2、獲取隨機字體顏色、隨機大小
//獲取隨機RGB值
function randomColor(){
return "rgba("+ Math.round(Math.random() * 255) + "," + Math.round(Math.random() * 255) + "," + Math.round(Math.random() * 255) + ",1)";
}
//獲取14-30隨機數
function runRandom(){
return Math.floor( Math.random()*17 ) + 14;
}
3、布局
<div id="box">
<img src="img/op.jpg"/>
<div id="list">
<p id="p1"></p>
</div>
<div id="bottom">
<input type="text" id="text" placeholder="請輸入彈幕" />
<span id="btn">提交</span>
<a id="close" href="javascript:;">隱藏彈幕</a>
</div>
</div>
<style type="text/css">
*{
margin:0;
padding:0;
}
#box{
width: 900px;
height: 700px;
border: 2px solid yellowgreen;
position: relative;
margin:100px auto;
}
#box img{
width: 900px;
height: 700px;
z-index: -1;
}
#bottom{
width: 440px;
position: absolute;
bottom: 10px;
left: 50%;
margin-left:-220px;
font-family: "微軟雅黑";
font-size: 14px;
display: none;
}
#text{
width: 300px;
height: 30px;
border:2px solid #FF0000;
float: left;
}
#btn{
display: block;
float: left;
width: 50px;
height: 32px;
color:#fff;
cursor: pointer;
text-align: center;
line-height: 32px;
background: #FF0000;
border:1px solid #FF0000;
}
a{
text-decoration: none;
text-align: center;
display: block;
float: right;
background: #FF0000;
line-height: 32px;
width: 80px;
height: 32px;
color:#fff;
border:1px solid #FF0000;
}
#list{
width: 100%;
height: 300px;
position: absolute;
top:0;
left: 0;
overflow: hidden;
}
p{
color:#FF0000;
position: absolute;
top: 0;
right:-304px;
}
</style>
4、js代碼段
var box = document.getElementById("box");
var list = document.getElementById("list");
var p1 = document.getElementById("p1");
var Text = document.getElementById("text");
var btn = document.getElementById("btn");
var close = document.getElementById("close");
var bottom = document.getElementById("bottom");
box.onmouseover = function(){
bottom.style.display = 'block';
};
box.onmouseout = function(){
bottom.style.display = 'none';
};
//點擊回車發(fā)送彈幕
Text.onfocus = function(){
document.onkeydown = function(ev){
ev = ev || event;
if( ev.keyCode == 13 ){
fn();
}
}
}
//點擊按鈕發(fā)送彈幕
btn.onclick = fn;
//彈幕移動
function fn(){
var val = Text.value;
var op = document.createElement('p');
op.innerHTML = val ;
list.appendChild( op );
op.style.marginTop = Math.floor( Math.random()*200 ) + 'px';
op.style.color = randomColor();
op.style.fontSize = ( Math.floor( Math.random()*20) +10 ) + 'px' ;
op.style.whiteSpace = 'nowrap';
op.num = 880;
var left = op.offsetLeft;
var timer = setInterval(function(){
op.num --;
if( op.offsetLeft <= -op.offsetWidth ){
clearInterval( timer );
list.removeChild( op );
}
op.style.left = op.num + 'px';
},runRandom());
Text.value = '';
}
//獲取隨機RGB值
function randomColor(){
return "rgba("+ Math.round(Math.random() * 255) + "," + Math.round(Math.random() * 255) + "," + Math.round(Math.random() * 255) + ",1)";
}
//獲取14-30隨機數
function runRandom(){
return Math.floor( Math.random()*17 ) + 14;
}
//隱藏彈幕 顯示彈幕
close.onclick = function(){
if(close.onOff){
close.innerHTML = '顯示彈幕';
list.style.display = 'none';
}else{
close.innerHTML = '隱藏彈幕';
list.style.display = 'block';
}
close.onOff = !close.onOff;
}
5、效果圖;

Paste_Image.png