好的,我想通了。我会在这里发布它,以便帮助其他人。这个解决方案使用原型和一个内部库,它为我提供了registerEvent,getElementX和getElementY函数,这些函数按照您的想法进行。
var MenuManager = Class.create({
initialize: function initialize(menuElt) {
this.menu = $(menuElt);
this.homePosn = { x: getElementX(this.menu), y: getElementY(this.menu) };
registerEvent(document, 'scroll', this.handleScroll.bind(this));
this.handleScroll();
},
handleScroll: function handleScroll() {
this.scrollOffset = document.viewport.getScrollOffsets().top;
if (this.scrollOffset > this.homePosn.y) {
this.menu.style.position = 'fixed';
this.menu.style.top = 0;
this.menu.style.left = this.homePosn.x;
} else {
this.menu.style.position = 'absolute';
this.menu.style.top = null;
this.menu.style.left = null;
}
}
});
只需调用带有菜单ID的构造函数,然后该类就会从中取出它。
边注:有些人恨菜单。只要看看摆脱它的所有Greasemonkey脚本。 – 2009-01-24 17:11:31