如何定位 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
});
});
});
关于jquery - Bootstrap popover 位置和位置以及提示,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30508587/