我正在使用Bootstrap 3设计一个页面。我试图使用一个弹窗的位置:正确的输入元素。新的Bootstrap确保如果你使用表单控件,你基本上有一个全宽的输入元素。

HTML代码看起来像这样:

<div class="row">
    <div class="col-md-6">
        <label for="name">Name:</label>
        <input id="name" class="form-control" type="text" 
                         data-toggle="popover" data-trigger="hover" 
                         data-content="My popover content.My popover content.My popover content.My popover content." />
    </div>
</div>

弹窗宽度太低,在我看来,因为他们没有任何宽度留在div。 我想在左侧输入表单,并在右侧宽弹窗。

大多数情况下,我正在寻找一个解决方案,我不需要覆盖引导。

附上的JsFiddle。第二个输入选项。没有使用jsfiddle很多,所以不知道,但尝试增加输出框的大小来查看结果,在较小的屏幕上甚至看不到它。 http://jsfiddle.net/Rqx8T/


要改变宽度,你可以使用css

需要固定尺寸

.popover{
    width:200px;
    height:250px;    
}

需要的最大宽度:

.popover{
    max-width:200px;
    height:250px;    
}

jsfiddle: http://jsfiddle.net/Rqx8T/2/


<div class="row" data-toggle="popover" data-trigger="hover" 
                 data-content="My popover content.My popover content.My popover content.My popover content.">
<div class="col-md-6">
    <label for="name">Name:</label>
    <input id="name" class="form-control" type="text" />
</div>
</div>

基本上我把弹窗代码放在行div,而不是输入div。解决了这个问题。


我还需要一个更宽的弹出窗口搜索文本字段。我想出了这个Javascript解决方案(在Coffee中):

$(".product-search-trigger")
  .click(-> false) # cancel click on <a> tag
  .popover
    container: "body"
    html: true
    placement: "left"
    title: "<strong>Product search</strong> enter number or name"
  .on("show.bs.popover", -> $(this).data("bs.popover").tip().css(maxWidth: "600px"))

变通方法在最后一行。在弹出窗口显示之前,max-width选项被设置为自定义值。还可以向tip元素添加自定义类。


这里没有最终的解决方案:/只是一些如何“干净地”解决这个问题的想法…

更新版本(jQuery 1.11 + Bootstrap 3.1.1 + class="col-xs-"而不是class="col-md-")的原始JSFiddle: http://jsfiddle.net/tkrotoff/N99h7/

现在使用您提出的解决方案:http://jsfiddle.net/tkrotoff/N99h7/8/ 它不起作用:弹出窗口的定位相对于<div class="col-*"> +想象你有多个输入相同的<div class="col-*">…

所以如果我们想让弹出窗口保持在输入上(语义上更好):

.popover{位置:固定;}:但是当你每次滚动页面时,弹出窗口将不会跟随滚动 .popover{宽度:100%;}:不是很好,因为你仍然依赖于父宽度(即<div class="col-*"> .popover-content {white-space: nowrap;}:仅当弹出窗口内的文本短于max-width时才有效

参见http://jsfiddle.net/tkrotoff/N99h7/11/

也许,使用最近的浏览器,新的CSS宽度值可以解决这个问题,我没有尝试。


使用CSS增加宽度

你可以使用CSS来增加弹出窗口的宽度,如下所示:

/* The max width is dependant on the container (more info below) */
.popover{
    max-width: 100%; /* Max Width of the popover (depending on the container!) */
}

如果这不起作用,您可能需要下面的解决方案并更改容器元素。(查看JSFiddle)


推特引导容器

如果这不起作用,你可能需要指定容器:

// Contain the popover within the body NOT the element it was called in.
$('[data-toggle="popover"]').popover({
    container: 'body'
});

更多信息

弹出窗口包含在触发它的元素中。为了将其扩展为“全宽度”-指定容器:

// Contain the popover within the body NOT the element it was called in.
$('[data-toggle="popover"]').popover({
    container: 'body'
});

JSFiddle

查看JSFiddle来尝试一下。

JSFiddle: http://jsfiddle.net/xp1369g4/


我也有同样的问题。花了很长时间寻找答案,并找到了自己的解决方案: 在头部添加以下文本:

<风格type = " text / css " > .popover { max-width: 600 px; } > < /风格


要改变弹窗宽度,你可以重写模板:

$('#name').popover({
    template: '<div class="popover" role="tooltip" style="width: 500px;"><div class="arrow"></div><h3 class="popover-title"></h3><div class="popover-content"><div class="data-content"></div></div></div>'
})

container: 'body'通常是这样的(参见上面JustAnil的回答),但如果你的弹窗是在一个模态中,就会有一个问题。当弹窗附加到body时,z索引将它放在模态后面。这似乎与BS2 5014问题有关,但我在3.1.1上得到了它。你不需要使用容器的主体,但是如果你将代码修正为

   $('#fubar').popover({
   trigger : 'hover',
   html    : true,
   dataContainer : '.modal-body',
   ...etc });

然后你修复了z-index问题,但弹窗宽度仍然是错误的。

我能找到的唯一解决办法是使用container: 'body',并添加一些额外的css:

.popover { 
  max-width : 400px;
  z-index   : 1060;
}

注意,css解决方案本身是不起作用的。


你还可以添加data-container="body",它可以完成这项工作:

<div class="row">
    <div class="col-md-6">
        <label for="name">Name:</label>
        <input id="name" class="form-control" type="text" 
               data-toggle="popover" data-trigger="hover"
               data-container="body"
               data-content="My popover content.My popover content.My popover content.My popover content." />
    </div>
</div>

您可以使用上述方法调整弹出窗口的宽度,但最好的方法是在Bootstrap看到并进行计算之前定义内容的宽度。例如,我有一个表,我定义了它的宽度,然后bootstrap建立了一个弹窗来适应它。(有时Bootstrap很难确定宽度,你需要介入并握住它的手)


借助@EML上面描述的关于模态窗口弹出窗口的帮助,以及@2called-chaos所显示的代码,这就是我解决问题的方式。

我有一个图标在模式,当点击应该弹出

我的HTML

<i title="" class="glyphicon glyphicon-info-sign" rel="popover" data-title="Several Lines" data-content="A - First line<br />B - Second line - Long line <br />C - Third Line - Long Long Long Line"></i>

我的脚本

$('[rel=popover]').popover({
    placement: 'bottom',
    html: 'true',
    container: '#name-of-modal-window .modal-body'
}).on("show.bs.popover", function () { 
$(this).data("bs.popover").tip().css("max-width", "600px"); });

试试这个:

var popover_size=($('.popover').css('width'));
var string=(popover_size).split('px');
alert("string"+string);
popover_size = ((parseInt(string[0])+350));
alert("ps"+popover_size);
$('.popover').css('width',parseInt(popover_size));

我使用了这个(工作正常):

.popover{
   background-color:#b94a48;
   border:none;
   border-radius:unset;
   min-width:100px;
   width:100%;
   max-width:400px;
   overflow-wrap:break-word;
}

你可以在弹出窗口中使用属性data-container="body"

<i class="fa fa-info-circle" data-container="body" data-toggle="popover"
   data-placement="right" data-trigger="hover" title="Title"
   data-content="Your content"></i>

下面是使用hover的非coffeescript方式:

$(".product-search-trigger").popover({
    trigger: "hover",
    container: "body",
    html: true,
    placement: "left"
  }).on("show.bs.popover", function() {
    return $(this).data("bs.popover").tip().css({
      maxWidth: "300px"
    });
  });
});

Bootstrap 4 beta测试的一个解决方案:

.popover {
        min-width: 30em !important;
    }

和jQuery语句一起使用:

$('[data-toggle="popover"]').popover({
      container: 'body',
      trigger: 'focus',
      html: true,
      placement: 'top'
    })

附注:data-container="body"或container: "body"在HTML中或作为弹窗({})对象的选项都没有真正做到这一点[也许do可以,但只有与CSS语句一起];

另外,请记住Bootstrap 4 beta依赖于popper.js来定位弹出窗口和工具提示(在此之前是tether.js)


对于喜欢JavaScript解决方案的人。 在Bootstrap 4中tip()变成了getTipElement(),它返回一个非jQuery对象。所以为了改变弹出窗口的宽度在Bootstrap 4你可以使用:

}).on("show.bs.popover", function() {
    $($(this).data("bs.popover").getTipElement()).css("max-width", "405px");
});

 <label id="txtLbl">Overview</label> <a tabindex="0" class="btn btn-default" role="button" data-toggle="popover"  data-placement="right" id="Pops" ></a>  
<div id="popover-content" class="hide">
  <div class="popovermenu">
        Your content                   
  </div>
 </div>

我最终通过设置div“popover-content”宽度为我想要的数字。(其他id 或类将无法工作.....)好运!

  #popover-content{
      width: 600px;

  }

对于typescript组件:

@Component({
    selector: "your-component",
    templateUrl: "your-template.component.html",
    styles: [`
        :host >>> .popover {
          max-width: 100%;
        }
    `]
})

在Angular ng-bootstrap中,你可以简单地将container="body"添加到触发弹窗(比如按钮或文本框)的控件中。然后在你的全局样式文件(style.css或style.scss)文件中,你必须添加.popover {max-width: 100% !}。之后,弹出窗口的内容将自动设置为其内容宽度。


您可以更改弹出窗口的模板。可以使用data-template-属性,也可以使用函数中设置它的相关部分:

<html> <head> <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script> <!-- Latest compiled and minified CSS --> <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css" integrity="sha384-BVYiiSIFeK1dGmJRAkycuHAHRg32OmUcww7on3RYdg4Va+PmSTsz/K68vbdEjh4u" crossorigin="anonymous"> <!-- Latest compiled and minified JavaScript --> <script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js" integrity="sha384-Tc5IQib027qvyjSMfHjOMaLkfuWVxZxUPnCJA7l2mCWNIpG9mGCD8wGNIcPD7Txa" crossorigin="anonymous"></script> </head> <body> <span class="text-green" data-toggle="popover" data-placement="right" data-template="<div class=&quot;popover fade top in&quot; style=&quot;max-width:none;&quot; role=&quot;tooltip&quot;><div class=&quot;arrow&quot;></div><h3 class=&quot;popover-title&quot;></h3><div class=&quot;popover-content&quot;></div></div>" data-trigger="manual" data-original-title="Some Title" data-content="<div>Some really long text and stuff. Some really long text and stuff. Some really long text and stuff. Some really long text and stuff.</div>" data-html="true" onclick="$(this).popover('show');">clickme</span> </body> </html>


在bootstrap 4中,你可以简单地在你的样式中覆盖bootstrap变量$popover-max-width的默认值。SCSS文件如下:

$popover-max-width: 300px;

@import "node_modules/bootstrap/scss/functions";
@import "node_modules/bootstrap/scss/variables";
@import "node_modules/bootstrap/scss/mixins";
@import "node_modules/bootstrap/scss/popover";

更多关于覆盖引导4默认值https://getbootstrap.com/docs/4.0/getting-started/theming/#variable-defaults


在Bootstrap 4上,你可以很容易地检查模板选项,通过覆盖max-width:

$('#myButton').popover({
    placement: 'bottom',
    html: true,
    trigger: 'click',
    template: '<div class="popover" style="max-width: 500px;" role="tooltip"><div class="arrow"></div><h3 class="popover-header"></h3><div class="popover-body"></div></div>'
});

如果页面上有多个弹出窗口,这是一个很好的解决方案。


当我想改变一个特定弹出窗口的宽度时,我发现了这个Bootstrap 5+的解决方案。添加customClass对我有用:

Html:

<button class="btn btn-outline-secondary"  id="popover-help" data-bs-container="body" data-bs-toggle="popover" data-bs-placement="right" data-bs-content="Right popover">
   <i class="fa fa-question-circle"></i>
</button>

<div id="popover-help-container" style="display: none;">
    <h4 style="text-align: left;"> How to:</h4>
    <p>Lorem ipsum dolor sit amet, consectetur adipiscing elit. Donec condimentum ultricies elit, mattis egestas nisl suscipit ac. Duis ipsum augue, convallis sed nunc in, pellentesque luctus elit. Duis porttitor massa sed finibus rutrum. Nunc eu risus ultrices, volutpat leo eget, pulvinar eros. Donec rhoncus mattis sem, ac iaculis turpis semper non. Cras dapibus tristique risus, eu tincidunt elit condimentum fermentum. Nunc at dignissim ante, nec efficitur felis. Duis mauris magna, fermentum eu egestas vel, vehicula at est. Nullam dapibus nisi non purus pellentesque ultrices. Nulla pulvinar neque quis ipsum semper, a congue tortor aliquet. Suspendisse vulputate porttitor feugiat. Maecenas pretium porta mauris feugiat pellentesque. Integer tempus risus eu pretium consectetur.

Donec interdum, eros ac imperdiet pretium, magna risus laoreet tellus, in finibus justo sapien et lacus. Pellentesque placerat ligula at metus sollicitudin, et convallis nisi luctus. Vivamus non arcu vel ex pharetra auctor. Morbi efficitur sed turpis id finibus. Sed porttitor luctus quam sed laoreet. Nunc facilisis finibus aliquet. Maecenas pellentesque sem dui, sed blandit tortor placerat ut. Donec tempor ipsum in sem fermentum, vel vulputate magna egestas.</p>
</div> 

JS:

<script type="text/javascript">
    var popoverTriggerList = [].slice.call(document.querySelectorAll('[id="popover-help"]'))
    var popoverList = popoverTriggerList.map(function (popoverTriggerEl) {
    return new bootstrap.Popover(popoverTriggerEl,{
        content: function() {
                return $('#popover-help-container').html();
            },
        html: true,
        placement: 'bottom',
        sanitize: false,
        customClass: 'popover-large'
        });
    })
</script>

css:

.popover-large {
  max-width: 50%; /* Max Width of the popover (depending on the container!) */
}