Bootstrap告警框(alert)實現彈出效果和短暫顯示后上浮消失


最近用到bootstrap的告警框時發現只有html的說明,就自己寫了一個彈出告警框彈出短暫顯示后上浮消失的告警框

使用效果

移入時停止上浮的效果

直接上JS代碼了,可以copy過去直接用(使用bootstrap的UI框架的情況下)

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('alert-dismissible').addClass('col-md-4').addClass('col-md-offset-4');
        divElement.css({ // 消息框的定位樣式
            "position": "absolute",
            "top": "80px"
        });
        divElement.text(msg); // 設置消息框的內容
        // 消息框添加可以關閉按鈕
        var closeBtn = $('<button type="button" class="close" data-dismiss="alert" aria-label="Close"><span aria-hidden="true">&times;</span></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);
    }
}

調用部分

function login() {
    $.ajax({
        url: "/apis/login/session",
        data: $('#loginForm').serialize(),
        dataType:"json",
        contentType: "application/json",
        success: function(result) {
            commonUtil.message(result.message); // 直接調用commonUtil對象的message方法
        }
    });
}

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM