需求
html代码
<div id="bigbox">
<input type="text" id="Text">
<input type="submit" value="发送" id="btn" onclick="change()">
<input type="button" value="弹出" id="btn1">
<input type="button" value="收入" id="btn2">
</div>
css代码
*{
margin: 0;
padding: 0;
}
#bigbox{
width: 100%;
height: 100%;
position: relative;
}
#Text{
position: absolute;
top: 800px;
left: 46%;
display: none;
}
#btn{
position: absolute;
top: 800px;
left: 58%;
display: none;
}
.newDiv{
position: absolute;
left: 0;
}
#btn1{
position: absolute;
top: 800px;
}
#btn2{
position: absolute;
top: 800px;
left: 40px;
}
JavaScript代码
var Text = document.getElementById('Text')
var btn = document.getElementById('btn')
function change(){
var div = document.createElement("div")
div.innerHTML = Text.value
div.className= ("newDiv")
document.body.append(div)
function randomRGB(){
var r = Math.floor(Math.random()*256);
var g = Math.floor(Math.random()*256);
var b = Math.floor(Math.random()*256);
return `color:rgb(${r},${g},${b});`
}
var newHei = Math.floor(Math.random()*400)
var fontsize = Math.floor(Math.random()*50)
div.style= randomRGB()
div.style.top = newHei + "px"
div.style.fontSize = fontsize +"px"
setInterval(()=>{
var lefts = parseInt (getComputedStyle(div).left)
div.style.left = lefts +10+"px"
if(lefts==1660){
div.style.left=0
}
},50)
}
var btn1 = document.getElementById('btn1')
var btn2 = document.getElementById('btn2')
btn1.onclick = function(){
btn.style.display = "block"
Text.style.display = "block"
}
btn2.onclick = function(){
btn.style.display = "none"
Text.style.display = "none"
}
样式截图
实现原理
- 先设置三个按钮和一个输入框,定位好他们的位置
- 设置点击事件,弹出,收入控制输入框和发送框的隐藏与显示
- 用随机函数定义输入内容的颜色,大小,出现的范围
- 用js在body中加入一个新的div,并且将输入的内容赋值进去