Skip to content

Instantly share code, notes, and snippets.

@voxpelli
Created December 14, 2011 16:01
Show Gist options
  • Save voxpelli/1477163 to your computer and use it in GitHub Desktop.
Save voxpelli/1477163 to your computer and use it in GitHub Desktop.
Improved autoBounds function for Tipsy.js that takes the size of the tooltip into account.
var autoBoundsImproved = function (margin, prefer) {
return function () {
var dir = {ns: prefer[0], ew: (prefer.length > 1 ? prefer[1] : false)},
$document = $(document),
$window = $(window),
$this = $(this),
$tip = $this.tipsy(true).$tip,
options = $this.tipsy(true).options,
marginTop, marginLeft, boundTop, boundLeft;
marginTop = $tip.outerHeight() + margin;
marginLeft = $tip.outerWidth() + margin;
boundTop = $document.scrollTop() + marginTop;
boundLeft = $document.scrollLeft() + marginLeft;
if ($this.offset().top < boundTop) dir.ns = 'n';
if ($this.offset().left < boundLeft) dir.ew = 'w';
if ($window.width() + $document.scrollLeft() - $this.offset().left < marginLeft) dir.ew = 'e';
if ($window.height() + $document.scrollTop() - $this.offset().top < marginTop) dir.ns = 's';
return dir.ns + (dir.ew ? dir.ew : '');
};
};
@e-motiv
Copy link

e-motiv commented Jan 30, 2014

Just what I needed. I consider the old function really a bug!
P.S. You don't need the options here, so why include them?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment