Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Changing the position of Bootstrap popovers based on the popover's X position in relation to window edge?

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!

like image 283
j0e Avatar asked Mar 31 '12 14:03

j0e


People also ask

How do I change the popover position in bootstrap?

Positioning with Margins So to move the popover more to the left, we can add a negative margin-left, or a positive one to move it further to the right. Likewise, we can move the popover more up by adding a negative margin-top, and down by using a positive value.

Which attribute is used to set the position of popover?

Positioning popovers: The data-placement attribute is used to set the positioning of popover elements. The position of popover element can be set to top, bottom, left or right side of the element.

How do I use popovers in bootstrap?

To create a popover, add the data-toggle="popover" attribute to an element. Note: Popovers must be initialized with jQuery: select the specified element and call the popover() method.


1 Answers

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:

var options = {     placement: function (context, source) {         var position = $(source).position();          if (position.left > 515) {             return "left";         }          if (position.left < 515) {             return "right";         }          if (position.top < 110){             return "bottom";         }          return "top";     }     , trigger: "click" }; $(".infopoint").popover(options); 
like image 196
bchhun Avatar answered Sep 22 '22 03:09

bchhun