如何针对其父元素定位工具提示?

时间:2014-03-15 17:14:03

标签: html css

我正在尝试制作小型图库。在:悬停时,应在每张图片的左侧显示工具提示。我的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的位置是绝对的,它应该出现在悬停的<li>左侧,但不知何故它每次出现在同一个地方。可能是什么原因?

4 个答案:

答案 0 :(得分:0)

尝试 -

   <style>
 .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;

}        

然后

    <img src="#" tooltip="This is a tooltip" alter="alternative text" />

答案 1 :(得分:0)

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

答案 2 :(得分:0)

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元素使用这种样式,但是却不使用{{1}}

答案 3 :(得分:0)

即使解决了定位问题,您的方法也无法在平板电脑和手机等触摸屏上使用。此方法确实需要一些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上的实时演示。