极简无插件前端弹框提示代码,码好
#不想引各种乱七八糟的插件,就想要个弹框。这份代码是你最好的选择!!<!DOCTYPE html>
<html>
<head>
<title>测试自定义弹框</title>
</head>
<style type="text/css">
.my-toast{
background-color: rgba(0,0,0,0.5);
min-width: 3rem;
min-height: 1rem;
text-align: center;
font-size: 12px;
position: fixed;
top: 50%;
left: 50%;
white-space: nowrap;
transform: translate(-50%,-50%);
color: #EEEEEE;
padding: .3rem .4rem;
z-index: 999;
border-radius: 5px;
}
</style>
<body>
<button type="button" onclick="myToast('你点击我了~')" style="width: 100px;height: 50px;background-color: gray;cursor: pointer;">点我</button>
</body>
<script type="text/javascript">
function myToast(str,timer=1500){
var toast=document.createElement('div')
toast.className='my-toast'
toast.innerHTML=str||'成功';
document.getElementsByTagName('body')[0].appendChild(toast)
setTimeout(function(){
toast.className+=' fadeOut'
toast.remove()
},timer)
}
</script>
</html>
滴水成冰,世间不存在毫无意义的付出,时间终会给你答案。