Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save GCheung55/154706 to your computer and use it in GitHub Desktop.
Save GCheung55/154706 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)){
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