最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Is it possible to change the position of Bootstrap popovers based on screen width? - Stack Overflow

programmeradmin4浏览0评论

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.

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.

Share Improve this question edited Nov 18, 2011 at 14:44 CherryFlavourPez asked Nov 17, 2011 at 11:08 CherryFlavourPezCherryFlavourPez 7,4976 gold badges47 silver badges48 bronze badges
Add a comment  | 

2 Answers 2

Reset to default 20

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';
}

Its change placement from left to top on window smaller than 768

placement: function(){return $(window).width()>768 ? "auto left":"auto top";}
发布评论

评论列表(0)

  1. 暂无评论