欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

Bootstrap告警框(alert)實(shí)現(xiàn)彈出效果和短暫顯示后上浮消失的示例代碼

 更新時(shí)間:2020年08月27日 09:50:20   作者:你像六月微風(fēng)  
這篇文章主要介紹了Bootstrap告警框(alert)實(shí)現(xiàn)彈出效果和短暫顯示后上浮消失,本文通過(guò)實(shí)例代碼給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下

最近用到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">&times;</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)文章

最新評(píng)論