MozTW 討論區

各項 Mozilla 相關軟體與技術討論
現在的時間是 2025-06-30, 22:10

所有顯示的時間為 UTC + 8 小時





發表新文章 回覆主題  [ 4 篇文章 ] 
發表人 內容
文章發表於 : 2004-12-26, 17:53 
離線
頭像

註冊時間: 2004-11-30, 10:46
文章: 101
常看到討論中提到有關W3C標準
版子中似乎這類文章少了
因此,提出自己的版本來供各位參考指正
以下的程式碼

首先定義版本參數

代碼:
ns4    = (document.layers);
ns6    = ((navigator.appName == "Netscape") && (parseInt(navigator.appVersion) >=5))
ie4    = (document.all);
ie5      = ((parseInt(navigator.appVersion) == 4) && (navigator.appName.indexOf("msie 5.0") != -1));
ie55     = ((parseInt(navigator.appVersion) >= 4) && (navigator.appName.indexOf("msie 5.5") != -1));
ie6     = (parseInt(navigator.appVersion) >= 4);
ver4    = (ns4 || ie4);
mac45    = (navigator.appVersion.indexOf("MSIE 4.5") != -1)
isMac    = (navigator.appVersion.indexOf("Mac") != -1);


接下來,定義一個名為init()的初值化函式

代碼:
var layerRef="null",layerStyleRef="null",styleSwitch="null",nostyleSwitch="null";
var widthClipSwitch="null";
var heightClipSwitch="null";
var bWidth,bHeight,offsetX,offsetY;
var layerTop,layerLeft;
var pxSwtch;
var topStop;

function init(){
   
   if (ns4) {
      layerStyleRef="layer.";
      layerRef="document.layers[";
      styleSwitch="]";
      pxSwitch="";
      layerTop=".top";
      layerLeft=".left";
      widthClipSwitch="].clip.right";
      heightClipSwitch="].clip.bottom";
      offsetX= "window.pageXOffset";
      offsetY= "window.pageYOffset";
      bHeight=window.innerHeight;
      bWidth= window.innerWidth;
      ns4=true;
   }
   if (ie4){ // mean ie4,ie5,ie5.5 or above
      layerStyleRef="layer.style.";
      layerRef="document.all[";
      styleSwitch="].style";
      nostyleSwitch="]";
      pxSwitch='px';
      layerTop=".pixelTop";
      layerLeft=".pixelLeft";
      widthClipSwitch=".clientWidth";
      heightClipSwitch=".clientHeight";
      offsetX= "document.body.scrollLeft";
        offsetY= "document.body.scrollTop";
        bHeight=document.body.clientHeight;
        bWidth= document.body.clientWidth;
        ie=true;
   }
   
   if (ns6){
      layerStyleRef="layer.style.";
      layerRef="document.getElementById(";
      styleSwitch=").style";
      nostyleSwitch=")";
      pxSwitch='px';
      layerTop=".top";
      layerLeft=".left";
      widthClipSwitch=".offsetWidth";
      heightClipSwitch=".offsetHeight";
      offsetX= "window.pageXOffset";
      offsetY= "window.pageYOffset";
      bHeight=window.innerHeight;
      bWidth= window.innerWidth;
        ns6=true;
   }
}


使用的方法,在BODY標籤裡加入

onload="init();"



代碼:
<body onload="init();">


其他的函式可以自行設計, 舉例如下

代碼:
function showLayer(layerName){
   eval(layerRef+'"'+layerName+'"'+styleSwitch+'.visibility="visible"');
}
   
function hideLayer(layerName){
   eval(layerRef+'"'+layerName+'"'+styleSwitch+'.visibility="hidden"');
}

function moveLayerto(layerName,top,left){
   eval(layerRef+'"'+layerName+'"'+styleSwitch+'.top=top');
   eval(layerRef+'"'+layerName+'"'+styleSwitch+'.left=left');
}

這些函式分別是顯示圖層,隱藏圖層,把圖層移到指定位置

使用時,例如先宣告一個圖層

代碼:
<div id="layerName" style="............">
...........
,/div>


而要隱藏時,即可使用(以按鈕為觸動例)

代碼:
<input type="button" value="Hide Layer" onclick="hideLayer('layerName');">


以上程式碼為本人自行撰寫,歡迎取用與修正及隨便散佈
更歡迎提出改進建議及討論

_________________
圖檔圖檔圖檔
我們原本就會的,因遺忘而要重頭學習
我的網誌 http://audi.tw


回頂端
Mozilla/5.0 (Windows; U; Windows NT 5.0; zh-TW; rv:1.7.5) Gecko/20041119 Firefox/1.0
 個人資料  
引用回覆  
 文章主題 :
文章發表於 : 2004-12-26, 23:39 
離線

註冊時間: 2004-06-23, 12:28
文章: 232
很不錯的 JavaScript 範例.


回頂端
Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.7.2) Gecko/20040804 Netscape/7.2 (ax)
 個人資料  
引用回覆  
 文章主題 :
文章發表於 : 2004-12-27, 12:21 
代碼:
<script>
function toggleLayer(aID) {
var t = document.getElementById(aID).style;
t.visibility = (t.visibility != 'hidden') ? 'hidden' : 'visible';
}
</script>
<input onclick="toggleLayer('aLayer')" type="button" value="test" />
<div id="aLayer" style="background: lime; height: 100px; width: 100px;"></div>


亂寫的, 但比較接近W3C標準的形式...


回頂端
Mozilla/5.0 (Windows; U; Windows NT 5.0; rv:1.7.3) Gecko/20041001 Firefox/0.10.1
  
引用回覆  
 文章主題 :
文章發表於 : 2004-12-27, 12:43 
離線
頭像

註冊時間: 2004-11-30, 10:46
文章: 101
Anonymous 寫:
代碼:
<script>
function toggleLayer(aID) {
var t = document.getElementById(aID).style;
t.visibility = (t.visibility != 'hidden') ? 'hidden' : 'visible';
}
</script>
<input onclick="toggleLayer('aLayer')" type="button" value="test" />
<div id="aLayer" style="background: lime; height: 100px; width: 100px;"></div>


亂寫的, 但比較接近W3C標準的形式...


兩種型式

第一種, 例如我的例子

代碼:
document.getElementById(layerName).style.property=


其中 property 是屬性, 例如 visibility

第二種, 例如您的例子
代碼:
var obj=document.getElementById(layerName).style;


使用時則為
代碼:
obj.property

obj.method


這兩者都可行
看開發人員接受的程度
如果物件導向概念夠的人
我建議採您的模式, 開發上較順利
但對於初探 DOM 模式者
可參考我的例子, 或許比較容易懂些

感謝您的參與討論

_________________
圖檔圖檔圖檔
我們原本就會的,因遺忘而要重頭學習
我的網誌 http://audi.tw


回頂端
Mozilla/5.0 (Windows; U; Windows NT 5.0; zh-TW; rv:1.7.5) Gecko/20041119 Firefox/1.0
 個人資料  
引用回覆  
顯示文章 :  排序  
發表新文章 回覆主題  [ 4 篇文章 ] 

所有顯示的時間為 UTC + 8 小時


誰在線上

正在瀏覽這個版面的使用者:沒有註冊會員 和 5 位訪客


不能 在這個版面發表主題
不能 在這個版面回覆主題
不能 在這個版面編輯您的文章
不能 在這個版面刪除您的文章
不能 在這個版面上傳附加檔案

搜尋:
前往 :  
Powered by phpBB® Forum Software © phpBB Group
正體中文語系由 竹貓星球 維護製作
© moztw.org, Mozilla Foundation
MozTW,Mozilla 台灣社群