【问题标题】:How to position bootstrap tooltip correctly next to a partially hidden image如何在部分隐藏的图像旁边正确定位引导工具提示
【发布时间】:2015-05-25 21:18:58
【问题描述】:

我有以下页面使用我无法正确定位的引导工具提示 该页面具有以下特点:

  • #outer-container 是我的页面。它有固定的宽度。
  • 有很多.inner-container。它们具有固定宽度和overflow-x: auto
  • .inner-containers 是动态生成的。 (不确定是否相关,所以下面的示例代码是静态的)
  • .inner-container div 中存在任意HTML,其中可能包含许多<img> 标签
  • 每个<img> 都有一个以alt 文本作为标题的工具提示。
  • 图片可以比.inner-container div 更宽

这里是sn-p,你可以在全页运行sn-p时看到问题。

$("#outer-container").tooltip({
   selector: "img",
   placement: "right",
   title: function () { return $(this).attr("alt"); },
 });
#outer-container
{
  border: 1px solid;
  padding: 10px;
  width: 960px;
  margin-left: auto;
  margin-right: auto;
  overflow: auto;
}

.inner-container
{
  width: 600px;
  overflow-x: auto;
  border: 1px solid;
  margin-bottom : 10px;
  float: right;
}
<link href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.4/css/bootstrap.min.css" rel="stylesheet"/>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.4/js/bootstrap.min.js"></script>

<div id="outer-container">
  <div class="inner-container">
    <figure>
      <img src="http://i.imgur.com/mrXWW8S.png" alt="Man ALL IS LOŚ͖̩͇̗̪̏̈́T ALL I​S LOST the pon̷y he comes he c̶̮omes he comes the ich​or permeates all MY FACE MY FACE ᵒh god no NO NOO̼O​O NΘ stop the an​*̶͑̾̾​̅ͫ͏̙̤g͇̫͛͆̾ͫ̑͆l͖͉̗̩̳̟̍ͫͥͨe̠̅s ͎a̧͈͖r̽̾̈́͒͑e n​ot rè̑ͧ̌aͨl̘̝̙̃ͤ͂̾̆ ZA̡͊͠͝LGΌ ISͮ̂҉̯͈͕̹̘̱ TO͇̹̺ͅƝ̴ȳ̳ TH̘Ë͖́̉ ͠P̯͍̭O̚​N̐Y̡ H̸̡̪̯ͨ͊̽̅̾̎Ȩ̬̩̾͛ͪ̈́̀́͘ ̶̧̨̱̹̭̯ͧ̾ͬC̷̙̲̝͖ͭ̏ͥͮ͟Oͮ͏̮̪̝͍M̲̖͊̒ͪͩͬ̚̚͜Ȇ̴̟̟͙̞ͩ͌͝S̨̥̫͎̭ͯ̿̔̀ͅ" />
      <figcaption>
        Image originally posted by <a href="http://meta.stackexchange.com/questions/213769/work-is-hard-lets-color-the-walls/213975#213975">Travis J</a>
      </figcaption>
    </figure>
  </div>
  
  <div class="inner-container">
    <figure>
      <img src="http://i.stack.imgur.com/CThCe.png" alt="Sanely-sized stackoverflow logo" />
      <figcaption>Sanely-sized stackoverflow logo</figcaption>
    </figure>
  </div>
  
  <div class="inner-container">
    <figure>
      <img src="http://i.stack.imgur.com/MvHsz.jpg" alt="Insanely-long-sized stackoverflow logo" />
      <figcaption>Insanely-long-sized stackoverflow logo</figcaption>
    </figure>
  </div>
</div>

上面的代码可以很好地显示工具提示,但是有一个小问题。 工具提示不会立即出现在被截断的图像之后,而是出现在隐藏的图像部分结束的位置之后,因此它看起来离图像太远了。

当您向右滚动 .inner-container 时,工具提示会越来越靠近 img,直到它靠近它。

当图像比整个屏幕宽时情况会变得更糟,因为现在工具提示太靠右了,现在它不仅会在.inner-container 中生成预期的滚动条,而且还会在整个页面上生成一个滚动条。 现在无法看到工具提示,因为一旦您尝试滚动工具提示就会消失。

现在的问题是.....

有没有办法配置引导工具提示或使用 css 对其进行样式设置,使其始终出现在裁剪图像的边缘,就像在第二张图像中一样,而不是“实际”但隐藏的边缘?此外,当图像比.inner-container 短时,工具提示应该出现在图像旁边而不是容器的右边缘

【问题讨论】:

    标签: css twitter-bootstrap-3 overflow twitter-bootstrap-tooltip


    【解决方案1】:
    #outer-container .tooltip { left: calc(50% + 480px)!important; }
    

    【讨论】:

    • 这在图像比.inner-container 宽时有效,但在图像较窄时无效:工具提示仍然出现在.inner-container 的右边缘,但它应该出现在图像的右边缘
    • 我发现更改 left 属性是可行的方法,但我现在正在用 javascript 计算它
    【解决方案2】:

    感谢@y34h 的想法,但我必须专门通过覆盖实际的引导 js 代码来计算 left 属性的正确值。

    这是新的Tooltip.getCalcultedOffset,它是计算工具提示绝对位置的函数:

        $.fn.tooltip.Constructor.prototype.getCalculatedOffset = function (placement, pos, actualWidth, actualHeight) {
        return placement === 'bottom' ? {
                top: pos.top + pos.height,
                left: pos.left + pos.width / 2 - actualWidth / 2 } :
            placement === 'top' ? {
                top: pos.top - actualHeight,
                left: pos.left + pos.width / 2 - actualWidth / 2 } :
            placement === 'left' ? {
                top: pos.top + pos.height / 2 - actualHeight / 2,
                left: pos.left - actualWidth } :
            /* placement == 'right' */ {
                top: pos.top + pos.height / 2 - actualHeight / 2,
                left:
                    /* begin fix */
                    Math.min(
                        pos.left + pos.width, //original left
                        $(".inner-container").offset().left + $(".inner-container")[0].clientWidth //max left
                    )
                    /* end fix */
            };
    };
    

    实际更改包含在/* begin fix *//* end fix */

    这是完整的工作代码:

    $.fn.tooltip.Constructor.prototype.getCalculatedOffset = function (placement, pos, actualWidth, actualHeight) {
            return placement === 'bottom' ? {
                    top: pos.top + pos.height,
                    left: pos.left + pos.width / 2 - actualWidth / 2 } :
                placement === 'top' ? {
                    top: pos.top - actualHeight,
                    left: pos.left + pos.width / 2 - actualWidth / 2 } :
                placement === 'left' ? {
                    top: pos.top + pos.height / 2 - actualHeight / 2,
                    left: pos.left - actualWidth } :
                /* placement == 'right' */ {
                    top: pos.top + pos.height / 2 - actualHeight / 2,
                    left:
                        /* begin fix */
                        Math.min(
                            pos.left + pos.width, //original left
                            $(".inner-container").offset().left + $(".inner-container")[0].clientWidth //max left
                        )
                        /* end fix */
                };
        };
    
    $("#outer-container").tooltip({
       selector: "img",
       placement: "right",
       title: function () { return $(this).attr("alt"); },
     });
    #outer-container
    {
      border: 1px solid;
      padding: 10px;
      width: 960px;
      margin-left: auto;
      margin-right: auto;
      overflow: auto;
    }
    
    .inner-container
    {
      width: 600px;
      overflow-x: auto;
      border: 1px solid;
      margin-bottom : 10px;
      float: right;
    }
    <link href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.4/css/bootstrap.min.css" rel="stylesheet"/>
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
    <script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.4/js/bootstrap.min.js"></script>
    
    <div id="outer-container">
      <div class="inner-container">
        <figure>
          <img src="http://i.imgur.com/mrXWW8S.png" alt="Man ALL IS LOŚ͖̩͇̗̪̏̈́T ALL I​S LOST the pon̷y he comes he c̶̮omes he comes the ich​or permeates all MY FACE MY FACE ᵒh god no NO NOO̼O​O NΘ stop the an​*̶͑̾̾​̅ͫ͏̙̤g͇̫͛͆̾ͫ̑͆l͖͉̗̩̳̟̍ͫͥͨe̠̅s ͎a̧͈͖r̽̾̈́͒͑e n​ot rè̑ͧ̌aͨl̘̝̙̃ͤ͂̾̆ ZA̡͊͠͝LGΌ ISͮ̂҉̯͈͕̹̘̱ TO͇̹̺ͅƝ̴ȳ̳ TH̘Ë͖́̉ ͠P̯͍̭O̚​N̐Y̡ H̸̡̪̯ͨ͊̽̅̾̎Ȩ̬̩̾͛ͪ̈́̀́͘ ̶̧̨̱̹̭̯ͧ̾ͬC̷̙̲̝͖ͭ̏ͥͮ͟Oͮ͏̮̪̝͍M̲̖͊̒ͪͩͬ̚̚͜Ȇ̴̟̟͙̞ͩ͌͝S̨̥̫͎̭ͯ̿̔̀ͅ" />
          <figcaption>
            Image originally posted by <a href="http://meta.stackexchange.com/questions/213769/work-is-hard-lets-color-the-walls/213975#213975">Travis J</a>
          </figcaption>
        </figure>
      </div>
      
      <div class="inner-container">
        <figure>
          <img src="http://i.stack.imgur.com/CThCe.png" alt="Sanely-sized stackoverflow logo" />
          <figcaption>Sanely-sized stackoverflow logo</figcaption>
        </figure>
      </div>
      
      <div class="inner-container">
        <figure>
          <img src="http://i.stack.imgur.com/MvHsz.jpg" alt="Insanely-long-sized stackoverflow logo" />
          <figcaption>Insanely-long-sized stackoverflow logo</figcaption>
        </figure>
      </div>
    </div>

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2013-03-22
      • 1970-01-01
      • 2013-02-16
      • 2021-02-06
      • 1970-01-01
      • 1970-01-01
      • 2017-04-04
      • 2019-02-28
      相关资源
      最近更新 更多