Alert方法重写

在正规项目中,总感觉alert框是非常难看的,但是有的时候又必须添加alert框来给用户一种警醒,废话不多说,先上图

下面是脚本代码,基于jQuery的

/// <summary>
/// 重写alert方法
/// </summary>
/// <param name="message">消息内容</param>
/// <param name="title">弹出窗标题</param>
/// <param name="title">点击确认按钮的回调函数</param>
window.alert = function (message, title, callbak) {
    /// <summary>
    /// 关闭Alert弹出窗
    /// </summary>
    (closeAlert = function () { $('#alert-window').remove(); })();
    var bg = $('<div class="alert-bg"></div>');
    var wnd = $('<div class="alert-wnd"><div class="alert-head"><center>'
                + (title || "提示信息")
                + '</center></div><div class="alert-body"><div class="alert-icon"><span></span></div><div class="alert-msg"><p>'
                + (message || '') + '</p></div></div><div class="alert-footer"><a class="btn btn-small" id="alert-confirm">确认</a></div></div>');
    window.top.$(document.body).append($('<div id="alert-window"></div>').append(bg).append(wnd).delegate('#alert-confirm', 'click', function () { closeAlert(); callbak && callbak(); }));
};

下面是css代码:

.btn {
    background-color: #F5F5F5;
    background-image: linear-gradient(to bottom, #FFFFFF, #E6E6E6);
    background-repeat: repeat-x;
    border-bottom: 0 none #B3B3B3;
    border-radius: 4px;
    border-right: 0 none #E6E6E6;
    border-top: 0 none #E6E6E6;
    box-shadow: 0 1px 0 rgba(255, 255, 255, 0.2) inset, 0 1px 2px rgba(0, 0, 0, 0.05);
    color: #333333;
    cursor: pointer;
    display: inline;
    font-size: 14px;
    line-height: 20px;
    margin-bottom: 0;
    padding: 4px 12px;
    text-align: center;
    text-shadow: 0 1px 1px rgba(255, 255, 255, 0.75);
    vertical-align: middle;
}
a, input[type="button"] {
    border-left: 1px none;
}
a {
    color: #007FBF;
    outline: medium none;
    text-decoration: none;
}
a {
    color: #0088CC;
    text-decoration: none;
}
.alert-bg {
    background-color: #999999;
    bottom: 0;
    left: 0;
    position: fixed;
    right: 0;
    top: 0;
    z-index: 1100;
    opacity: 0.3;
}

.alert-wnd {
    width: 320px;
    height: 120px;
    margin-left: -160px;
    margin-top: -160px;
    box-shadow: 5px 5px 5px #AAAAAA;
    left: 50%;
    top: 50%;
    z-index: 1110;
    background-color: #ffffff;
    font-family: 'Microsoft YaHei';
    font-size: 12px;
    opacity: 1;
    position: fixed;
    border: 5px solid #999999;
}

    .alert-wnd .alert-head {
        height: 24px;
        border-bottom: 1px solid #999999;
        font-weight: 600;
        background-image: -moz-linear-gradient(top, #F1F1F1, #CCCCCC); /* Firefox */
        background-image: -webkit-gradient(linear, left top, left bottom, color-stop(0, #F1F1F1), color-stop(1, #CCCCCC)); /* Saf4+, Chrome */
        filter: progid:DXImageTransform.Microsoft.gradient(startColorstr='#F1F1F1', endColorstr='#CCCCCC', GradientType='0'); /* IE*/
    }

    .alert-wnd .alert-body {
        height: 60px;
    }

        .alert-wnd .alert-body .alert-icon {
            width: 80px;
            height: 60px;
            float: left;
        }

            .alert-wnd .alert-body .alert-icon span {
                background-image: url("../img/alert.png");
                width: 32px;
                height: 32px;
                display: block;
                float: left;
                margin-left: 20px;
                margin-top: 15px;
            }

        .alert-wnd .alert-body .alert-msg {
            float: left;
            width: 240px;
            height: 60px;
        }

            .alert-wnd .alert-body .alert-msg p {
                margin-top: 20px;
                width: 225px;
            }
.btn {
    background-color: #F5F5F5;
    background-image: linear-gradient(to bottom, #FFFFFF, #E6E6E6);
    background-repeat: repeat-x;
    border-bottom: 0 none #B3B3B3;
    border-radius: 4px;
    border-right: 0 none #E6E6E6;
    border-top: 0 none #E6E6E6;
    box-shadow: 0 1px 0 rgba(255, 255, 255, 0.2) inset, 0 1px 2px rgba(0, 0, 0, 0.05);
    color: #333333;
    cursor: pointer;
    display: inline;
    font-size: 14px;
    line-height: 20px;
    margin-bottom: 0;
    padding: 4px 12px;
    text-align: center;
    text-shadow: 0 1px 1px rgba(255, 255, 255, 0.75);
    vertical-align: middle;
}
a, input[type="button"] {
    border-left: 1px none;
}
a {
    color: #007FBF;
    outline: medium none;
    text-decoration: none;
}
a {
    color: #0088CC;
    text-decoration: none;
}
/*文飞  20:52:07 */
.btn:active,
.btn.active {
  background-color: #cccccc \9;
}

.btn:first-child {
  *margin-left: 0;
}

.btn:hover,
.btn:focus {
  color: #333333;
  text-decoration: none;
  background-position: 0 -15px;
  -webkit-transition: background-position 0.1s linear;
     -moz-transition: background-position 0.1s linear;
       -o-transition: background-position 0.1s linear;
          transition: background-position 0.1s linear;
}

.btn:focus {
  outline: thin dotted #333;
  outline: 5px auto -webkit-focus-ring-color;
  outline-offset: -2px;
}

.btn.active,
.btn:active {
  background-image: none;
  outline: 0;
  -webkit-box-shadow: inset 0 2px 4px rgba(0, 0, 0, 0.15), 0 1px 2px rgba(0, 0, 0, 0.05);
     -moz-box-shadow: inset 0 2px 4px rgba(0, 0, 0, 0.15), 0 1px 2px rgba(0, 0, 0, 0.05);
          box-shadow: inset 0 2px 4px rgba(0, 0, 0, 0.15), 0 1px 2px rgba(0, 0, 0, 0.05);
}

对了还有一个资源文件

当加上上述代码到你的工程中,普通的alert方法就会产生如图的效果。这样alert的样式就随心所欲了。

现在还有没解决的问题是,这种alert框不能像浏览器自带的alert框那样停止脚本的运行,有没有完美的解决方案呢?

转载于:https://www.cnblogs.com/dacuotecuo/p/3481017.html

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值