【问题标题】:How to show an image in a tool-tip on hovering over another image using CSS only?如何在仅使用 CSS 将鼠标悬停在另一个图像上的工具提示中显示图像?
【发布时间】:2015-03-17 11:11:14
【问题描述】:

我有一组三个水平图像。现在,当用户将鼠标光标悬停在任何其他三个图像上时,我必须显示每个图像的工具提示图像。

问题在于我想显示图像的每个工具提示。我只能通过 CSS 来实现这件事。没有更多的 jQuery 和其他东西。

有人可以在这方面帮助我吗?

以下是三张水平图像的 HTML。

<table style="width:100%" id="thumbs">
                      <tr>
                        <td><span style="cursor:pointer" ><img id="img1" width="80" height="80" src="http://78.134.51.289/theme/frontend/foxplus/style/default/image/layout/OpenIcon.png"/></span></td> 
                        <td><span style="cursor:pointer" ><img id="img2" width="80" height="80" src="http://78.134.51.289/theme/frontend/foxplus/style/default/image/layout/ColsedIcon.png"/></span> </td>
                        <td><span style="cursor:pointer" ><img id="img3" width="80" height="80" src="http://78.134.51.289/theme/frontend/foxplus/style/default/image/layout/SecretIcon.png"/></span> </td>
                      </tr>
                    </table>

提前致谢。您可以将任何.png 格式且尺寸为273px * 224 px 的图像作为工具提示

【问题讨论】:

  • 这样的? Fiddle
  • 不会欺骗我的答案,但我认为第一个 sn-p here 可以根据您的需要进行更改。 (其中 cat 1-cat 4 改为图像)
  • @Mr_Green:是的,您确实遇到了我的问题。但我想为三个不同的图像显示三个不同的工具提示图像。在您的小提琴中,相同的图像显示为每个图像的工具提示。你能相应地修改你的小提琴吗?非常感谢您对我的问题表现出兴趣并帮助我。
  • @user2839497 你只需要覆盖这个Fiddle

标签: html image css tooltip


【解决方案1】:

您可以使用:after:before 伪类元素来实现。

tr span {
    display: block;
    position: relative;
}
tr span:hover {
    z-index: 1;
}
tr td span:hover:after {
    content:"";
    background-image: url('http://lorempixel.com/273/274/animals');
    position: absolute;
    top:50%;
    left: 50%;
    width: 273px;
    height: 274px;
}
tr td + td span:hover:after {
    background-image: url('http://lorempixel.com/273/274/sports');
}
tr td + td + td span:hover:after {
    background-image: url('http://lorempixel.com/273/274/people');
}

Working Fiddle

【讨论】:

  • 非常感谢您的大力帮助。现在只是一种要求。如果您仔细观察,最左侧图像的工具提示图像没有完全显示。它在屏幕的右边缘内移动。因此,我想在位于中心的图像下方显示每个工具提示图像,以便在悬停相应图像时,所有工具提示图像都将显示在屏幕的中心。此外,所有工具提示图像部分都将完全显示。还将工具提示图像的高度从 274 更改为 224。如果您可以对小提琴进行此更改,那就太好了。
  • @user2839497 你只需要使用topleft css 属性。这是fiddle..请做这些小改动..
  • :谢谢。但我仍然在三个不同的位置看到三个工具提示图像。我想在三个水平图像行中的第二个图像下方悬停时显示相应的工具提示图像。在您提供的新小提琴中,这没有发生,我尝试使用左右值,但我得到的结果相同。您能否对您创建的小提琴进行此更改?再次感谢您所做的努力。
【解决方案2】:

您是要显示工具提示,还是将其用作图片库?无论哪种方式;

工具提示:

.image {
  display: inline-block;
  height: 150px;
  width: 200px;
  background: gray;
  position: relative;
  overflow:hidden;

}
.image img {
  height: 100%;
  width: 100%;
}
.image .tooltip {
  position: absolute;
  transform: translate(100%);
  bottom: 0;
  width: 100%;
  color: white;
  background: rgba(0, 0, 0, 0.5);
  transition: all 0.8s;
  opacity:0;
}
.image:hover .tooltip {
  position: absolute;
  transform: translate(0);
  opacity:1;
}
.tooltip:hover ~ .this{
  width:300px;
  }
<div class="image">
  <img src="http://placekitten.com/g/300/300" />
  <div class="tooltip">
    i'm a tool tip.
  </div>
</div>

画廊:

#gallery {
  width: 300px;
  border: 3px solid red;
  text-align: center;
  padding-top: 300px;
  background: url(http://placekitten.com/g/300/300) no-repeat;
  position: relative;
}
#gallery li {
  display: inline;
}
#gallery span img {
  padding: 0 1em;
  transition:all 0.8s;
}
#gallery img {
  display: none;
  position: absolute;
  top: 0;
  left: 0;
  height: 300px;
  width: 300px;
}
#gallery span:hover {
  background: red;
}
#gallery span:hover ~ img {
  display: inline-block;
}
<ul id="gallery">
  <li>
    <span>Cat 1</span>
    <img src="http://placekitten.com/g/200/200" />
  </li>
  <li>
    <span>Cat 2</span>
    <img src="http://placekitten.com/g/300/200" />
  </li>
  <li>
    <span>Cat 3</span>
    <img src="http://placekitten.com/g/110/100" />
  </li>
  <li>
    <span>Cat 4</span>
    <img src="http://placekitten.com/g/140/100" />
  </li>
</ul>

【讨论】:

    【解决方案3】:

    .ImgWithTool {
      position: relative;
      width: 200px;
      height: 200px;
    }
    .tool {
      display: none;
      width: 100%;
      height: 50px;
      position: absolute;
      bottom: 0;
      text-align: center;
      background: #ddd;
    }
    .ImgWithTool:hover .tool {
      display: block;
    }
    .image-icon {
      width: 50px;
      height: 50px;
      background: #ccc;
      display: inline-block;
    }
    .image-icon:hover {
      background: #eee;
    }
    <td>
      <div class="ImgWithTool">
        <img id="img1" width="200px" height="200px" alt="your img" src="" />
        <div class="tool">
          <div class="image-icon">tool1</div>
          <div class="image-icon">tool2</div>
        </div>
      </div>
    </td>

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2021-03-25
      • 2014-12-31
      • 1970-01-01
      • 1970-01-01
      • 2015-12-07
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多