DIV CSS 佈局教程網

用 JavaScript 做動畫展開的半透明提示層(含拖曳)
編輯:JavaScript綜合知識     

本文實作一個相當有手感的元件:點擊按鈕時,提示層會從左上角一點開始,先展開寬度再展開高度,形成一種由內向外生長的動畫;關閉時反向收合。此外還加上滑鼠拖曳移動的功能,拖曳過程中層會變半透明。

結構與樣式

<style type="text/css">
body,span,div,td{font-size:12px;line-height:1.5em;color:#849BCA;}

#bodyL{ float:left; width:84px; margin-right:2px; }

a.od{
  width:80px; height:25px; line-height:25px; text-align:center;
  font-weight:bold; border: 2px solid #849BCA; display:block;
  color:#547BC9; float:left; text-decoration:none; margin-top:2px;
}
a.od:link, a.od:visited { background:#EEF1F8; }
a.od:hover, a.od:active { background:#EEE; }

#fd{
  width:500px; height:200px; background:#EDF1F8;
  border: 2px solid #849BCA; margin-top:2px; margin-left:2px;
  overflow:hidden; position:absolute; left:0px; top:0px;
  cursor:move;
}

.content{ padding:10px; }
</style>

<div id="bodyL">
  <a href="#" class="od" onclick="show('fd');return false;">[打開層]</a>
  <a href="#" class="od" onclick="closeed('fd');return false;">[關閉層]</a>
</div>

<div id="fd" style="display:none;filter:alpha(opacity=100);opacity:1;">
  <div class="content">移動層</div>
</div>

overflow:hidden 是關鍵:層在展開過程中的實際尺寸會小於內容,沒有 overflow 的話文字會溢出到框外,動畫就破功了。

展開與收合動畫

var prox, proy, proxc, proyc;

function show(id){
  // 先清掉所有可能還在跑的計時器,避免動畫打架
  clearInterval(prox); clearInterval(proy);
  clearInterval(proxc); clearInterval(proyc);

  var o = document.getElementById(id);
  o.style.display = "block";
  o.style.width  = "1px";   // 從一個點開始
  o.style.height = "1px";

  prox = setInterval(function(){ openx(o, 500); }, 10);
}

// 展開寬度
function openx(o, x){
  var cx = parseInt(o.style.width);
  if (cx < x) {
    o.style.width = (cx + Math.ceil((x - cx) / 5)) + "px";
  } else {
    clearInterval(prox);
    proy = setInterval(function(){ openy(o, 200); }, 10);  // 寬度完成才展開高度
  }
}

// 展開高度
function openy(o, y){
  var cy = parseInt(o.style.height);
  if (cy < y) {
    o.style.height = (cy + Math.ceil((y - cy) / 5)) + "px";
  } else {
    clearInterval(proy);
  }
}

function closeed(id){
  clearInterval(prox); clearInterval(proy);
  clearInterval(proxc); clearInterval(proyc);

  var o = document.getElementById(id);
  if (o.style.display == "block") {
    proyc = setInterval(function(){ closey(o); }, 10);
  }
}

// 收合:先縮高度,再縮寬度
function closey(o){
  var cy = parseInt(o.style.height);
  if (cy > 0) {
    o.style.height = (cy - Math.ceil(cy / 5)) + "px";
  } else {
    clearInterval(proyc);
    proxc = setInterval(function(){ closex(o); }, 10);
  }
}

function closex(o){
  var cx = parseInt(o.style.width);
  if (cx > 0) {
    o.style.width = (cx - Math.ceil(cx / 5)) + "px";
  } else {
    clearInterval(proxc);
    o.style.display = "none";
  }
}

這裡有個值得學的設計:(x - cx) / 5 而不是固定步長。每次都走「剩餘距離的五分之一」,會形成先快後慢的 ease-out 減速效果——視覺上比等速移動自然得多,這正是很多動畫函式庫的實作原理。

滑鼠拖曳

var od = document.getElementById("fd");
var dx, dy, mx, my, mouseD;
var isIE = document.all ? true : false;

document.onmousedown = function(e){
  var e = e ? e : event;
  if (e.button == (document.all ? 1 : 0)) {
    mouseD = true;
  }
};

document.onmouseup = function(){
  mouseD = false;
  if (isIE) {
    od.releaseCapture();
    od.filters.alpha.opacity = 100;      // 恢復不透明
  } else {
    window.releaseEvents(od.MOUSEMOVE);
    od.style.opacity = 1;
  }
};

od.onmousedown = function(e){
  var e = e ? e : event;
  if (e.button == (document.all ? 1 : 0)) {
    mx = e.clientX;
    my = e.clientY;
    od.style.left = od.offsetLeft + "px";
    od.style.top  = od.offsetTop  + "px";

    if (isIE) {
      od.setCapture();
      od.filters.alpha.opacity = 50;     // 拖曳中變半透明
    } else {
      window.captureEvents(Event.MOUSEMOVE);
      od.style.opacity = 0.5;
    }
  }
};

document.onmousemove = function(e){
  var e = e ? e : event;
  if (mouseD == true) {
    var mrx = e.clientX - mx;
    var mry = e.clientY - my;
    od.style.left = (parseInt(od.style.left) + mrx) + "px";
    od.style.top  = (parseInt(od.style.top)  + mry) + "px";
    mx = e.clientX;    // 更新基準點,才能連續拖動
    my = e.clientY;
  }
};

拖曳的關鍵是 mx、my 記錄「上一個滑鼠位置」,每次移動時算出位移量後立刻更新基準點。少了這一步,第二次移動就會用錯誤的基準計算,層會瞬間飛出畫面。

現代寫法對照

同樣的效果,今天用 CSS transition 會簡短非常多:

<style>
#fd {
  width: 500px; height: 200px;
  overflow: hidden;
  transition: width .4s ease-out, height .4s ease-out;
}
#fd.collapsed { width: 0; height: 0; }
</style>

<script>
var fd = document.getElementById('fd');
fd.classList.add('collapsed');           // 初始收合

document.querySelector('.open').onclick  = function(){ fd.classList.remove('collapsed'); };
document.querySelector('.close').onclick = function(){ fd.classList.add('collapsed'); };
</script>

瀏覽器會自動處理逐幀計算,效能也更好。但理解 setTimeout 逐格推進的原理仍然重要——它讓你明白 transition 背後到底在做什麼,也讓你在需要「非線性、可中途打斷、要與其他邏輯協調」的複雜動畫時知道該怎麼自己動手。

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