Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improved version of autoBounds for automatic placement of chunky tips #164

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
39 changes: 39 additions & 0 deletions src/javascripts/jquery.tipsy.js
Original file line number Diff line number Diff line change
Expand Up @@ -254,5 +254,44 @@
return dir.ns + (dir.ew ? dir.ew : '');
}
};

/**
* Improved version of autoBounds for automatic placement of chunky tips
* The original autoBounds failed in two regards: 1. it would never return a 'w' or 'e', gravity even if they
* were preferred and/or optimal, 2. it only respected the margin between the left hand side of an element and
* left hand side of the viewport, and the top of an element and the top of the viewport. This version checks
* to see if the bottom of an element is too close to the bottom of the screen, similarly for the right hand side
*/
$.fn.tipsy.autoBounds2 = function(margin, prefer) {
return function() {
var dir = {},
boundTop = $(document).scrollTop() + margin,
boundLeft = $(document).scrollLeft() + margin,
$this = $(this);

// bi-directional string (ne, se, sw, etc...)
if (prefer.length > 1) {
dir.ns = prefer[0];
dir.ew = prefer[1];
} else {
// single direction string (e, w, n or s)
if (prefer[0] == 'e' || prefer[0] == 'w') {
dir.ew = prefer[0];
} else {
dir.ns = prefer[0];
}
}

if ($this.offset().top < boundTop) dir.ns = 'n';
if ($this.offset().left < boundLeft) dir.ew = 'w';
if ($(window).width() + $(document).scrollLeft() - ($this.offset().left + $this.width()) < margin) dir.ew = 'e';
if ($(window).height() + $(document).scrollTop() - ($this.offset().top + $this.height()) < margin) dir.ns = 's';

if (dir.ns) {
return dir.ns + (dir.ew ? dir.ew : '');
}
return dir.ew;
}
};

})(jQuery);