VUE 自定义插件Toast
程序员文章站
2024-03-04 16:54:41
...
toast.js
import ToastTemplate from './ToastTemplate'
let Toast = {}
Toast.install = function(Vue,params={}){
const VueToast = Vue.extend(ToastTemplate) //创建模板
let toast = null
Vue.prototype.$toast = (params={}) =>{
if(!toast){
toast = new VueToast().$mount() //创建实例
document.body.appendChild(toast.$el) //挂载实例
}
toast.show(params)
}
}
export default Toast
ToastTemplate.vue
<template>
<div class="msg" ref="msg" :style="{backgroundColor: backgroundColor,color: color}" v-show="isShow" v-html="message"></div>
</template>
<script>
export default {
name: 'ToastTemplate',
data(){
return{
isShow: false,
color: '#FFFFFF',
backgroundColor: '#29B0F0',
message: '333',
time:800,
timer:null,
animate:null,
}
},
updated(){
let left = this.$refs.msg.clientWidth/2
this.$refs.msg.style.left = 'calc(50% - '+ left +'px)'
},
methods:{
show(params) {
let _this = this
if(this.animate){
this.animate.cancel()
}
if(this.timer){
clearTimeout(this.timer);
}
//初始化参数
let { color,backgroundColor,message } = params
this.color = color?color:'#FFFFFF'
this.backgroundColor = backgroundColor?backgroundColor:'#29B0F0'
this.message = message
this.isShow = true
this.timer = setTimeout(function () {
_this.animate = _this.$refs.msg.animate([
{opacity:1},
{opacity:0}
],{duration:500})
_this.animate.onfinish = function(){
_this.isShow = false
clearTimeout(_this.timer);
}
},_this.time)
}
}
}
</script>
<style scoped>
.msg{
position: fixed;
top: 38%;
border-radius: 2px;
padding: 5px 10px;
}
</style>
main.js
import Toast from './Toast'
Vue.use(Toast)
推荐阅读
-
VUE 自定义插件Toast
-
Vue利用Vue.extend()实现自定义弹出框
-
Django自定义插件实现网站登录验证码功能
-
Django自定义插件实现网站登录验证码功能
-
为Android Studio编写自定义Gradle插件的教程
-
为Android Studio编写自定义Gradle插件的教程
-
elasticsearch中文分词器插件elasticsearch-analysis-ik远程自定义词典热更新
-
PHP框架Laravel插件Pagination实现自定义分页,laravelpagination
-
vue本地存储、获取自定义data-id、获取链接url参数、页面跳转返回、修改页面title
-
Android编程实现自定义toast示例