jquery - Bootstrap popover 位置和位置以及提示

标签 jquery css twitter-bootstrap twitter-bootstrap-tooltip bootstrap-popover

如何定位 Bootstrap 弹出窗口,使其不会在窗口调整大小时漂浮离开触发元素?

需要应用什么 css 才能将提示移动到弹出窗口的顶部边缘,即靠近 Angular 落?

我设置了以下选项,但它会将弹出窗口放在左侧而不是右侧。

$(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')
     });
});  

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>

最佳答案

我不喜欢它是如何工作的,但本质上你必须创建一个元素,它停留在你想要弹出框的位置,并将弹出框附加到它。在本例中,我使用了您已有的用于工具提示 html 的容器。我正在动态创建容器 ID,因此您不必担心在 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>

这个 CSS:

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

而这个 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

关于jquery - Bootstrap popover 位置和位置以及提示,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30508587/

相关文章:

html - 将行 div 中的 3 个 div 与文本居中对齐

html - Img 作为背景图像

html - Bootstrap 不均匀列顺序

css - 拉/推中的 Bootstrap 故障

javascript - jQuery 帮助 - 最小化代码(隐藏/显示 div)并将 "form"重置为默认状态

javascript - 为什么我必须在 IE 中单击两次选择框选项才能使其消失?

javascript - 浏览器无法读取 PHP 生成的 CSS 文件

html - 如何垂直显示 "tagsinput"?

jQuery UI 对话框单独的 CSS 样式

javascript - 使用大量 AngularJS 和 JavaScript 对网站来说是一种不好的做法吗?