首页 文章

Bootstrap弹出位置和位置和提示

提问于
浏览
-1

你如何定位引导弹出窗口,以便在窗口调整大小时它不会从触发元素浮动?

什么css需要应用于将尖端移动到弹出窗口的顶部边缘,即靠近角落?

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

$(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>

1 回答

  • 2

    我不喜欢它是如何工作的,但基本上你必须创建一个保持在你想要弹出窗口的位置的元素,并将弹出窗口附加到它上面 . 在这种情况下,我使用了您已经拥有的工具提示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

相关问题