bootstrap框架下封装一个Alert弹出框

1,026次阅读
没有评论

共计 2010 个字符,预计需要花费 6 分钟才能阅读完成。

bootstrap 框架下封装一个 Alert 弹出框

废话不说直接开始

新建 alert_box.js 文件

var commonUtil = {
/**
* 弹出消息框
* @param msg 消息内容
* @param type 消息框类型(参考 bootstrap 的 alert)*/
alert: function(msg, type){if(typeof(type) =="undefined") { // 未传入 type 则默认为 success 类型的消息框
type = "success";
}
// 创建 bootstrap 的 alert 元素
var divElement = $("<div></div>").addClass('alert').addClass('alert-'+type).addClass('solid').addClass('alert-dismissible').addClass('col-md-4').addClass('col-md-offset-4');
divElement.css({ // 消息框的定位样式
"position": "fixed",
"top": "15%",
"left":"40%",
"z-index":"10"
});
divElement.text(msg); // 设置消息框的内容
// 消息框添加可以关闭按钮
var closeBtn = $('<button type="button"class="btn-close"data-bs-dismiss="alert"aria-label="btn-close"></button>');
$(divElement).append(closeBtn);
// 消息框放入到页面中
$('body').append(divElement);
return divElement;
},
/**
* 短暂显示后上浮消失的消息框
* @param msg 消息内容
* @param type 消息框类型
*/
message: function(msg, type) {var divElement = commonUtil.alert(msg, type); // 生成 Alert 消息框
var isIn = false; // 鼠标是否在消息框中
        
divElement.on({ // 在 setTimeout 执行之前先判定鼠标是否在消息框中
  mouseover : function(){isIn = true;},
  mouseout  : function(){isIn = false;}
});
// 短暂延时后上浮消失
setTimeout(function() {
var IntervalMS = 20; // 每次上浮的间隔毫秒
var floatSpace = 60; // 上浮的空间 (px)
var nowTop = divElement.offset().top; // 获取元素当前的 top 值
var stopTop = nowTop - floatSpace;    // 上浮停止时的 top 值
divElement.fadeOut(IntervalMS * floatSpace); // 设置元素淡出
var upFloat = setInterval(function(){ // 开始上浮
if (nowTop >= stopTop) { // 判断当前消息框 top 是否还在可上升的范围内
divElement.css({"top": nowTop--}); // 消息框的 top 上升 1px
} else {clearInterval(upFloat); // 关闭上浮
divElement.remove();    // 移除元素}
}, IntervalMS);
if (isIn) { // 如果鼠标在 setTimeout 之前已经放在的消息框中,则停止上浮
                clearInterval(upFloat);
divElement.stop();}
divElement.hover(function() { // 鼠标悬浮时停止上浮和淡出效果,过后恢复
                clearInterval(upFloat);
divElement.stop();},function() {divElement.fadeOut(IntervalMS * (nowTop - stopTop)); // 这里设置元素淡出的时间应该为:间隔毫秒 * 剩余可以上浮空间
upFloat = setInterval(function(){ // 继续上浮
if (nowTop >= stopTop) {divElement.css({"top": nowTop--});
} else {clearInterval(upFloat); // 关闭上浮
divElement.remove();    // 移除元素}
}, IntervalMS);
});
}, 1500);
}
}

 

页面引入

<script src="static/js/alert_box.js"></script>

调用方法:

commonUtil.message(result.msg,'primary'); // 直接调用 commonUtil 对象的 message 方法,后面的 primary 参数也可以不加 

 

 

正文完
 0
评论(没有评论)