简体   繁体   English

引导弹出窗口的位置,位置和提示

[英]Bootstrap popover position and placement and tip

How do you position the bootstrap popover so that it doesn't float away from the triggering element upon window resize? 如何设置自举弹出窗口,以使其在调整窗口大小时不会从触发元素浮起?

what css needs to be applied to move the tip to the top edge of the popover ie near the corner? 需要使用什么CSS才能将笔尖移动到弹出框的顶部边缘(即靠近拐角处)?

I have the following options set but it places the popover on the left instead of right. 我设置了以下选项,但是它将弹出窗口放置在左侧而不是右侧。

$(function () {
  $('.js-tooltip-trigger').popover({
    html: true,
    trigger: 'focus',
    placement: 'auto right',
    content: function (e) {
        return $(this).parent(".form-group").find(".js-tooltip").html();
    }
  }).on('shown.bs.popover', function () {
       $('.popover').css('top',parseInt($('.popover').css('top')) + 22 + 'px')
     });
});  

The html: 的HTML:

<div class="form-group">
<label for="ref">Reference</label>

<input type="text" class="form-control js-tooltip-trigger" id="ref" maxlength="50" >  

<div class="js-tooltip" style="display: none;">
    <p><strong>Your reference is optional.</strong></p>
    <p>Enter a code of your choice for reference purposes.</p>
</div>
</div>

I'm not a fan of how this works, but essentially you have to create an element that stays in the position you want the popover , and append the popover to it. 我不喜欢它的工作方式,但是从本质上讲,您必须创建一个元素,使其停留在所需的弹出框位置,然后将弹出框附加到该位置。 In this case, I have used the container you already have in place for the tooltip html. 在这种情况下,我使用了已经存在的工具提示html容器。 I'm creating container ID's dynamically so you don't have to worry about doing that in your html. 我正在动态创建容器ID,因此您不必担心在html中这样做。

So for this HTML: 因此,对于此HTML:

<div class="form-group pos-relative">
    <label for="ref">Reference</label>
    <input type="text" class="form-control js-tooltip-trigger" id="ref" maxlength="50">
    <span class="js-tooltip" style="display: none;">
        <p><strong>Your reference is optional.</strong></p>
        <p>Enter a code of your choice for reference purposes.</p>
    </span>
</div>

This CSS: 此CSS:

.pos-relative{
  position:relative;
}
.js-tooltip{
   position:absolute; 
   top:0;
   right:0;
}

And this JS--here's where it happens: 而这个JS-发生的地方是:

$(function () {

  $('.js-tooltip-trigger').each(function(ind, ele){

    var $ele = $(ele),
        $ttSpan = $ele.next('.js-tooltip'),
        ttHtml = $ttSpan.html(),
        rndID = 'ttid'+ String(Math.random()).substr(2);

    // set the ID, strip the style, and empty the html--
    // we already have it stored in the var above
    $ttSpan.attr('id', rndID).removeAttr('style').html('');

    // make the popovers
    $ele.popover({
        html: true,
        trigger: 'focus',
        placement: 'right',
        container: '#'+rndID, 
        content: ttHtml
    });

  });

});

See it in action here 看到它在这里行动

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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