DIV CSS 佈局教程網

純 JS 實現仿 QQ 郵箱彈出確認框
編輯:JavaScript綜合知識     

本文將分享一個仿 QQ 郵箱風格的彈出確認框,界面與交互是透過 Firebug 抓取 QQ 郵箱的 HTML 與 CSS 復刻而來。代碼未作過多封裝,僅定義了一個可拖動的窗口工具類,大家可以在其基礎上封裝出屬於自己的彈出窗。

完整代碼如下:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "https://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<HTML>
<HEAD>
<TITLE>QQ郵箱的彈出層</TITLE>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<style>
body { background: none repeat scroll 0 0 #FFFFFF; font-family: "lucida Grande",Verdana; font-size: 12px; }
select, body, textarea { font-size: 12px; }
.tipbg { margin: 0; padding: 0; background-color: transparent; }
.qmpanel_shadow { border-radius: 3px 3px 3px 3px; box-shadow: 0 2px 7px rgba(0, 0, 0, 0.35); }
.bd_upload { border: 1px solid #628D0B; }
.fdbody { border-left: 8px solid #FFFFFF; border-right: 1px solid #87A34D; }
.fdbody, .tipstitle { background: none repeat scroll 0 0 #9BBB59; }
.editor_dialog_title { color: white; font: bold 12px "lucida Grande",Verdana; padding: 9px 0 7px 10px; text-align: left; }
.editor_dialog_content { background: none repeat scroll 0 0 #FFFFFF; filter: none; margin: 0; padding: 0; text-align: center; }
.mailinfo { border-bottom: 2px solid #FFFFFF; background: none repeat scroll 0 0 #FFFFFF; }
.cnfx_content { padding: 10px 0 5px 10px; text-align: left; }
.cnfx_status { float: left; padding: 0 0 0 10px; }
.cnfx_btn { padding: 0 10px 10px 0; text-align: right; }
.b_size { font-size: 14px; }
.editor_close { background: none repeat scroll 0 0 #DC4835; }
.editor_close, .editor_close_mover { border: 1px solid #A7190F; cursor: pointer; float: right; margin: 7px 7px 0 0; }
.editor_close img, .editor_close_mover img, .editor_min img, .editor_min_mover img { display: block; }
img { border: medium none; }
.wd2 { margin: 1px 1px 0 0; width: 64px; }
.btn, button, .qm_btn { padding-left: 0; padding-right: 0; }
input, textarea, a { outline: medium none; }
.editor_mask { background: none repeat scroll 0 0 #FFFFFF; height: 100%; left: 0; opacity: 0.5; position: absolute; top: 0; width: 100%; }
</style>
<script>
///////// 拖拽工具類 ////////
var DragUtil = (function(){
  var doc = document;
  var moveX = 0;
  var moveY = 0;
  var moveTop = 0;
  var moveLeft = 0;
  var moveable = false;
  return {
    /**
     * 注冊拖拽
     * 需要傳入整個窗體id和標題部分的id
     */
    regist:function(winId,titleId) {
      // 頁面頭部要加上 DOCTYPE 聲明,否則會有問題
      var width = doc.documentElement.clientWidth;
      var height = doc.documentElement.clientHeight;
      var title = doc.getElementById(titleId);
      var win = doc.getElementById(winId);
      title.onmousedown = function() {
        var evt = DragUtil._getEvent();
        moveable = true;
        moveX = evt.clientX;
        moveY = evt.clientY;
        moveTop = parseInt(win.style.top);
        moveLeft = parseInt(win.style.left);
        doc.onmousemove = function() {
          if (moveable) {
            var evt = DragUtil._getEvent();
            var x = moveLeft + evt.clientX - moveX;
            var y = moveTop + evt.clientY - moveY;
            var w = parseInt(win.style.width);
            var h = parseInt(win.style.height);
            if ( x > 0 &&( x + w < width) && y > 0 && (y + h < height) ) {
              win.style.left = x + "px";
              win.style.top = y + "px";
            }
          }
        };
        doc.onmouseup = function () {
          if (moveable) {
            moveable = false;
            moveX = 0;
            moveY = 0;
            moveTop = 0;
            moveLeft = 0;
          }
        };
      }
    },
    /**
     * 獲取事件
     */
    _getEvent:function(){
      return window.event || arguments.callee.caller.arguments[0];
    }
  }
})()
///////////////////////
function init(){
  DragUtil.regist("WindowId","titleId")
  DragUtil.regist("WindowId2","titleId2")
}
</script>
</HEAD>
<BODY onload="init()">
<span id="qmdialog_container"><div style="z-index: 1120; position: absolute; width: 447px; height: 163px; opacity: 1; left: 514px; top: 124px;" id="WindowId">
<div class="tipbg">
<div class="opashowOuter qmpanel_shadow">
<table cellspacing="0" cellpadding="0" style="width: 447px; height: 163px;background: white;" class="bd_upload">
<tbody>
<tr>
<td style="height: 28px; border: none; cursor: move; overflow: hidden;" class="editor_dialog_titlebar fdbody">
<div id="titleId" class="editor_dialog_title">刪除確認</div>
</td>
</tr>
<tr>
<td valign="top" style="height: 131px; border: medium none;" class="editor_dialog_content">
<div class="mailinfo">
<div class="cnfx_content">
<table style="width: 350px; height: 80px;"><tbody><tr>
<td style="vertical-align: top;"><div class="b_size" style="padding-top: 10px; line-height: 150%;">
<div>徹底刪除後郵件將無法恢復,您確定要刪除嗎?</div>
</div></td></tr></tbody>
</table>
</div>
<div class="cnfx_btn">
<input type="button" value="確定" class="wd2 btn">
<input type="button" value="取消" class="wd2 btn">
</div>
</div>
</div>
</td>
</tr>
</tbody>
</table>
</div>
</div>
</div>
</span>
</BODY>
</HTML>

核心在於 DragUtil 這個自封裝的拖拽工具類:透過 regist 方法為指定窗口與標題欄分別綁定 onmousedown、onmousemove、onmouseup 事件,並在拖動過程中即時計算新的 left 與 top 值;同時透過邊界判斷,確保窗口不會被拖出可視范圍之外。 init 函數則負責在頁面加載時初始化各個彈出層,从而实现多窗口獨立拖動的效果。

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