封装自己的alert toast

class Toast {
  constructor() {}
  // 传入的一个参数可以是一个字符串或者一个对象 
  default(org, callback) {
    let config = {
      text: '',
      tit: '提示',
      type: 'toast',
      timeOut: 1800
    }
    if (callback) {
      config.type = 'alert'
    }

    if (typeof org === 'string') {
      config.text = org
    } else {
      config.text = org.text || ''
      config.tit = org.tit || '提示'
      config.timeOut = org.timeOut || 2000
    }
    this.createElement(config, callback)
  }
// 默认有回调函数的是alert 无回调的是toast
  createElement(config, _callback) {
    if (_callback) {
      this.createAlert(config, _callback)
    } else {
      this.createToast(config)
    }
  }

  createToast(config) {
    var p = document.createElement('p')
    p.innerText = config.text
    p.setAttribute('style', this.setStyle(config))
    document.body.appendChild(p)

    // toast 自动消失
    setTimeout(() => {
      document.body.removeChild(p)
    }, config.timeOut)
  }

  createAlert(config, callback) {
    let odiv = document.createElement('div')
    odiv.style =
      'position:fixed;top:0;left:0;bottom:0;right:0;background:rgba(0,0,0,.8);'

    let boxSize =
      'width:240px;background:#fff;position:absolute;top:30%;left:50%;margin-left:-120px;border-radius:10px;'
    let fontSize =
      'padding:10px;font-size:16px;color:#000;line-height:20px;min-height:20px'
    let box =
      `<div style=` +
      boxSize +
      `>
      <h4 style='text-align:center; font-size:18px;font-weight:700;'>` +
      config.tit +
      `</h4>
        <p style=` +
      fontSize +
      `>` +
      config.text +
      `</p>
        <p style='border-top:1px solid #eee;display:flex;height:30px;line-height:30px;text-align:center;'>
          <span style='flex:1;border-right:1px solid #eee;' class='cancle'>取消</span>
          <span style='flex:1;' class='sure'>确定</span>
        </p>
    </div>`
    odiv.innerHTML = box
    document.body.appendChild(odiv)
    // alert点击box外部,取消按钮 页面alert 消失
    // 点击确定按钮执行回调, alert消失
    odiv.onclick = () => {
      document.body.removeChild(odiv)
    }

    document.querySelector('.cancle').onclick = e => {
      document.body.removeChild(odiv)
      e.stopPropagation()
    }

    document.querySelector('.sure').onclick = e => {
      e.stopPropagation()
      callback()
      document.body.removeChild(odiv)
    }
  }

  setStyle(config) {
  // toast 根据文字的长度设置div的宽度
  // 其实这个有很多办法实现,我这边主要用的是js
    let width = config.text.length * 17.5
    let css =
      'width:' +
      width +
      'px;' +
      'height:50px;line-height:50px;font-size:16px;position:fixed;top:30%;left:50%;margin-left:-' +
      width / 2 +
      'px;border-radius:10px;text-align:center;padding:0 10px;box-sizing:content-box;background:rgba(0,0,0,.5);color:#fff;border:1px solid #909399;'
    return css
  }
}
export default new Toast()
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
在 UniApp 中封装一个 Toast 组件非常简单,可以按照以下步骤进行操作: 1. 创建一个新的 .vue 文件,用于封装 Toast 组件。 2. 在该文件中,定义一个 template(模板)来展示 Toast 内容,例如: ```html <template> <div class="toast">{{ message }}</div> </template> ``` 3. 在该文件中,使用 props 来接收 Toast 组件的参数,例如: ```javascript <script> export default { props: { message: String } } </script> ``` 4. 使用 CSS 样式来美化 Toast 组件的外观,例如: ```css <style scoped> .toast { position: fixed; top: 50%; left: 50%; transform: translate(-50%, -50%); padding: 10px 20px; background-color: rgba(0, 0, 0, 0.8); color: #fff; border-radius: 4px; } </style> ``` 5. 在需要使用 Toast 的页面或组件中,引入并注册 Toast 组件。例如,在 App.vue 中注册: ```javascript <template> <div> <toast :message="toastMessage" v-if="showToast"></toast> <!-- 其他内容 --> </div> </template> <script> import Toast from '@/components/Toast.vue' export default { components: { Toast }, data() { return { showToast: false, toastMessage: '' } }, methods: { showToast(message) { this.toastMessage = message this.showToast = true // 设置一定时间后关闭 Toast setTimeout(() => { this.showToast = false }, 2000) } } } </script> ``` 6. 在需要弹出 Toast 的地方,调用 showToast 方法,并传入相应的消息: ```javascript this.showToast('Hello, UniApp!') ``` 这样,你就成功地封装了一个简单的 Toast 组件,在需要时可以随时调用并显示相应的提示信息。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值