Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is it possible to change the position of Bootstrap popovers based on screen width?

I have a list of items, and each one has a Bootstrap popover associated with it (docs here). They are initiated like this:

$('#my_list li').popover({
   placement: 'left'
});

This works, but at small widths the popover is lost from the viewport. I'd like to make the placement conditional based on $(document).width();, however I can't see a way to over-ride the initial options (e.g. at at width of around 1000px, switch the placement to 'above').

I've put together a simplified version at jsfiddle here. Many thanks.

like image 971
CherryFlavourPez Avatar asked Nov 17 '11 11:11

CherryFlavourPez


People also ask

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. Example: HTML.


1 Answers

placement can also take a function as its value. In this function you can return the appropriate value based on width of viewport. For eg.

$(document).ready(function(){
    $('#my_list li').popover({
        placement: wheretoplace
    });
});
function wheretoplace(){
    var width = window.innerWidth;
    if (width<500) return 'bottom';
    return 'left';
}
like image 92
spicavigo Avatar answered Oct 04 '22 21:10

spicavigo