DIV CSS 佈局教程網

用 DIV 打造居中、可拖曳的彈出視窗(含完整 CSS 與 JS)
編輯:JQuery特效代碼     

彈出視窗不一定要用 browser 的 alert,也不一定得載入整套 Modal 框架。只用一個 div 加上 CSS 定位與少量 jQuery,就能做出置中顯示、還能用標題欄拖著跑的登入視窗。

這個做法有個附帶好處:內容其實一直存在於 HTML 裡,只是初始狀態隱藏,所以不會像用 JS 動態插入那樣影響搜尋引擎抓取,同時又比整頁跳轉省下大量版面。

DIV 彈出視窗效果

一、先寫出視窗的 HTML 結構

<body>
<div id="login">
  <h2><img src="images/close.png" alt="" class="close" />網站登錄</h2>
  <form id="loginForm">
    <div class="info"></div>
    <div class="user">帳 號:<input type="text" name="user" class="text" /></div>
    <div class="pass">密 碼:<input type="password" name="pass" class="text" /></div>
    <div class="button"><input type="button" name="sub" class="submit" value="" /></div>
  </form>
  <div class="other">注冊新用戶 | 忘記密碼?</div>
</div>
</body>

二、CSS:三個屬性決定成敗

#login {
  width:350px; height:250px;
  border:1px solid #ccc;
  position:absolute;
  display:block;
  z-index:9999;
  background:#fff;
}
#login h2 {
  height:40px; line-height:40px; text-align:center;
  font-size:14px; letter-spacing:1px; color:#666;
  background:url(images/login_header.png) repeat-x;
  margin:0; padding:0;
  border-bottom:1px solid #ccc;
  cursor:move;
}
#login h2 img { float:right; position:relative; top:14px; right:8px; cursor:pointer; }
#login div.info { padding:10px 0 5px 0; text-align:center; color:maroon; }
#login div.user, #login div.pass { font-size:14px; color:#666; padding:5px 0; text-align:center; }
#login input.text { width:200px; height:25px; border:1px solid #ccc; background:#fff; font-size:14px; }
#login .button { text-align:center; padding:15px 0; }
#login input.submit { width:107px; height:30px; background:url(images/login_button.png) no-repeat; border:none; cursor:pointer; }
#login .other { text-align:right; padding:15px 10px; color:#666; }

要浮在頁面之上,position:absolute 是前提;要蓋在所有內容之上,z-index 得給一個夠大的值,這裡用 9999。真正上線時,記得把 display 改回 none,讓它預設隱藏、由程式觸發顯示(這裡為了預覽效果先寫成 block)。

三、讓它乖乖置中

置中的關鍵是「視窗尺寸減去元素尺寸再除以二」,而且必須把捲軸的偏移量加回來,否則頁面捲動後位置就會跑掉。

$(document).ready(function() {
  jQuery.fn.extend({
    center: function(width, height) {
      return $(this)
        .css("left", ($(window).width()  - width)  / 2 + $(window).scrollLeft())
        .css("top",  ($(window).height() - height) / 2 + $(window).scrollTop())
        .css("width", width)
        .css("height", height);
    }
  });
});

$(".login").click(function () {
  $("#login").show().center(350, 250);
});

把 center 掛在 jQuery.fn 上之後,任何元素都能直接呼叫,寫法與原生前後一致,複用性比寫成獨立函式好得多。

視窗置中效果

四、加上拖曳能力

$(document).ready(function() {
  jQuery.fn.extend({
    drag: function(){
      var $tar = $(this);
      return $(this).mousedown(function(e){
        if(e.target.tagName == "H2"){
          var diffX = e.clientX - $tar.offset().left;
          var diffY = e.clientY - $tar.offset().top;
          $(document).mousemove(function(e){
            var left = e.clientX - diffX;
            var top  = e.clientY - diffY;
            if (left < 0){ left = 0; }
            else if (left <= $(window).scrollLeft()){ left = $(window).scrollLeft(); }
            else if (left > $(window).width() + $(window).scrollLeft() - $tar.width()){
              left = $(window).width() + $(window).scrollLeft() - $tar.width();
            }
            if (top < 0){ top = 0; }
            else if (top <= $(window).scrollTop()){ top = $(window).scrollTop(); }
            else if (top > $(window).height() + $(window).scrollTop() - $tar.height()){
              top = $(window).height() + $(window).scrollTop() - $tar.height();
            }
            $tar.css("left", left + 'px').css("top", top + 'px');
          });
        }
        $(document).mouseup(function(){
          $(this).unbind("mousemove");
          $(this).unbind("mouseup");
        });
      });
    }
  });
});

$("#login").drag();

原理很直觀:在標題(H2)上按下滑鼠時,先記下「滑鼠座標減去元素左上角座標」的差值,之後每次移動都用同一個差值反推新位置,視窗 therefore 永遠跟著滑鼠走且不會跳位;放開滑鼠就解绑 mousemove,拖曳結束。

那一段又長又醜的 if 判斷不是裝飾,而是把元素夾在可視範圍內,避免一不小心把視窗拖出畫面再也拉不回來。如果想讓整個視窗都能拖,把 e.target.tagName == "H2" 這個條件拿掉即可,但實務上通常只開放標題欄,這樣才能正常點選輸入框。

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