@keyframesshow-toast{0%{opacity:0;}25%{opacity:1;z-index:9999}50%{不透明度:1;z-index:9999}75%{不透明度:1;z-index:9999}100%{不透明度:0;z-index:0}}.toast{位置:固定;顶部:50%;左:50%;转换:翻译(-50%,-50%);z-指数:999;背景色:#000;不透明度:.7;颜色:#fff;框大小:边框框;最小高度:80px;填充:20px30px;行高:50px;最小宽度:364px;最大宽度:80%;边界半径:15px;字体大小:28px;文本对齐:居中;word-wrap:break-word;问题发现最近打算给团队里的公共插件做一些小动画效果,优化用户体验。这次先从最简单的toast插件说起。主要的文件有如下两个:index.jsimportToastfrom'./Toast.vue';const_TOAST={show:false,component:null};exportdefault{install(Vue){//添加实例方法Vue.prototype.$toast=(text,options={duration:2000})=>{if(_TOAST.show){return;}if(!_TOAST.component){让ToastComponent=Vue.extend(Toast);_TOAST.component=newToastComponent();让元素=_TOAST.component.$mount().$el;document.body.appendChild(元素);}_TOAST.component.duration=options.duration||2000;_TOAST.component.whiteSpace=options.whiteSpace||'继承';_TOAST.component.position=options.position||'中心';_TOAST.component.text=文本;_TOAST.component.show=_TOAST.show=true;setTimeout(()=>{_TOAST.component.show=_TOAST.show=假e;},options.duration);};Vue.prototype.$killToast=()=>{if(_TOAST.component){_TOAST.component.show=_TOAST.show=false;}};}};Toast.vue{{text}}