vue封装提示框

定义样式 sass

.toast-container
    position: absolute
    top: 45%
    left: 50%
    transform: translate(-50%, -50%)
    width: 80%
    height: auto
    padding: 20px
    background-color: #333
    border-radius: 5px
    box-shadow: 0 0 10px rgba(0, 0, 0, .4)
    text-align: center
    font-size: 14px
    transition: opacity .3s
    &.fade-out
        opacity: 0

定义js

import '@/assets/style/toast.sass';

function Toast (str, options = {}) {
    let { time, html } = options;
    if (!time) time = 1200;
    const toastEl = document.createElement('div');
    const className = 'toast-container';
    toastEl.className = className;
    if (html) {
        toastEl.innerHTML = str;
    } else {
        if (toastEl.textContent) {
            toastEl.textContent = str;
        } else {
            toastEl.innerText = str;
        }
    }
    document.body.appendChild(toastEl);
    setTimeout(() => {
        toastEl.className = className + ' fade-out';
        setTimeout(() => {
            document.body.removeChild(toastEl);
        }, 300);
    }, time);
}
导出
export default Toast;

使用

在main.js里面引入
import toast from '@/自己的路径/toast.js';
挂载到原型上
Vue.prototype.toast = toast;

在页面里面使用
this.toast('字符串',{time:"时间",html:"html模板或者是文本"})
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值