使用的vue文件:
<template>
<div>
// 点击使用toast方法
<div class="clickDiv" @click="toToast('请输入正确的电话号码')">click me</div>
// toast对应代码块
<div class="toast" v-if="isShowToast">
{{toastText}}
</div>
</div>
</template>
js代码:
<script>
export default {
name: 'End',
data () {
return {
isShowToast: false,
toastText: ''
}
},
methods: {
toToast(msg){
let that = this
that.toastText = msg
that.isShowToast = true
setTimeout(function(){
that.toastShow = false
}, 2000)
},
}
}
</script>
css文件:
.toast {
position: fixed;
z-index: 999;
left: 50%;
top:50%;
color:rgb(255, 255, 255);
background: rgba(0, 0, 0, 0.7);
text-align: center;
height: 45px;
line-height: 45px;
padding: 0 10px;
-webkit-transform: translateX(-50%) translateY(-50%);
transform: translateX(-50%) translateY(-50%);
}