DIV CSS 佈局教程網

jQuery 彈出層插件 $-layer 簡化版:原始碼與用法
編輯:JQuery特效代碼     

$-layer 是早期相當流行的一款輕量彈出層插件,作者 King Wong 於 2008 年釋出 0.1 版。這份「簡化版」拿掉了跨框架與外部 CSS 載入的能力,換來的是更精簡的檔案與更容易閱讀的結構,很適合拿來當作學習拖曳實作的教材。

核心設計

整個插件由幾個私有變數撐起來:___id___ 記住目前操作的彈出層 ID、___settings___ 保存每個彈出層的設定、dragStatus 標記拖曳狀態,而 dragCallbacks 與 dropCallbacks 則是使用者自訂的拖曳與放開回呼。

座標與拖曳

$.getMousePosition = function(e){
  var posx = 0, posy = 0;
  if (!e) var e = window.event;
  if (e.pageX || e.pageY) {
    posx = e.pageX; posy = e.pageY;
  } else if (e.clientX || e.clientY) {
    posx = e.clientX + document.body.scrollLeft + document.documentElement.scrollLeft;
    posy = e.clientY + document.body.scrollTop + document.documentElement.scrollTop;
  }
  return { 'x': posx, 'y': posy };
};

$.updatePosition = function(e) {
  var pos = $.getMousePosition(e);
  var spanX = (pos.x - lastMouseX);
  var spanY = (pos.y - lastMouseY);
  var _top  = (lastElemTop  + spanY) > 0 ? (lastElemTop  + spanY) : 0;
  var _left = (lastElemLeft + spanX) > 0 ? (lastElemLeft + spanX) : 0;
  $("#"+___id___).css("top", _top);
  $("#"+___id___).css("left", _left);
};

這裡的做法是「位移量累加」而非直接跳到滑鼠位置:按下時先記住滑鼠座標與元素當前的 offsetTop/offsetLeft,移動時計算差值再加回去,並用三元運算把上下界鎖在 0,避免視窗被拖出畫面。

對外開放的拖曳控制

$.fn.ondrag  = function(callback){ return this.each(function(){ dragCallbacks[this.id] = callback; }); };
$.fn.ondrop  = function(callback){ return this.each(function(){ dropCallbacks[this.id] = callback; }); };
$.fn.dragOff = function(){ return this.each(function(){ dragStatus[this.id] = 'off'; }); };
$.fn.dragOn  = function(){ return this.each(function(){ dragStatus[this.id] = 'on';  }); };

預設設定與模板

$.extend({
  layerSettings:{
    id:"layerdiv",
    width:220,
    height:220,
    templete:'<div style="height:20px; width:@width@px; background-color:#777777;">
      <span id="@moveid@" style="position:relative; height:20px; width:100px;">
        <span id="@titleid@">@title@</span>
      </span>
      <span class="layerclose" style="float:right; right:20px; color:red;">close</span>
    </div>
    <div style="border:solid #ff0000 1px; width:@width@px; height:@height@px;">
      <div style="width:100%; height:100%; background-color:#ffffff;" id="@contentid@"></div>
    </div>',
    content:'',
    title:'',
    isbg:true,
    opacity:0.3
  },
  layerSetup: function( settings ) {
    $.extend( $.layerSettings, settings );
    ___settings___[settings.id] = settings;
    ___id___ = settings.id;
  },
  layershow: function(){ /* 見下 */ },
  layerclose: function(__id){
    $("#"+__id+"_background").remove();
    $("#"+__id).remove();
  },
  layermaxindex: function(){
    var ___index = 0;
    $.each($("*"), function(i,n){
      var __tem = $(n).css("z-index");
      if(__tem > 0 && __tem > ___index){ ___index = __tem + 1; }
    });
    return ___index;
  }
});

模板裡的 @width@、@height@、@titleid@、@moveid@、@contentid@ 都是佔位符,靠自製的 replaceAll 逐一替換。因為 JavaScript 原生的 replace 只換第一個,作者直接在 String.prototype 上補了一個全域版本:

String.prototype.replaceAll = function(s1,s2){
  return this.replace(new RegExp(s1,"gm"), s2);
};

顯示流程

layershow() 依序完成:計算視窗寬高並決定水平居中位置 → 用 layermaxindex() 取得最高 z-index 疊上去 → 視需要插入半透明黑色遮罩 → 附加彈出層容器並套用模板 → 綁定關閉鈕、點擊提層,以及 mousemove/mouseup 的拖曳收尾。

使用方式

顯示彈出層

function show(){
  $.layerSetup({
    id:"abc",              // 彈出層的 ID
    title:"test",          // 標題
    content:'test',        // 內容
    isbg:false,            // 是否顯示遮罩層
    opacity:0.3,           // 遮罩透明度,0 全透明、1 不透明
    templete:'<div class="showwint_mini_title">
      <span class="showwint_mini_close_btn">
        <a href="javascript:void(null);" class="layerclose"></a>
      </span>
      <span class="showwint_mini_title_content" id="@moveid@">
        <span id="@titleid@"></span>
      </span>
    </div>
    <div class="showwint_mini_content">
      <div class="showwint_mini_content_content" id="@contentid@"></div>
    </div>'
  });
  $.layershow();
}

關閉彈出層

$.layerclose("彈出層的ID");

注意事項

如果你的模板搭配了外部 CSS 檔,請自行在頁面中 <link> 引入。這份簡化版刻意移除了 cssurl 與 target 兩個屬性,因此無法跨 iframe 或跨框架彈出,只能在當前文件內運作。

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