Search

12/25/2007

prototype wrapper for YUI Dom utilities

http://chunghe.googlepages.com/prototype.wrapper.for.YUI.Dom.utilit.htm
用prototype把常用的一些 YUI Dom utilities (getRegion, getXY, getViewportHeight/getViewportWidth, getDocumentScrollTop/getDocumentScrollLeft)重寫一次,還是比較習慣YUI的naming convention,順便熟悉一下在prototype的哪些function mapping到YUI的哪些function。
值得一提的是 document.viewport.getDimensions().width;這個會把viewport width多算'17px',之前在lightbox.js也有遇到一樣的問題,不是很確定,所以用了YUI的code把這部分補起來。另外YUI在IE底下,getXY在X與Y都多算了'2px',prototype算起來倒是正確的。

var PUD = {
getXY: function(ele){
var ele = $(ele);
var pos = ele.positionedOffset();
return [pos.left, pos.top];
},

getRegion: function(ele){
var ele = $(ele);
var pos = ele.positionedOffset();
var t = pos[1];
var r = pos[0] + ele.offsetWidth;
var b = pos[1] + ele.offsetHeight;
var l = pos[0];
return [t, r, b, l];
},

getViewportHeight: function(ele){
return document.viewport.getDimensions().height;
},

getWrongViewportWidth : function(ele){
// belowing method provided by prototype has '17px' margin error
return document.viewport.getDimensions().width;
},

getViewportWidth : function(ele){
// belowing method provided by prototype has '17px' margin error
//return document.viewport.getDimensions().width;
var width = self.innerWidth;
var mode = document.compatMode;

if (mode || Prototype.Browser.IE) { // IE, Gecko, Opera
width = (mode == 'CSS1Compat') ?
document.documentElement.clientWidth : // Standards
document.body.clientWidth; // Quirks
}
return width;
},

getDocumentScrollLeft : function(ele){
return document.viewport.getScrollOffsets()[0];
},

getDocumentScrollTop : function(ele){
return document.viewport.getScrollOffsets()[1];
}
}

沒有留言: