//toast.js
复制代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26const TOAST_CLASS = 'toast' const TOAST_OUT_CLASS = 'toast out' let innerHtml="" function toast(msg,time=1000) { let body=document.querySelector('#app'); if(body.querySelector('.toast')){ body.removeChild(body.querySelector('.toast')) } let toastElem = document.createElement('div') toastElem.setAttribute('class',TOAST_CLASS) innerHtml = `<sapn>${msg}</sapn>` toastElem.innerHTML = innerHtml; body.appendChild(toastElem); setTimeout(function () { toastElem.setAttribute('class',TOAST_OUT_CLASS) },time) setTimeout(function () { let elm = body.querySelector('.toast'); if(elm){ body.removeChild(elm) } },time+1000) } export default toast
//toast.less
复制代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49@-webkit-keyframes toastIn { 0%{ opacity: 1; } 50%{ opacity: 1; } 100%{ opacity: 1; } } @-webkit-keyframes toastOut { 0%{ opacity:1; } 50%{ opacity:0.7; } 100%{ opacity:0; } } //animation: name duration timing-function delay iteration-count direction; .toast{ position: fixed; z-index:99; background-color: rgba(0,0,0,0.6); color:#fff; padding:15px 25px; border-radius:5px; top: 50%; left:50%; font-size:18px; transform: translate(-50% , -50%); animation-name: toastIn; animation-duration: 1s; animation-iteration-count: 1; animation-delay: 0s; } .toast.out { animation-name: toastOut; animation-duration: 1s; animation-iteration-count: 1; animation-delay: 0s; animation-fill-mode: forwards; }
使用:
全局注入(main.js),this._toast(‘XXXX’)调用
复制代码
1
2
3
4import toast from "./utils/toast"; window._toast = toast
最后
以上就是乐观小鸭子最近收集整理的关于vue自定义toast组件的全部内容,更多相关vue自定义toast组件内容请搜索靠谱客的其他文章。
本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
发表评论 取消回复