DIV CSS 佈局教程網

JS 評分後彈出提示:拋物線上浮加淡出,仿中關村論壇
編輯:關於JavaScript     

在論壇按評分後跳出「謝謝參與!經驗 +5」然後往上飄走淡出,是論壇系統常見的回饋動畫。這個範例用物件導向的寫法把「位移」與「淡出」兩個階段封裝起來。

一、程式碼

<script language="javascript">
var x = window.x || {};

x.creat = function (t, b, c, d) {
  this.t = t;      // 目前時間(帧計數)
  this.b = b;      // 起始 top 百分比
  this.c = c;      // 拋物線開口大小係數
  this.d = d;      // 總時長
  this.op = 1;     // 目前不透明度

  this.div = document.createElement("div");
  this.div.style.height = "40px";
  this.div.style.width  = "100px";
  this.div.style.background = "red";
  this.div.style.position = "absolute";
  this.div.style.left = "50%";
  this.div.style.marginLeft = "-50px";       // 水平置中
  this.div.style.marginTop  = "-20px";
  this.div.innerText = "謝謝參與!經驗+5";
  this.div.style.fontSize  = "12px";
  this.div.style.lineHeight = this.div.style.height;
  this.div.style.textAlign  = "center";
  this.div.style.fontWeight = "bold";
  this.div.style.color = "#fff";
  this.div.style.top  = (this.b + "%");
  document.body.appendChild(this.div);

  this.run();
};

x.creat.prototype = {
  // 第一階段:沿拋物線往上移動
  run: function () {
    var me = this;
    this.div.style.top = -this.c * (this.t / this.d) * (this.t / this.d) + this.b + "%";
    this.t++;
    this.q = setTimeout(function () { me.run(); }, 25);
    if (this.t == this.d) {
      clearTimeout(me.q);
      setTimeout(function () { me.alpha(); }, 1000);   // 停留 1 秒後淡出
    }
  },

  // 第二階段:逐漸透明後移除
  alpha: function () {
    var me = this;
    if ("\v" == "v") {          // 偵測舊 IE
      this.div.style.filter = "progid:DXImageTransform.Microsoft.Alpha(opacity=" + this.op * 100 + ")";
      this.div.style.filter = "alpha(opacity=" + this.op * 100 + ")";
    } else {
      this.div.style.opacity = this.op;
    }
    this.op -= 0.02;
    this.w = setTimeout(function () { me.alpha(); }, 25);
    if (this.op <= 0) {
      clearTimeout(this.w);
      document.body.removeChild(me.div);       // 淡出完就從 DOM 移除
    }
  }
};

new x.creat(1, 50, 25, 30);
</script>

二、拋物線是怎麼算出來的

關鍵是這一行:

top = -c * (t / d)² + b

把 t / d 當成進度(0 → 1),平方之後再乘上係數 c,就得到一條開口向下的拋物線。因為前面有負號,top 會隨時間推移而變小(往上移動),而且越接近結尾移動越慢——這正是重力拋體的視覺特徵,比等速上移自然得多。

參數意義:b 是起始位置(50%)、c 控制飄多高、d 是動畫總帧數(30 × 25ms ≈ 0.75 秒)。

三、淡出階段的細節

位移結束後停 1 秒讓使用者看清文字,接著每 25ms 把 opacity 減 0.02,約 50 步(1.25 秒)完全消失,最後 removeChild 把節點從 DOM 移除以釋放記憶體。

"\v" == "v" 是一個老派的 IE 偵測技巧:IE8 以下會把 \v 解讀為垂直定位字元 0x0B,其他瀏覽器則視為 v。今天已無必要,直接用 style.opacity 即可。

四、可改善的地方

1. innerText 建議改 textContent,相容性更好且不觸發 reflow。

2. fontSize = "12" 缺少單位,標準模式下會被忽略,應寫 "12px"。

3. 用 setTimeout 鏈做動畫會掉幀。建議改用 CSS animation,由瀏覽器合成器執行:

@keyframes floatUp {
  0%   { transform: translateY(0);     opacity: 1; }
  30%  { transform: translateY(-60px); opacity: 1; }
  100% { transform: translateY(-120px); opacity: 0; }
}
.toast {
  position: fixed; left: 50%; top: 50%; margin-left: -50px;
  animation: floatUp 2s ease-out forwards;
}
.toast.addEventListener('animationend', function () { this.remove(); });

這樣整段 JS 只剩「建立元素 + 加入 class」,程式碼少一半、效能更好,而且 transform 與 opacity 都不觸發 reflow。

4. 應該用 aria-live 通知螢幕閱讀器。視覺提示對看不見的使用者無效,加上 role="status" 或 aria-live="polite" 讓輔具也能播報「獲得 5 點經驗」。

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