// usage:
// fixedPosition(elem, {top:0, left:0});
// fixedPosition(elem, {bottom:0, right:0});
var fixedPosition = function(){
var html = document.getElementsByTagName('html')[0],
dd = document.documentElement,
db = document.body,
doc = dd || db;
// 给IE6 fixed 提供一个"不抖动的环境"
// 只需要 html 与 body 标签其一使用背景静止定位即可让IE6下滚动条拖动元素也不会抖动
// 注意:IE6如果 body 已经设置了背景图像静止定位后还给 html 标签设置会让 body 设置的背景静止(fixed)失效
if (isIE6 && db.currentStyle.backgroundAttachment !== 'fixed') {
html.style.backgroundImage = 'url(about:blank)';
html.style.backgroundAttachment = 'fixed';
};
// pos = {top:0, right:0, bottom:0, left:0}
return isIE6 ?
function(elem, pos){
var style = elem.style,
dom = '(document.documentElement || document.body)';
if(typeof pos.left !== 'number'){
pos.left = doc.clientWidth - pos.right - elem.offsetWidth;
}
if(typeof pos.top !== 'number'){
pos.top = doc.clientHeight - pos.bottom - elem.offsetHeight;
}
elem.style.position = 'absolute';
style.removeExpression('left');
style.removeExpression('top');
style.setExpression('left', 'eval(' + dom + '.scrollLeft + ' + pos.left + ') + "px"');
style.setExpression('top', 'eval(' + dom + '.scrollTop + ' + pos.top + ') + "px"');
} :
function(elem, pos){
var style = elem.style;
style.position = 'fixed';
if(typeof pos.left === 'number'){
style.left = pos.left + 'px';
}else{
style.left = 'auto';
style.right = pos.right + 'px';
}
if(typeof pos.top === 'number'){
style.top = pos.top + 'px';
}else{
style.top = 'auto';
style.bottom = pos.bottom + 'px';
}
};
}();