vue 自定义Toast提示

这篇博客介绍了如何使用纯JavaScript创建一个简单的提示框组件。通过定义样式和函数`Toast`,可以在页面中动态显示提示信息,并设定显示时间。这个提示框具有基本的样式设置,如背景颜色、边框、阴影等,且可以自定义HTML内容和显示时间。示例代码展示了如何调用`Toast`函数来显示指定时间长度的提示信息。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

 

<div id='toast'></div>

 

  .toast-container {
    position: fixed;
    top: 50%;
    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;
 color: #fff;
 display: none;
   
  }

 提示框方法

Toast(str, options = {}) {
        let {
          time,
          html
        } = options;
        if (!time) time = 1200;
        const toastEl = document.getElementById('toast');
        const className = 'toast-container';
        toastEl.className = className;
        if (html) {
          toastEl.innerHTML = html+str;
        } else {
          if (toastEl.textContent) {
            toastEl.textContent = str;
          } else {
            toastEl.innerText = str;
          }
        }
        toastEl.style='display: block'
        setTimeout(() => {
              toastEl.style='display: none'
        }, time);
      },

调用
time 显示时间
html  看情况自己修改,一般情况下传空

 this.Toast('提示语', {
              time: "1000",
              html: ""
            })

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值