【问题标题】:Angular uib-popover displays in wrong position when set on overflowed text在溢出文本上​​设置时,角度 uib-popover 显示在错误的位置
【发布时间】:2016-09-01 19:18:09
【问题描述】:

我在这里有一个关于我的问题的工作示例: http://plnkr.co/edit/vwZS5e?p=preview

这是问题范围:

<div class="test-container">
    <span uib-popover="Test"
          popover-placement="top"
          popover-trigger="mouseenter"
          popover-append-to-body="true">
        MouseoverMe..MouseoverMe..MouseoverMe..MouseoverMe..MouseoverMe..MouseoverMe..MouseoverMe..MouseoverMe
    </span>
</div>

当我将鼠标悬停在该跨度的中心上方时,我试图显示一个弹出框。当文本太长时,我正在使用文本溢出来切断我的文本。但似乎 uib-popover 不考虑溢出.. 弹出框看起来太靠右了。

这是我的 CSS:

.test-container {
    text-overflow:ellipsis; 
    overflow:hidden; 
    width:100px; 
    border:1px solid red; 
    margin-top:50px; 
    margin-left:50px;
}

我知道我可以将弹出框放在测试容器 div 上,但我希望弹出框位于跨度的中心。

有人知道如何解决这个问题吗?

【问题讨论】:

    标签: javascript css angularjs twitter-bootstrap angular-ui-bootstrap


    【解决方案1】:

    &lt;span&gt; 是一个 inline 元素,widthinline 元素取决于其内容。如果您要添加更多内容,则其宽度会增加,反之亦然。

    在上述情况下,即使没有空格,您也会有很长的文本字符串。如果您检查您的&lt;span&gt;,您会发现&lt;span&gt;width 远大于其父.test-containerwidth

    uib-popover 根据&lt;span&gt;width 占据其位置。如果您增加或减少&lt;span&gt; 元素的内容,您也会看到uib-popover 的位置发生变化。

    您可以通过将&lt;span&gt; 设为block 元素并在其上移动文本剪辑属性来解决此问题。

    (function(){
      'use strict';
    
      angular
        .module('app', ['ui.bootstrap', 'ngAnimate']);
    })();
    
    (function(){
      'use strict';
    
      angular
        .module('app')
        .controller('appController', AppController);
    
      AppController.$inject = ['$log', '$timeout'];
    
      function AppController($log, $timeout) {
        var vm = this;
    
        return vm;
      }
    })();
    html,
    body {
      background-color:darkgray;
    }
    
    .test-container {
      width:100px; 
      border:1px solid red; 
      margin-top:50px; 
      margin-left:50px;
    }
    
    .test-container span {
      text-overflow:ellipsis; 
      overflow:hidden;
      display: block;
    }
    <link data-require="bootstrap@*" data-semver="3.3.5" rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.5/css/bootstrap.min.css" />
      <script data-require="jquery@*" data-semver="2.1.4" src="http://code.jquery.com/jquery-2.1.4.min.js"></script>
      <script data-require="bootstrap@*" data-semver="3.3.5" src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.5/js/bootstrap.min.js"></script>
      <script data-require="angular.js@1.4.6" data-semver="1.4.6" src="https://cdnjs.cloudflare.com/ajax/libs/angular.js/1.4.6/angular.min.js"></script>
      <script src="//ajax.googleapis.com/ajax/libs/angularjs/1.4.7/angular-animate.js"></script>
      <script type="text/javascript" src="//cdnjs.cloudflare.com/ajax/libs/angular-ui-bootstrap/0.14.3/ui-bootstrap-tpls.min.js"></script>
    
    <div ng-app="app" data-ng-controller="appController as vm">
      <div class="test-container">
        <span uib-popover="Test"
              popover-placement="top"
              popover-trigger="mouseenter"
              popover-append-to-body="true">
          MouseoverMe..MouseoverMe..MouseoverMe..MouseoverMe..MouseoverMe..MouseoverMe..MouseoverMe..MouseoverMe
        </span>
      </div>
    </div>

    【讨论】:

    • 哇!我从没想过这个问题会有答案。非常感谢!
    猜你喜欢
    • 1970-01-01
    • 2017-01-14
    • 1970-01-01
    • 2017-01-20
    • 2018-01-16
    • 2013-01-20
    • 1970-01-01
    • 2012-10-06
    • 2022-01-23
    相关资源
    最近更新 更多