简体   繁体   English

在悬停时以td显示图像

[英]Display image in td on hover

Here is what I am trying to achieve (not sure if it's even possible): 这是我想要实现的目标(不确定是否可行):

Hover the mouse over a td, and in the bottom right corner of that td (perhaps overlapping the cell border), show a small image. 将鼠标悬停在td上方,并在该td的右下角(可能与单元格边界重叠)显示一个小图像。

I'm okay with jQuery, so I can handle the hovering, but the CSS kills me. 我对jQuery没问题,所以我可以处理悬停,但是CSS杀死了我。 How do I position the image? 如何定位图像? Would I have the image in every cell, and only show it on hover? 我会在每个单元格中都有图像,并且只在悬停时显示它吗? Or is there a better way? 或者,还有更好的方法?

I'm on IE7. 我在IE7上。 I appreciate any guidance. 我感谢任何指导。

To avoid putting an image into every single td in the page you could also use a background image: 为了避免将图像放入页面的每个td中,您还可以使用背景图像:

td{
  background-image: url('/yourImage.png');
  background-position: -2000px -2000px;
}

td:hover{
  background-position: right bottom;
}

The reasoning for using the initial offset is that it makes sure that the image is pre-loaded with the page and so there's no lag during the first mouseover. 使用初始偏移量的原因是,它可以确保图像已随页面预加载,因此在第一次鼠标悬停时没有滞后。

Browser support issues 浏览器支持问题

I'm not sure how well supported the positioning directions are for different browsers and you may also want to check out this article to make sure that the :hover pseudo class works correctly for your target browsers. 我不确定不同浏览器的定位说明是否得到了很好的支持,您可能还想查看一下本文,以确保:hover伪类对您的目标浏览器正确运行。 IE7+ will support the :hover pseudo class but they need the correct doctype (and a good prevailing wind) http://www.bernzilla.com/item.php?id=762 IE7 +将支持:hover伪类,但它们需要正确的doctype(和流行的风) http://www.bernzilla.com/item.php?id=762

Using jQuery instead 改用jQuery

If you don't want to use :hover you can stick to jquery to add and remove a class. 如果您不想使用:hover,则可以坚持使用jquery添加和删除类。 The equivalent CSS for that would be: 等效的CSS为:

td{
  background-image: url('/yourImage.png');
  background-position: -2000px -2000px;
}

td.hoverclass{
  background-position: right bottom;
}

where ".hoverclass" is the classname that you add to the td during the mouseover period. 其中“ .hoverclass”是您在鼠标悬停期间添加到td的类名。

Something along the lines of this will show and hide images in a td. 与此类似的东西将在td中显示和隐藏图像。

td img
{
 display:none;
}

td:hover img
{
 display:block;
}

To do the positioning, what you want to use is absolute positioning. 要进行定位,您要使用的是绝对定位。 The CSS would be: CSS将是:

#id-of-td {
    position: relative;
}

#id-of-image {
    position: absolute;
    right: 0;
    bottom: 0;
}

The HTML should be similar to: HTML应该类似于:

<td id="id-of-td">
    <img id="id-of-image" src="path/to/image" />
</td>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM