Skip to content

Instantly share code, notes, and snippets.

@restlessmedia
Created December 10, 2015 13:48
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save restlessmedia/cfc9d99232ef5f02dcc9 to your computer and use it in GitHub Desktop.
Save restlessmedia/cfc9d99232ef5f02dcc9 to your computer and use it in GitHub Desktop.
Bootstrap tooltip auto placement to prevent tooltip dropping off screen
var autoPlacement = function (tip, element) {
var $document = $(document);
var offset = $(element).offset();
var height = $document.outerHeight();
var width = $document.outerWidth();
var vert = 0.5 * height - offset.top;
var horiz = 0.5 * width - offset.left;
if (Math.abs(horiz) > Math.abs(vert)) {
return horiz > 0 ? 'right' : 'left';
}
return vert > 0 ? 'bottom' : 'top';
};
@restlessmedia
Copy link
Author

$('el').tooltip({
placement: autoPlacement,
trigger: 'hover',
html: true
});

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