prototype使用学习手册指南之Position.js
2010-09-14 13:11:45 来源:WEB开发网//将element的position设置为absolute的模式
absolutize: function(element) {
element = $(element);
if (element.style.position == 'absolute’) return;
Position.prepare();
var offsets = Position.positionedOffset(element);
var top = offsets[1];
var left = offsets[0];
var width = element.clientWidth;
var height = element.clientHeight;
element._originalLeft = left - parseFloat(element.style.left || 0);
element._originalTop = top - parseFloat(element.style.top || 0);
element._originalWidth = element.style.width;
element._originalHeight = element.style.height;
element.style.position = 'absolute’;
element.style.top = top + 'px’;;
element.style.left = left + 'px’;;
element.style.width = width + 'px’;;
element.style.height = height + 'px’;;
},
//将element的position设置为absolute的模式
relativize: function(element) {
element = $(element);
if (element.style.position == 'relative’) return;
Position.prepare();
element.style.position = 'relative’;
var top = parseFloat(element.style.top || 0) - (element._originalTop || 0);
var left = parseFloat(element.style.left || 0) - (element._originalLeft || 0);
element.style.top = top + 'px’;
element.style.left = left + 'px’;
element.style.height = element._originalHeight;
element.style.width = element._originalWidth;
}
}
// Safari returns margins on body which is incorrect if the child is absolutely
// positioned. For performance reasons, redefine Position.cumulativeOffset for
// KHTML/WebKit only.
if (/Konqueror|Safari|KHTML/.test(navigator.userAgent)) {
Position.cumulativeOffset = function(element) {
var valueT = 0, valueL = 0;
do {
valueT += element.offsetTop || 0;
valueL += element.offsetLeft || 0;
if (element.offsetParent == document.body)
if (Element.getStyle(element, 'position’) == 'absolute’) break;
element = element.offsetParent;
} while (element);
return [valueL, valueT];
}
}
更多精彩
赞助商链接