Created
December 10, 2015 13:48
-
-
Save restlessmedia/cfc9d99232ef5f02dcc9 to your computer and use it in GitHub Desktop.
Bootstrap tooltip auto placement to prevent tooltip dropping off screen
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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'; | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
$('el').tooltip({
placement: autoPlacement,
trigger: 'hover',
html: true
});