需求
- 页面上漂浮字体大小不一、颜色不—,从左向右滚动的弹幕;
- 底部中间有一个发送功能,可以发送新的弹幕;
- 底部的发送部分可以向下收起和弹出。
实现原理
需求 | 实现原理 |
---|
页面上漂浮字体大小不一、颜色不—,从左向右滚动的弹幕;
|
导入初始滚动弹幕内容,给每条弹幕设置初始轨道随机飘入屏幕中,调用随机函数设置字体的大小和颜色
|
给滚动弹幕内容飘出设置定时器,获取盒子的左偏移量判断弹幕消失后进行删除 |
底部中间有一个发送功能,可以发送新的弹幕
| 设置input输入框,先取出输入框内容,添加到新创建的弹幕盒子,并给盒子内容设置样式 |
|
底部的发送部分可以向下收起和弹出
|
调用toggle()方法实现发送弹幕的显示与隐藏
|
代码
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>sendConScreen</title>
</head>
<style>
* {
padding: 0;
margin: 0;
}
.send {
width: 600px;
height: 60px;
margin: 0 auto;
}
.send input {
width: 300px;
height: 40px;
border: 1px solid #b0b0b0;
border-radius: 7px;
padding-left: 15px;
margin-top: 10px;
outline: none;
}
.send button {
width: 80px;
height: 40px;
border: none;
margin-left: 10px;
background-color: #ff6699;
border-radius: 8px;
color: white;
cursor: pointer;
}
.content {
margin: 0 auto;
width: 1000px;
height: 600px;
background: url(./barrage.gif);
background-size: cover;
position: relative;
overflow: hidden;
}
.sendCon {
position: absolute;
}
#initial {
position: relative;
height: 100%;
width: 100%;
overflow: hidden;
font-size: 50px;
}
#initial .initialon {
height: 50px;
line-height: 50px;
position: absolute;
overflow: hidden;
}
.left{
position: absolute;
top: 625px;
left: 500px;
width: 10px;
height: 10px;
background-color: transparent;
transform: rotate(45deg);
border-left: 5px solid #b0b0b0;
border-bottom: 5px solid#b0b0b0;
border-top: transparent;
border-right: transparent;
cursor: pointer;
}
</style>
<body>
<div class="container">
<div class="content">
<div id="initial"></div>
</div>
<div class="left"></div>
<div class="send">
<input type="text" />
<button>发送</button>
</div>
</div>
<script type="text/javascript" src="jquery-3.6.3.js"></script>
<script type="text/javascript">
const data = ['阴羊怪气', '呆jio不', '阳勿运动', '中日结合', '阿里嘎多美羊羊桑', '拱火の好兄弟', '跨服聊天', '我咋地她了']
const initial = document.getElementById('initial')
const iniHeight = initial.clientHeight
function insert() {
const initialon = document.createElement('div')
const randomIni = Math.floor(Math.random() * 8)
initialon.innerHTML = data[randomIni]
initialon.className = 'initialon'
const top = iniHeight - 50
const initialonTop = Math.floor(Math.random() * (top - 1))
const initialonLeft = initial.clientWidth
initialon.style.left = initialonLeft + 'px'
initialon.style.top = initialonTop + 'px'
initial.appendChild(initialon)
$('.initialon').css("font-size", Math.round(Math.random() * 60) + 12 + "px")
$('.initialon').css("color", "rgb(" + Math.round(Math.random() * 255) + "," + Math.round(Math.random() * 255) + "," + Math.round(Math.random() * 255) + ")")
move(initialon)
}
function move(obj) {
const timer1 = setInterval(() => {
const initialonWidth = obj.clientWidth
let run = obj.offsetLeft
run--
if (run <= -initialonWidth) {
initial.removeChild(obj)
clearInterval(timer1)
}
obj.style.left = run + 'px'
}, 1)
}
const timer2 = setInterval(() => {
insert()
}, 1000)
window.onblur = function () {
clearInterval(timer2)
}
$('.left').click(function(){
$('.send').toggle()
})
$("button").click(function () {
const con = $("input").val()
if (con.length > 20) {
alert("字数不得超过20个!")
return
}
const sendCon = $("<div>")
sendCon.text(con)
sendCon.addClass("sendCon")
sendCon.css("top", Math.round(Math.random() * 500))
sendCon.css("left", "1600px")
sendCon.css("font-size", Math.round(Math.random() * 60) + 12 + "px")
sendCon.css("color", "rgb(" + Math.round(Math.random() * 255) + "," + Math.round(Math.random() * 255) + "," + Math.round(Math.random() * 255) + ")")
sendCon.animate({
left: -1000
}, Math.round(Math.random() * 9000) + 1000, "linear", function () {
sendCon.remove()
})
$(".content").append(sendCon)
})
</script>
</body>
</html>
效果展示
