通过Jquery实现轻量型吐丝消息框

js代码

var luckToast = function () {
    context = "body";
    left = 0;
    toppx = 0;
    time = 3000;
    msgEntity = {};
    width = 100;
    innerinit = function () {
        left = $(context).width() / 2 - msgEntity.find('span').width() / 2;
        toppx = window.screen.availHeight / 2;
    }
    initWith = function (str) {
        width = str.length * 20
    }
    appendDiv = function (message) {
        var msgDIV = new Array();
        msgDIV.push('<div id="toastMessage">');
        msgDIV.push('<span>' + message + '</span>');
        msgDIV.push('</div>');
        msgEntity = $(msgDIV.join('')).appendTo(context + "").hide();
    }
    show = function (message) {
        var toastObj = $("#toastMessage");
        if (toastObj) {
            toastObj.remove();
        }
        appendDiv(message);
        innerinit();
        initWith(message);
        //设置消息样式  
        toastObj = $("#toastMessage");
        toastObj.css({
            position: 'absolute', top: '0px', 'z-index': '19999', left: '0px', 'background-color': 'black', color: 'white', 'font-size': '18px', margin: 'auto', right: "0px", bottom: "0px", width: width + "px", height: "20px", padding: "10px", "border-radius": ".5em",
            "text-align": "center", "vertical-align": " middle;", "line-height": " 20px;"
        });
        toastObj.hide();
        toastObj.fadeIn(time / 2);
        toastObj.fadeOut(time / 2);
    }
    return { show: show };
}
function luckToast(msg) {
    var M_toastMsg = new luckToast();
    M_toastMsg.show(msg);
}

页面效果:

代码上传

https://download.csdn.net/download/qq_25744257/10645384

修改:

position: 'absolute',改为position: 'fixed',适应移动端

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值