I have scoured the Internet far and wide and while I found this stackoverflow post insightful Is it possible to change the position of Bootstrap popovers based on screen width?, it still didn't answer my question, likely due to my trouble understanding position/offset.
Here's what I'm trying to do. I want the Twitter Bootstap Popover position to be RIGHT unless the hotspot popover will be positioned beyond the viewport, then I want it to be positioned LEFT. I'm making an iPad web app that has hot spots, when you push on a hotspot, information appears but I don't want it to appear outside of the viewport when horizontal scrolling is locked.
I'm thinking it'd go something like this:
$('.infopoint').popover({
trigger:'hover',
animation: false,
placement: wheretoplace
});
function wheretoplace(){
var myLeft = $(this).offset.left;
if (myLeft<500) return 'left';
return 'right';
}
Thoughts? Thanks!
You can also try this one,
==> Get target/source position in viewport
==> Check whether space from bottom is less that your tooltip height
==> If space from bottom is less that your tooltip height, then just scroll page up
I just noticed that the placement option could either be a string or a function returning a string that makes the calculation each time you click on a popover-able link.
This makes it real easy to replicate what you did without the initial $.each function:
For Bootstrap 3, there is
which is easier. By default, it will be right, but if the element is located in the right side of the screen, the popover will be left. So it should be:
So I figured out a way to do this effectively. For my particular project I'm building an iPad website so I knew exactly what the pixel X/Y value would be to reach the edge of the screen. Your thisX and thisY values will vary.
Since the popovers are being placed by inline styling anyway, I simply grab the left and top values for each link to decide which direction the popover should be. This is done by appending html5 data- values that .popover() uses upon execution.
Any comments/improvements are welcome but this gets the job done if you're willing to put the values in manually.
You can also try this one if u need it in almost all places in the page.
U can configure it in a general way then just use it.
In this way u can also use HTML element and anything u want :)
Addition
Update
If u want the popup after click, u can change the JS option to
trigger: "click"
like this-U also can customize it in HTML ading
data-trigger="click"
like this-I think it will be more oriented code and more re-usable and more helpful to all :).
bchhun's answer got me on the right track, but I wanted to check for actual space available between the source and the viewport edge. I also wanted to respect the data-placement attribute as a preference with appropriate fallbacks if there wasn't enough space. That way "right" would always go right unless there wasn't enough space for the popover to show on the right side, for example. This was the way I handled it. It works for me, but it feels a bit cumbersome. If anyone has any ideas for a cleaner, more concise solution, I'd be interested to see it.