DIV CSS 佈局教程網

JS 實作仿 IE 的可關閉頂部提示條
編輯:關於JavaScript     

就是那種頁面最上方橫跨整寬的黃色提示條,右側有個小叉可按關閉,頁面向下捲動時它會固定跟著——IE 的「安全性警告」就是這個樣子。功能實用、程式碼也不長,本文完整示範。

樣式

#informationbar {
  position:fixed; left:0; width:100%;
  text-indent:5px; padding:5px 0;
  background-color:lightyellow;
  border-bottom:1px solid black;
  font:bold 12px Verdana;
}
* html #informationbar {   /* IE6 hack */
  position:absolute;
  width:expression(document.compatMode=="CSS1Compat"
    ? document.documentElement.clientWidth+"px" : body.clientWidth+"px");
}

JavaScript

function informationbar() {
  this.displayfreq = "always";
  this.content = '<a href="javascript:informationbar.close()">' +
    '<img src="close.gif" style="width:14px;height:14px;float:right;' +
    'border:0;margin-right:5px" alt="關閉" /></a>';
}

// 設定提示內容
informationbar.prototype.setContent = function(data) {
  this.content = this.content + data;
  document.write('<div id="informationbar" style="top:-500px">' + this.content + '</div>');
};

// 從上方滑落下來
informationbar.prototype.animatetoview = function() {
  var barinstance = this;
  if (parseInt(this.barref.style.top) < 0) {
    this.barref.style.top = parseInt(this.barref.style.top) + 5 + "px";
    setTimeout(function() { barinstance.animatetoview(); }, 50);
  } else {
    // 舊 IE 用表達式讓提示條永遠固定在頂端
    if (document.all && !window.XMLHttpRequest)
      this.barref.style.setExpression("top",
        'document.compatMode=="CSS1Compat" ? document.documentElement.scrollTop+"px" : body.scrollTop+"px"');
    else
      this.barref.style.top = 0;
  }
};

// 關閉
informationbar.close = function() {
  document.getElementById("informationbar").style.display = "none";
  if (this.displayfreq == "session")
    document.cookie = "infobarshown=1;path=/";
};

// 設定顯示頻率:always(每次都顯示)或 session(本次工作階段只顯示一次)
informationbar.prototype.setfrequency = function(type) { this.displayfreq = type; };

informationbar.prototype.initialize = function() {
  if ((this.displayfreq == "session" &&
       document.cookie.indexOf("infobarshown") == -1) || this.displayfreq == "always") {
    this.barref = document.getElementById("informationbar");
    this.barheight = parseInt(this.barref.offsetHeight);
    this.barref.style.top = this.barheight * (-1) + "px"; // 先藏到畫面上方
    this.animatetoview();
  }
};

window.onunload = function() { this.barref = null; };

呼叫方式

var infobar = new informationbar();
infobar.setContent('敬告:明天中午 12 點天降錢雨,請備好麻袋! <a href="#">購買麻袋</a>');
infobar.initialize();

幾個設計細節

先藏再滑出。初始化時把 top 設為負的自身高度,讓提示條躲在畫面上方,再逐格往下移動營造落下效果。

用 cookie 記住「已關閉」。setfrequency('session') 會在關閉時寫入 cookie,下次載入時檢查到就不顯示,避免同一則訊息反覆打擾使用者。這是很好的習慣——推播式訊息若不給人「不要再顯示」的選項,很快就會被忽略甚至反感。

window.onunload 清空參考。把 barref 設為 null,避免舊版 IE 因 DOM 與 JS 之間的循環參考無法回收而洩漏記憶體。

這個版本不建議照抄的地方

document.write 用在 onload 之後會清空整個頁面。它會覆蓋掉目前文件的所有內容。正確做法是用 document.body.appendChild() 或 insertAdjacentHTML() 插入元素。

CSS expression 早已從所有瀏覽器移除。那段 IE6 hack 現在只有歷史價值。

用 javascript: 偽協定的 a 標籤不利於無障礙。關閉鈕應該用 <button>,並加上 aria-label="關閉提示",鍵盤使用者才能聚焦並操作。

提示條應該用 role="alert"。這樣螢幕閱讀器才會主動播報內容。

現代改寫

<div id="informationbar" role="alert">
  <span>敬告:明天中午 12 點天降錢雨,請備好麻袋!</span>
  <button type="button" aria-label="關閉提示">×</button>
</div>
var bar = document.getElementById('informationbar');

bar.querySelector('button').addEventListener('click', function() {
  bar.remove();
  // 用 localStorage 取代 cookie,語意更清楚
  localStorage.setItem('infobar_dismissed', '1');
});

// 本次工作階段不再顯示
if (localStorage.getItem('infobar_dismissed')) bar.remove();

樣式則用 position:sticky; top:0; 就能達到「捲動時固定在頂端」,不必任何 JavaScript。若希望不佔用版面,改用 position:fixed 並替 body 加上相當的 padding-top。

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