0

I'm trying to create a dynamic displacement function for bootstrap popovers, so that if the content is too large to be shown on the screen with "bottom" placement without a vertical scroll, it will show the popover upwards with "top" placement.

For this, I created a function:

function popoverPlacement(popoverHeight, element) {
    var offset = element.offset().top;
    var height = popoverHeight;
    var docheight = $(document).height();

    if (offset + height >= docheight) {
        return "top";
    }
    else {
        return "bottom"
    }
}

Unfortunately, I need to specify the popoverHeight as a hardcoded value, since the content is generated for the popover and not added to the DOM, so I cannot obtain the height it will be.

This is how it is used:

$(this).popover({
            placement: function () { return popoverPlacement(270, this.$element); },
            title: "Status",
            trigger: "click",
            content: editStatusContent,
            html: true,
            delay: {
                show: 1,
                hide: 1
            },
            animation: false
        });

Is there a dynamic way to achieve this?

Thanks

4

2 回答 2

1

您可以通过将内容附加到文档中作为不可见节点来测量您的弹出框,如下所示:

var tpl = this.options.template;
var $inner_el = $(this.options.content).clone(false);
var $measure_el = $('.popover-content', tpl).append($inner_el)
                                            .parents('.popover');
$measure_el.css({
    visibility: 'hidden',
    position:   'absolute'
});

$measure_el.appendTo('body');
actual_height = $measure_el.height();
actual_width = $measure_el.width();
$measure_el.remove();
于 2013-02-28T14:17:08.383 回答
0

我发现您可以在显示工具提示后使用绝对定位:

$('#liveCallData').tooltip('show');
var tooltip = $('#liveCallData .tooltip');
width = tooltip.width();

定位它:

tooltip.css({'position': 'absolute', top: y - 41, left: x - (width / 2) - 1});
于 2013-04-24T18:34:06.613 回答