1、新建一个文件,放弹窗函数
modal.js
/**
* 提示弹窗
*/
export default {
// 提示弹窗,无按钮,延迟关闭
toast: function (msg, duration) {
duration = isNaN(duration) ? 2000 : duration;
var m = document.createElement('div');
m.innerHTML = msg
m.style.cssText = "min-width: 150px; padding: 10px;opacity: 0.7;color: rgb(255, 255, 255);text-align: center;border-radius: 5px;position: fixed;top: 50%;left: 50%;z-index: 999999;background: rgb(42, 168, 55);font-size: 12px;";
document.body.appendChild(m);
if(timer) {
clearTimeout(timer)
}
var timer = setTimeout(function () {
var d = 0.5;
m.style.webkitTransition = '-webkit-transform ' + d + 's ease-in, opacity ' + d + 's ease-in';
m.style.opacity = '0';
if(cssTimer) {
clearTimeout(cssTimer)
}
var cssTimer = setTimeout(function () {
document.body.removeChild(m)
}, d * 1000);
}, duration);
}
};
2、在main.js中挂载上全局
main.js
import toast from './modal/modal.js'
Vue.prototype.$toast = toast
3、组件中使用
test.vue
this.$toast.toast("自定义弹窗时长弹窗", 3000);
什么是快乐星球?