话不多说直接上代码:
组件代码:
//window信息弹层组件
Vue.component("pop-up",{
template:'<div class="prompt" v-show="message == \'\' ? false : true">{{message}}</div>',
props:["message"],
updated(){
var _that = this;
setTimeout(function(){
_that.$emit("fadeout");
},2500)
}
})
组件css:
/*信息提示层*/
.prompt{
z-index: 9999;
position: fixed;
padding: .45em .25rem;
background-color: rgb(0 0 0 / 55%);
color: #fff;
font-size: .38rem;
border-radius: .2rem;
top: 5rem;
left: 50%;
-webkit-transform: translate(-50%,0);
transform: translate(-50%,0);
animation-duration: .4s;
animation-fill-mode: forwards;
animation-name: fadeIn;
}
使用:
<!--信息提示层-->
<pop-up :message="errorMsg" @fadeout="fadeOutPop"></pop-up>
页面vue实例data中需要定义“errorMsg”变量,methods中定义fadeOutPop回调方法:
<script>
var vu = new Vue({
el: "#app",
data: {
errorMsg:"",
},
methods: {
//提示信息弹层关闭
fadeOutPop(){
this.errorMsg = "";
}
}
})
</script>
看效果:
![](https://img.laitimes.com/img/9ZDMuAjOiMmIsIjOiQnIsICM38FdsYkRGZkRG9lcvx2bjxiNx8VZ6l2cs0TPn10MNR1TwMGRNBDOsJGcohVYsR2MMBjVtJWd0ckW65UbM5WOHJWa5kHT20ESjBjUIF2X0hXZ0xCMx81dvRWYoNHLrdEZwZ1Rh5WNXp1bwNjW1ZUba9VZwlHdssmch1mclRXY39CXldWYtlWPzNXZj9mcw1ycz9WL49zZuBnLwAzN5QDMzcTM3EDOwEjMwIzLc52YucWbp5GZzNmLn9Gbi1yZtl2Lc9CX6MHc0RHaiojIsJye.png)