<!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>弹幕飘屏</title>
<style>
.aa {
width: 600px;
margin: 0 auto;
}
#pm {
width: 600px;
height: 400px;
border: 1px solid #5d0b0b;
position: relative;
overflow: hidden;
}
p {
position: absolute;
top: 40px;
animation: mymate 5s linear 300ms infinite;
}
input {
margin-top: 10px;
outline: none;
}
button {
margin: 10px;
}
</style>
</head>
<body>
<div class="aa">
<div id="pm">
</div>
<input id="text" type="text" placeholder="输入弹幕">
<button οnclick="enter()">发送</button>
</div>
<script>
function enter() {
let text = document.getElementById("text").value;
let p = document.createElement("p");
p.innerHTML = text;
pm.appendChild(p);
let right =-20;
let func = function(){
right++;
p.style.right = right + 'px'
if( right > 600){
pm.removeChild(p);
clearInterval(tiem);
}
}
let tiem = setInterval(func,1)
}
</script>
</body>
</html>