提示卡片
html部分
<div class="msg-list">
</div>
<button class="btn">点我看提示</button>
css部分
*{
margin: 0;
padding: 0;
}
body{
background-color: rebeccapurple;
height: 100vh;
overflow: hidden;
display: flex;
justify-content: center;
align-items: center;
}
.btn{
background-color: #fff;
color: rebeccapurple;
font-weight: bold;
padding: 1rem;
border-radius: 5px;
border: none;
cursor: pointer;
}
.btn:focus{
outline: none;
}
.btn:active{
transform: scale(0.98);
}
.msg{
padding: 10px 30px;
background-color: #fff;
border-radius: 20px;
margin: 10px 0;
color: #fff;
}
.info{
background-color: #ccc;
}
.success{
background-color: green;
}
.error{
background-color: red;
}
.msg-list{
position: fixed;
top: 10px;
right: 10px;
}
js部分
// 获取对象
const msgs=document.querySelector(".msg-list")
const btn=document.querySelector(".btn")
const msg=[
"消息1",
"消息2",
"消息3",
]
const mgs_status=["info","success","error"]
// 绑定点击事件
btn.addEventListener("click",function(){
// 创建元素以及添加样式
const div=document.createElement("div");
const index=random()
div.className="msg";
div.innerHTML=msg[index]
div.classList.add(mgs_status[index])
console.log(div);
msgs.appendChild(div);
setTimeout(()=>{
div.remove()
},1000)
})
function random(){
const index=Math.floor(Math.random()*3);
return index
}
效果