简体   繁体   中英

How to change Bootstrap popover position dynamically?

Is there any way to change Bootstraps Popover placement/position depending on window width? I'm using it to display some extra form items and need to show if on left site, but on mobile on top. So basically when screens goes smaller - I need to change the position.

jQuery('#monthly-expenses').popover({
    content: jQuery('#monthly-expense-datails'),
    placement() {
        let placement = 'left';

        if (jQuery(window).width() <= 992) {
            placement = 'top';
        }

        return placement;
    },
    html: true,
    trigger: 'manual'
})

You can destroy and recreate the popover when you change the window size:

var placement;
jQuery(window).on('resize',function(){

        if (jQuery(window).width() <= 992 && placement == 'left') {//when the width is right and we did't change placement
           placement = 'top';
           jQuery('#monthly-expenses').popover('dispose');//destroy the 
           createPopover(placement);//create it with the new placement
        } else  if (jQuery(window).width() > 992 && placement == 'top')
           placement = 'left';
           jQuery('#monthly-expenses').popover('dispose');//destroy the popover
           createPopover(placement);//create it with the new placement
        }

});
function createPopover(placement) {
  jQuery('#monthly-expenses').popover({
        content: jQuery('#monthly-expense-datails'),
        placement:placement,
        html: true,
        trigger: 'manual'
   });
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM