【问题标题】:How to change Bootstrap popover position dynamically?如何动态更改 Bootstrap 弹出框位置?
【发布时间】:2018-10-31 19:52:18
【问题描述】:
有什么方法可以根据窗口宽度更改Bootstraps Popover 放置/位置?我正在使用它来显示一些额外的表单项,并且需要显示是否在左侧站点上,但在顶部的移动设备上。所以基本上当屏幕变小时 - 我需要改变位置。
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'
})
【问题讨论】:
标签:
jquery
bootstrap-4
popover
popper.js
【解决方案1】:
您可以在更改窗口大小时销毁并重新创建弹出框:
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'
});
}