DIV CSS 佈局教程網

JavaScript 實現頁面右下角的浮動提示信息框
編輯:關於JavaScript     

系統操作完成後需要給個回饋,最簡單是 alert,但它會打斷操作且不美觀。比較好的做法是右下角滑出一張提示卡,停留幾秒自動收起,滑鼠移上去則暫停計時。以下這個 notice 模組只提供兩個方法:lay 設定尺寸,show 設定標題、內容與停留時間。

模組程式碼(notice.js)

var time;        // 自動收起的計時器
var delayTime;   // 停留時間

$(function(){

  // 動態加入浮動層
  $('body').append(
    "<div id='notice' onselectstart='return false'>" +
    "  <span class='notice_title'> </span>" +
    "  <span class='cbtn'>[關閉]</span>" +
    "  <div class='notice_content'></div>" +
    "</div>"
  );

  // 外層樣式:固定於右下角
  $('#notice').css({
    right: "0", bottom: "0", cursor: "default", position: "fixed",
    "background-color": "#CFDEF4", color: "#1F336B", "z-index": "999",
    border: "1px #1F336B solid", margin: "2px", padding: "10px",
    "font-weight": "bold", "line-height": "25px", display: "none"
  });

  // 關閉按鈕樣式
  $('#notice .cbtn').css({
    color: "#FF0000", cursor: "pointer", "padding-right": "5px",
    float: "right", position: "relative"
  });

  // 內容區樣式
  $('#notice .notice_content').css({
    margin: "3px", "font-weight": "normal", border: "1px #B9C9EF solid",
    "line-height": "20px", "margin-bottom": "10px", padding: "10px"
  });

  // 滑鼠移入展開並暫停計時,移出重新計時
  $('#notice').hover(
    function(){ $(this).stop(true, true).slideDown(); clearTimeout(time); },
    function(){ time = setTimeout('_notice()', delayTime); }
  );

  // 點擊 [關閉] 立即收起
  $('.cbtn').bind('click', function(){
    $('#notice').slideUp('fast');
    clearTimeout(time);
  });
});

// 對外方法一:設定提示框寬高
$.extend({
  lay: function(_width, _height){
    $('#notice').css({ width: _width, height: _height });
  },

  // 對外方法二:顯示提示
  show: function(_title, _msg, _time){
    delayTime = _time;
    $('.notice_title').html(_title);
    $('.notice_content').html(_msg);
    $('#notice').slideDown(2000);
    time = setTimeout('_notice()', delayTime);
  }
});

function _notice(){
  $('#notice').slideUp(2000);
}

呼叫方式(index.html)

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
  "https://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html>
<head>
<title>index.html</title>
<meta http-equiv="keywords" content="keyword1,keyword2,keyword3" />
<meta http-equiv="description" content="this is my page" />
<meta http-equiv="content-type" content="text/html; charset=GBK" />
</head>

<body>
<script type="text/javascript">
function initPage(){
  var returnMsg =
    "<p>資訊1 jquery-1.7.2.min.js</p>" +
    "<p>資訊2 notice.js</p>" +
    "<p>資訊3</p>";
  $.show('提示資訊', returnMsg, 10000);
}
initPage();
</script>

<!-- 注意:notice.js 依賴 jQuery,必須先引入 -->
<script src="jquery-1.7.2.min.js" type="text/javascript"></script>
<script src="notice.js" type="text/javascript"></script>
</body>
</html>

使用時要留意的幾點

1. 腳本載入順序。原始碼把 initPage() 放在 body 中的 script,卻在之後才引入 jQuery 與 notice.js,會出現「$.show is not a function」的錯誤。上述範例已調整順序,或改用 window.onload 延後執行。

2. onselectstart 只有 IE 支援。要跨瀏覽器防止選取文字,應改用 CSS 的 user-select: none。

3. 內容用 html() 寫入有 XSS 風險。若 _msg 含使用者輸入,請改用 text(),或先消毒再傳入。

4. 計時器要收斂。連續呼叫 show 時,前一個計時器必須先 clearTimeout,否則新提示會被舊計時器提前收掉。範例在 show 開頭少了這一步,建議補上。

5. position: fixed 的相容性。IE6 不支援,若還要照顧可改用 position: absolute 搭配 JS 補上捲動補償。

6. slideDown(2000) 偏慢。2 秒的展開動畫在實務上顯得拖沓,通常 200–300ms 的手感最好。

7. 若要同時堆疊多則訊息,可把單一 #notice 改成容器 + 多個子項的結構,每則各自計時,避免互相覆蓋。

XML學習教程| jQuery入門知識| AJAX入門| Dreamweaver教程| Fireworks入門知識| SEO技巧| SEO優化集錦|
Copyright © DIV+CSS佈局教程網 All Rights Reserved