原生JS封装 toast 弹层,自动关闭

由于公司业务需求,要一个公共toast ,下面是自己封装的一个。

css:

.toast {
        text-align: center;
        min-height: 70px;
        width: 220px;
        color: #e6e6e6;
        border-radius: 4px;
        background: rgba(0, 0, 0, 0.7);
        position: fixed;
        left: 43%;
        top: 40%;
}

.toast-icon {
    padding-top: 10px;
    padding-bottom: 10px;
}

.toast-tip {
    padding-left: 20px;
    padding-right: 20px;
    padding-bottom: 10px;
}

js:

/*
 * @Author: liguowei
 * @Date:   2019-04-08 10:25:42
 * @Last Modified by:   liguowei01
 * @Last Modified time: 2019-04-08 11:17:17
 */
function toast(){};
toast.prototype = {
    init:function(){
        this.loadCss()
    },
    iconObj : {
        right:'data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAACYAAAAmCAYAAACoPemuAAADIElEQVRYR+2XT0jUQRTH3/e3u0YSYSDuzgwUCOHBS4e6BJmEEfRfiDAI6aAEYkTgsahDNy+BhUQgEgbRoZCoiEpSokuXLh26CAUz4/5YECSIdH++GHFjG9f9o9tq4Bxn5r33+X3fm/d2QZt0YZNy0RZYpZnZUuy/VSwMw71RFF0goqlUKjW5KVJprT3AzG+JaOeystc3HCydTh+Moug1Ee3IpZuZJzYUzFrbvri4+BLA9vwaZObeDQPTWh8loucAtnlQV5RSdzcEzFp7gpmfEVEiD4qJ6LKU8oHbqzmYMaaTiJ4QUTwfCsAlIcTD3F5NwbTWXQDGiChWDKqmillru5l51MtSxMwXlVKP/QZcE8VWgcoS0Xkppau1FQthGKay2ewoM7cCuCqlfFrp+Ch23xjTS0T3PaWyAM4KIV6sZgut9TiA08sXmJl7lFIj1YDTWvcDGPJ8LQDoLAa1VGNaa9dLTuYbA+gTQgyvB85aO8DMg16P+kVEp5RSb0r5hjFmPzNP+d2XiG5IKW+XclDoXGt9C8BND+pnEATHhRDvy/G5VPyF5tWy8R0p5bVyHOXuaK0HAQz4UPF4vCOZTH4s19efV1lgwud8DEsp+8pxqLUeAtDv3f0Ri8WOVQK1oo+FYb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toastTxtObj : {
        right:'成功',
        err:'失败',
        tip:'提示'
    },
    /*
    * icon 1成功   2失败  3 提示
    * toastTxt 提示语
    * time 关闭前的毫秒数 不传默认2000
     */
    showToast: function(icon,toastTxt,time){
        var iconSrc;        
        switch(parseInt(icon)){
            case 1:
            iconSrc = this.iconObj.right;
            break;
            case 2:
            iconSrc = this.iconObj.err;
            break;
            case 3:
            iconSrc = this.iconObj.tip;
            break;
            default:
            iconSrc = this.iconObj.right;
            break;
        }
        var toastTxt = toastTxt || this.toastTxtObj.right;
        html = '<div class="toast-icon"><img src="'+iconSrc+'"></div><div class="toast-tip">'+toastTxt+'</div></div>';
        toastBox = document.createElement('div');
        toastBox.className = 'toast';
        toastBox.id = 'toast';
        toastBox.innerHTML = html;
        document.body.appendChild(toastBox);
    setTimeout(function(){
      document.getElementById('toast').parentNode.removeChild(document.getElementById('toast'));
    },time||2000)
    },
    loadCss:function(){
        toastCss = document.createElement('link');
        toastCss.rel = 'stylesheet';
        toastCss.type = 'text/css';
        toastCss.href = this.T() + 'toast.css';
        document.head.appendChild(toastCss);
    },
    T:function() {
        var b, a, f = window['document']['getElementsByTagName']("script");
        for (var c = 0; c < f.length; c++) {
            b = f[c].getAttribute("src") || "";
            b = b.substr(0, b.toLowerCase().indexOf("toast.js"));
            var a = b.lastIndexOf("/");
            if (a > 0) {
                b = b.substring(0, a + 1)
            }
            if (b) {
                break;
            }
        }
        return b
    }



}
var T = new toast();
T.init();

已上代码不用看,按下面操作来就行:

1.新建文件夹 如toast

2.新建css文件,命名toast.css,把上面的css复制到这个文件里

3.新建js文件,命名toast.js,把上面的js复制到这个文件里

好了,准备完毕,下面是用法:

一、       仅引入js 如:

<script src="../static/plugins/toast/toast.js"></script>

二、       具体用法如下:

/*

* T.showToast(icon,toastTxt,time);

* 三个参数

* icon 1成功   2失败  3 提示

* toastTxt 提示语

* time 关闭前的毫秒数 不传默认2000

* 示例如下:

*/

T.showToast(1,'我是提示语',2000);

 

posted @ 2019-04-08 14:10  奔跑的太阳花  阅读(2226)  评论(0编辑  收藏  举报