【问题标题】:How can I position a tooltip with respect to its parent element?如何相对于其父元素定位工具提示?
【发布时间】:2014-04-21 01:06:49
【问题描述】:

我正在尝试制作小型图片库。在 :hover 上,每张图片的左侧都会出现一个工具提示。我的 HTML 代码如下所示:

.gallery {
  right: 247px;
  width: 220px;
}

.gallery li {
  display: inline;
  width: 100px;
  height: 100px;
  position: relative;
}

.gallery img {
  float: right;
  margin: 10px;
  box-shadow: 0 0 0 8px rgba(0, 0, 0, 0.3);
  -webkit-background-clip: padding-box;
  /* for Safari */
  background-clip: padding-box;
  /* for IE9+, Firefox 4+, Opera, Chrome */
}

.tooltip {
  width: 300px;
  height: 100px;
  display: none;
  position: absolute;
  right: -108px;
  top: 222px;
  z-index: 70;
  background-color: rgba(0, 0, 0, 0.4);
  -ms-filter: "progid:DXImageTransform.Microsoft.Alpha(opacity=40)";
}

.gallery li:hover>.tooltip {
  display: inline;
  z-index: 70;
}

.gallery li:hover {
  background: #fff;
}

.gallery li:hover img {
  box-shadow: 0 0 0 8px rgba(255, 255, 255, 1);
}
<ul class="gallery">
  <li>
    <a href="#" class="thumb"><img src="img/pillow.jpg" alt=""></a>
    <div class="tooltip"></div>
  </li>
  <li>
    <a href="#" class="thumb"><img src="" alt=""></a>
    <div class="tooltip"></div>
  </li>
  <li>
    <a href="#" class="thumb"><img src="" alt=""></a>
    <div class="tooltip"></div>
  </li>
  <li>
    <a href="#" class="thumb "><img src="" alt=""></a>
    <div class="tooltip"></div>
  </li>
  <li>
    <a href="#" class="thumb"><img src="" alt=""></a>
    <div class="tooltip"></div>
  </li>
</ul>

如前所述,这个想法是 .tooltip 具有 position:absolute 并且它应该出现在悬停的&lt;li&gt; 的左侧,但不知何故它每次都出现在同一个地方。可能是什么原因?

【问题讨论】:

  • 任何position:absolute; 元素的父元素都需要将其自己的位置声明为静态以外的位置,才能相对于该父元素。尝试将position:relative; 添加到您的.thumb 链接中。
  • 如果我设置位置:相对于锚点(或图像),工具提示会更改它出现的位置,但主要问题仍然存在:工具提示出现在完全相同的位置,无论哪个 li/ img/a 是重点。

标签: html css


【解决方案1】:

试试-

.tooltip {
  display: inline;
  position: relative;
}

.tooltip:hover:after {
  background: #333;
  background: rgba(0, 0, 0, .4);
  border-radius: 5px;
  bottom: 26px;
  color: #fff;
  content: attr(tooltip);
  right: -180px;
  top: 222px;
  padding: 5px 15px;
  position: absolute;
  z-index: 98;
  width: 300px;
  height: 100px;
}
&lt;img src="https://via.placeholder.com/100" tooltip="This is a tooltip" alter="alternative text" /&gt;

【讨论】:

    【解决方案2】:

    您需要将li 设置为position:relative不是aimg,因为li 是实际的祖先;另外两个是兄弟姐妹。

    【讨论】:

    • 其实在原代码中li设置为relative。
    【解决方案3】:
    .gallery li {
        display: inline;
        width: 100px;
        height: 100px;
        position: relative;
    }
    

    这种样式不正确,内联元素没有height,实际上没有width,试试display: inline-block

    另外,您将display: inline 设置为position: absolute 元素,更正确的也是display: block|inline-block

    .gallery img {
        float: right;
        margin: 10px;
        box-shadow: 0 0 0 8px rgba(0, 0, 0, 0.3);
        -webkit-background-clip: padding-box; /* for Safari */
        background-clip: padding-box; /* for IE9+, Firefox 4+, Opera, Chrome */
    }
    

    上面的代码看起来也很奇怪,为什么你需要float: right作为图像,可能你想要li元素的这种风格,但不要使用display: inline or inline-block

    【讨论】:

      【解决方案4】:

      即使您的定位问题得到解决,您的方法也不适用于平板电脑和手机等触摸屏。这个方法,用一点Javascript,也解决了你的问题:

      <!DOCTYPE html>
      <html>
      <head>
          <meta charset="utf-8">
          <title>Light-weight Tooltip by FC</title>
      <style>
      h2 {
          text-align: center;
          margin-bottom: 2em;
      }
      span.tool {
          position: relative;
          display: inline-block;
          border-bottom: 1px dotted black;
      }
      span.tool:hover {
          cursor: help;
      }
      span.tip {
          position: absolute;
          bottom: 20px;
          left: 0px;
          display: block;
          width: auto;
          white-space: nowrap;
          font-size: .9em;
          border: 0px solid black; /* change as desired */
          border-radius: 6px;
          padding: 1px 5px;
          background: #eee;
          background: linear-gradient(top, #eee, #ccc);
          background: -moz-linear-gradient(top, #eee, #ccc);
          background: -o-linear-gradient(top, #eee, #ccc);
          background: -webkit-linear-gradient(top, #eee, #ccc);
          background: -webkit-gradient(linear, left top, left bottom, from(#eee), to(#ccc));
          background: -ms-linear-gradient(top, #eee, #ccc);
          filter: progid:DXImageTransform.Microsoft.Gradient(GradientType=0,StartColorStr=#eeeeee,EndColorStr=#cccccc);
          zoom: 1;
          visibility: hidden;
      }
      </style>
      </head>
      <body>
      
          <h2>Light-weight Tooltip by FC</h2>
      
          <p>The <span class="tool">WHO<span class="tip">World Health Organization</span></span> was established in 1948.</p>
      
          <p>
              It is part of the
              <span class="tool">UN
                  <span class="tip">United Nations, <br>the successor of the <br>League of Nations</span>
              </span>,
              which was established in 1945.
          </p>
      
          <hr>
      
          <p>Explanation and notes:</p>
      
          <ul>
              <li>The method consists of local nested spans ('tools' with 'tips' inside), positioned relative-absolute.</li>
              <li>If the same tips are to be used several times throughout the page or website, the tip spans can be populated centrally with Javascript or server-side scripting.</li>
              <li>In the current code the width of the tips is set to <i>auto</i>, and controlled with &lt;br&gt;s in the tip text. Change to fixed width as desired.</li>
              <li>With the current code, tablet users must tap (= <i>onclick</i>) rather than press-hold (= <i>onmousedown</i>). It is assumed that that is the intuitive thing most tablet users do, but it can be changed to press-hold.</li>
              <li>The HTML is valid and the code works in IE8+ as well.</li>
              <li>For the sake of completeness: IE9 does not form a border-radius when the element has no declared border, or a border-width of 0.</li>
          </ul>
      
      <script>
          var tools = document.querySelectorAll('.tool'); // mind the dot
          var nrOfTools = tools.length;
          for (var i=0; i<nrOfTools; i++) {
              if ('ontouchstart' in window || (window.DocumentTouch && document instanceof DocumentTouch)) {
                  tools[i].onclick = function() {
                      if (this.children[0].style.visibility == '' || this.children[0].style.visibility == 'hidden')
                          this.children[0].style.visibility = 'visible';
                      else
                          this.children[0].style.visibility = 'hidden';
                  }
              }
              else {
                  tools[i].onmouseover = function() {
                      this.children[0].style.visibility = 'visible';
                  }
                  tools[i].onmouseout = function() {
                      this.children[0].style.visibility = 'hidden';
                  }
              }
          }
      </script>
      </body>
      </html>
      

      http://jsbin.com/nerul/6/edit?html,output 上的现场演示。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2013-11-10
        • 2014-11-12
        • 1970-01-01
        • 1970-01-01
        • 2012-06-13
        • 2016-10-03
        • 2015-12-21
        • 1970-01-01
        相关资源
        最近更新 更多