javaScript、css实现toast

手机网页上时常要使用toast来弹出一下错误信息或提醒。开源上有一些很好的toast组件,例如jquery.toaster、toastr等等。但是他们的大小太大了,对于比较简单的页面,感觉有点得不偿失。
下面介绍一种利用css动画来实现的toast
HTML:

         <div class="toast-wrap">
            <span class="toast-msg"></span>
         </div>

CSS

   .toast-wrap{
      opacity: 0;
      position: fixed;
      bottom: 10%;
      color: #fff;
      width: 100%;
      text-align: center;
   }
   .toast-msg{
      background-color: rgba(0,0,0,0.7);
      padding: 2px 5px;
      border-radius: 5px;
   }
  .toastAnimate{
      animation: toastKF 2s;
  }
  @keyframes toastKF{
      0% {opacity: 0;}
     25% {opacity: 1; z-index: 9999}
     50% {opacity: 1; z-index: 9999}
     75% {opacity: 1; z-index: 9999}
     100% {opacity: 0; z-index: 0}
  }

javaScript

  function toast(msg){
    setTimeout(function(){
        document.getElementsByClassName('toast-wrap')[0].getElementsByClassName('toast-msg')[0].innerHTML=msg;
        var toastTag = document.getElementsByClassName('toast-wrap')[0];
        toastTag.className = toastTag.className.replace('toastAnimate','');
        setTimeout(function(){
            toastTag.className = toastTag.className + ' toastAnimate';
        }, 100);
    },500);
  }

使用方法:

    toast('Hi, darling');
  • 4
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 3
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值