Bootstrap告警框(alert)實現(xiàn)彈出效果和短暫顯示后上浮消失的示例代碼
最近用到bootstrap的告警框時發(fā)現(xiàn)只有html的說明,就自己寫了一個彈出告警框和彈出短暫顯示后上浮消失的告警框。
使用效果
移入時停止上浮的效果
直接上JS代碼了,可以copy過去直接用(使用bootstrap的UI框架的情況下)
var commonUtil = { /** * 彈出消息框 * @param msg 消息內(nèi)容 * @param type 消息框類型(參考bootstrap的alert) */ alert: function(msg, type){ if(typeof(type) =="undefined") { // 未傳入type則默認為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); // 設置消息框的內(nèi)容 // 消息框添加可以關閉按鈕 var closeBtn = $('<button type="button" class="close" data-dismiss="alert" aria-label="Close"><span aria-hidden="true">×</span></button>'); $(divElement).append(closeBtn); // 消息框放入到頁面中 $('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; // 鼠標是否在消息框中 divElement.on({ // 在setTimeout執(zhí)行之前先判定鼠標是否在消息框中 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是否還在可上升的范圍內(nèi) divElement.css({"top": nowTop--}); // 消息框的top上升1px } else { clearInterval(upFloat); // 關閉上浮 divElement.remove(); // 移除元素 } }, IntervalMS); if (isIn) { // 如果鼠標在setTimeout之前已經(jīng)放在的消息框中,則停止上浮 clearInterval(upFloat); divElement.stop(); } divElement.hover(function() { // 鼠標懸浮時停止上浮和淡出效果,過后恢復 clearInterval(upFloat); divElement.stop(); },function() { divElement.fadeOut(IntervalMS * (nowTop - stopTop)); // 這里設置元素淡出的時間應該為:間隔毫秒*剩余可以上浮空間 upFloat = setInterval(function(){ // 繼續(xù)上浮 if (nowTop >= stopTop) { divElement.css({"top": nowTop--}); } else { clearInterval(upFloat); // 關閉上浮 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對象的message方法 } }); }
總結
到此這篇關于Bootstrap告警框(alert)實現(xiàn)彈出效果和短暫顯示后上浮消失的文章就介紹到這了,更多相關Bootstrap告警框(alert)內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關文章希望大家以后多多支持腳本之家!
相關文章
js實現(xiàn)同一個頁面,多個enter事件綁定的示例
今天小編就為大家分享一篇js實現(xiàn)同一個頁面,多個enter事件綁定的示例,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2018-10-10javascript 模擬JQuery的Ready方法實現(xiàn)并出現(xiàn)的問題
今天在閱讀網(wǎng)上一些模擬Jq的ready方法時,發(fā)現(xiàn)一些小細節(jié),就是網(wǎng)上的ready事件大部分都是在onload事件執(zhí)行后加載,而jquery確能在onload加載前。2009-12-12JavaScrpt中如何使用 cookie 設置查看與刪除功能
這篇文章主要介紹了JavaScrpt中使用 cookie 設置查看與刪除功能的方法,文中通過實例代碼給大家介紹了js cookie常用的3個預設函數(shù)庫,需要的朋友可以參考下2017-07-07django js 實現(xiàn)表格動態(tài)標序號的實例代碼
本文通過實例代碼給大家介紹了django js 實現(xiàn)表格動態(tài)標序號 ,代碼簡單易懂,非常不錯,具有一定的參考借鑒價值,需要的朋友可以參考下2019-07-07JavaScript中Number對象的toFixed() 方法詳解
下面小編就為大家?guī)硪黄狫avaScript中Number對象的toFixed() 方法詳解。小編覺得挺不錯的,現(xiàn)在就分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2016-09-09