Skip to content

Instantly share code, notes, and snippets.

Created July 17, 2009 19:29
Show Gist options
  • Save anonymous/149237 to your computer and use it in GitHub Desktop.
Save anonymous/149237 to your computer and use it in GitHub Desktop.
Fx.Scroll.implement({
scrollToCenter: function(el, axes, offset){
axes = axes ? $splat(axes) : ['x', 'y'];
el = $(el);
var to = {},
pos = el.getPosition(this.element),
size = el.getSize(),
scroll = this.element.getScroll(),
containerSize = this.element.getSize(),
edge = {
x: pos.x + size.x,
y: pos.y + size.y
};
['x','y'].each(function(axis){
if(axes.contains(axis)){
if ((edge[axis] > scroll[axis] + containerSize[axis]) || (pos[axis] < scroll[axis])) to[axis] = pos[axis] - (containerSize[axis] - size[axis])/2;
}
if(to[axis] == null) to[axis] = scroll[axis];
if(offset && offset[axis]) to[axis] = to[axis] + offset[axis];
}, this);
if (to.x != scroll.x || to.y != scroll.y) this.start(to.x, to.y);
return this;
}
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment