Bootstrap告警框(alert)實(shí)現(xiàn)彈出效果和短暫顯示后上浮消失的示例代碼
最近用到bootstrap的告警框時(shí)發(fā)現(xiàn)只有html的說(shuō)明,就自己寫(xiě)了一個(gè)彈出告警框和彈出短暫顯示后上浮消失的告警框。
使用效果
移入時(shí)停止上浮的效果
直接上JS代碼了,可以copy過(guò)去直接用(使用bootstrap的UI框架的情況下)
var commonUtil = { /** * 彈出消息框 * @param msg 消息內(nèi)容 * @param type 消息框類型(參考bootstrap的alert) */ alert: function(msg, type){ if(typeof(type) =="undefined") { // 未傳入type則默認(rèn)為success類型的消息框 type = "success"; } // 創(chuàng)建bootstrap的alert元素 var divElement = $("<div></div>").addClass('alert').addClass('alert-'+type).addClass('alert-dismissible').addClass('col-md-4').addClass('col-md-offset-4'); divElement.css({ // 消息框的定位樣式 "position": "absolute", "top": "80px" }); divElement.text(msg); // 設(shè)置消息框的內(nèi)容 // 消息框添加可以關(guān)閉按鈕 var closeBtn = $('<button type="button" class="close" data-dismiss="alert" aria-label="Close"><span aria-hidden="true">×</span></button>'); $(divElement).append(closeBtn); // 消息框放入到頁(yè)面中 $('body').append(divElement); return divElement; }, /** * 短暫顯示后上浮消失的消息框 * @param msg 消息內(nèi)容 * @param type 消息框類型 */ message: function(msg, type) { var divElement = commonUtil.alert(msg, type); // 生成Alert消息框 var isIn = false; // 鼠標(biāo)是否在消息框中 divElement.on({ // 在setTimeout執(zhí)行之前先判定鼠標(biāo)是否在消息框中 mouseover : function(){isIn = true;}, mouseout : function(){isIn = false;} }); // 短暫延時(shí)后上浮消失 setTimeout(function() { var IntervalMS = 20; // 每次上浮的間隔毫秒 var floatSpace = 60; // 上浮的空間(px) var nowTop = divElement.offset().top; // 獲取元素當(dāng)前的top值 var stopTop = nowTop - floatSpace; // 上浮停止時(shí)的top值 divElement.fadeOut(IntervalMS * floatSpace); // 設(shè)置元素淡出 var upFloat = setInterval(function(){ // 開(kāi)始上浮 if (nowTop >= stopTop) { // 判斷當(dāng)前消息框top是否還在可上升的范圍內(nèi) divElement.css({"top": nowTop--}); // 消息框的top上升1px } else { clearInterval(upFloat); // 關(guān)閉上浮 divElement.remove(); // 移除元素 } }, IntervalMS); if (isIn) { // 如果鼠標(biāo)在setTimeout之前已經(jīng)放在的消息框中,則停止上浮 clearInterval(upFloat); divElement.stop(); } divElement.hover(function() { // 鼠標(biāo)懸浮時(shí)停止上浮和淡出效果,過(guò)后恢復(fù) clearInterval(upFloat); divElement.stop(); },function() { divElement.fadeOut(IntervalMS * (nowTop - stopTop)); // 這里設(shè)置元素淡出的時(shí)間應(yīng)該為:間隔毫秒*剩余可以上浮空間 upFloat = setInterval(function(){ // 繼續(xù)上浮 if (nowTop >= stopTop) { divElement.css({"top": nowTop--}); } else { clearInterval(upFloat); // 關(guān)閉上浮 divElement.remove(); // 移除元素 } }, IntervalMS); }); }, 1500); } }
調(diào)用部分
function login() { $.ajax({ url: "/apis/login/session", data: $('#loginForm').serialize(), dataType:"json", contentType: "application/json", success: function(result) { commonUtil.message(result.message); // 直接調(diào)用commonUtil對(duì)象的message方法 } }); }
總結(jié)
到此這篇關(guān)于Bootstrap告警框(alert)實(shí)現(xiàn)彈出效果和短暫顯示后上浮消失的文章就介紹到這了,更多相關(guān)Bootstrap告警框(alert)內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
js實(shí)現(xiàn)同一個(gè)頁(yè)面,多個(gè)enter事件綁定的示例
今天小編就為大家分享一篇js實(shí)現(xiàn)同一個(gè)頁(yè)面,多個(gè)enter事件綁定的示例,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2018-10-10javascript 模擬JQuery的Ready方法實(shí)現(xiàn)并出現(xiàn)的問(wèn)題
今天在閱讀網(wǎng)上一些模擬Jq的ready方法時(shí),發(fā)現(xiàn)一些小細(xì)節(jié),就是網(wǎng)上的ready事件大部分都是在onload事件執(zhí)行后加載,而jquery確能在onload加載前。2009-12-12JavaScrpt中如何使用 cookie 設(shè)置查看與刪除功能
這篇文章主要介紹了JavaScrpt中使用 cookie 設(shè)置查看與刪除功能的方法,文中通過(guò)實(shí)例代碼給大家介紹了js cookie常用的3個(gè)預(yù)設(shè)函數(shù)庫(kù),需要的朋友可以參考下2017-07-07django js 實(shí)現(xiàn)表格動(dòng)態(tài)標(biāo)序號(hào)的實(shí)例代碼
本文通過(guò)實(shí)例代碼給大家介紹了django js 實(shí)現(xiàn)表格動(dòng)態(tài)標(biāo)序號(hào) ,代碼簡(jiǎn)單易懂,非常不錯(cuò),具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2019-07-07JavaScript中Number對(duì)象的toFixed() 方法詳解
下面小編就為大家?guī)?lái)一篇JavaScript中Number對(duì)象的toFixed() 方法詳解。小編覺(jué)得挺不錯(cuò)的,現(xiàn)在就分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2016-09-09JS執(zhí)行控制之節(jié)流模式實(shí)例分析
這篇文章主要介紹了JS執(zhí)行控制之節(jié)流模式,結(jié)合實(shí)例形式分析了節(jié)流模式的功能、原理及相關(guān)使用方法,需要的朋友可以參考下2018-12-12