console
$(document).ready(function() {
$(".shoot").on("click", startDanmu);
$(".clear").on("click", clearDanmu);
});
function RandomNum(Min, Max) {
var Range = Max - Min;
var Rand = Math.random();
var num = Min + Math.round(Rand * Range);
return num;
}
function plusZero(x) {
if (x < 10) {
x = "0" + x;
} else {
x = x;
}
return x;
}
function startDanmu() {
var message = $("input");
var danmuMessage = "<span class='danmu-message'>" + message.val() + "</span>";
var color = RandomNum(100000, 999999);
var speed = RandomNum(10000, 20000);
var positionY = RandomNum(50, 400);
$(".danmu-box").prepend(danmuMessage);
$(".danmu-message").first().css({
"right": "0",
"top": positionY,
"color": "#" + color
});
$(".danmu-message").first().animate({
left: '0px',
},
speed,
function() {
$(this).fadeOut();
});
var time = new Date();
var month = time.getMonth() + 1;
var day = time.getDay();
var hour = time.getHours();
var minute = time.getMinutes();
var danmuTime = plusZero(month) + "-" + plusZero(day) + " " + plusZero(hour) + ":" + plusZero(minute);
var messageToTable = "<tr><td>" + message.val() + "</td><td>" + danmuTime + "</td></tr>";
$(".danmu-table > tbody").prepend(messageToTable);
message.val("");
}
function clearDanmu() {
$(".danmu-box").html("");
}
<div class="frame">
<div class="row">
<div class="col-xs-8 col-sm-8 col-md-8 col-lg-8 danmu-box-frame">
<div class="danmu-box">
</div>
</div>
<div class="col-xs-4 col-sm-4 col-md-4 col-lg-4 danmu-table-frame">
<table class="table .table-condensed danmu-table">
<thead>
<tr>
<th>
弹幕内容
</th>
<th>
弹幕时间
</th>
</tr>
</thead>
<tbody>
</tbody>
</table>
</div>
</div>
<div class="danmu-form">
<form class="form-inline">
<input type="text" class="form-control" placeholder="开始吐槽!">
<button type="button" class="btn btn-primary shoot">
发射弹幕!
</button>
<button type="button" class="btn btn-danger clear">
清空弹幕
</button>
</form>
</div>
</div>
<hr>
<footer>
Designed By
<a href="http://blog.csdn.net/alenhhy" target="_blank">
Alen Hu
</a>
</footer>
* {
font-family: '微软雅黑', sans-serif;
}
body {
background: #f5f5f5;
}
.frame {
width: 900px;
margin: 0 auto;
}
.danmu-box {
position: relative;
overflow: hidden;
width: 100%;
height: 450px;
margin: 30px 0 20px;
background: white;
}
.danmu-message {
position: absolute;
display: inline-block;
}
.danmu-table-frame {
overflow: auto;
height: 450px;
margin: 30px 0 20px;
background: white;
}
.danmu-table {
color: #aaa;
}
.danmu-table > thead > tr > th {
font-weight: normal !important;
}
.danmu-form form {
width: 60%;
margin: 0 auto;
}
input {
width: 60% !important;
}
button {
width: 19%;
background: #00a1d6;
}
hr {
width: 60%;
margin: 20px auto;
}
footer {
font-size: 14px;
text-align: center;
}
footer > a {
text-decoration: none;
}
footer > a:hover,
footer > a:active,
footer > a:focus {
text-decoration: underline;
}