div:悬停位置在不同浏览器中以不同方式交换图像
我使用 div:hover CSS 规则来实现所需的效果 - 当鼠标悬停在导航图像上时图像“交换”:www.scottmccarthydesign.com/dev.index.html
然而,我在这里的设置实际上并不是“交换”。主导航图像是整个桌面的扁平 jpeg(为了更快加载),并且桌面上的每个项目上方都有空的 div,以使用链接映射图像。当鼠标悬停在这些空 div 上时,div:hover 规则会使用 .png 填充 div,该 .png 会精确放置在主桌面图像上,以提供图像交换的效果。
它在 Firefox 中运行良好,但我不明白为什么 Safari 将 :hover 图像定位在桌面上与 Firefox 不同 - 每个 :hover 图像大约有 1 像素的偏差,使得桌面上的单独图像看起来实际上正在移动鼠标悬停时会有点。有什么建议吗??
I've used the div:hover CSS rule to achieve the desired affect - an image "swap" when the mouse hovers over a navigation image: www.scottmccarthydesign.com/dev.index.html
My setup here, however, is not actually a "swap." The main navigation image is a flattened jpeg of the entire desk (for faster loading), and there are empty divs over each item on the desk to map the image with links. When these empty divs are moused over, the div:hover rule fills the div with a .png that is meant to be placed precisely over the main desk image to give the effect of an image swap.
It works nicely in Firefox, but I do not understand why Safari is positioning the :hover image over the desk differently than Firefox is - each :hover image is about 1 pixel off, making it look like the seperate images on the desk are actually shifting a bit when moused over. Any suggestions??
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
我在
以外的元素上使用
:hover
伪类时遇到了问题。您可以使用 (jquery/javascript) 使用onmouseover
和onmouseout
事件来更改所述的类。
使用
onmouseover
添加一个定义特定背景图像的类。使用onmouseout
删除该类。更简单的是,使用 jquery
.hover()
I've had trouble using the
:hover
pseudo-class on elements other than<a></a>
. You could use (jquery/javascript) to alter the class of the said<div>
usingonmouseover
andonmouseout
events.With
onmouseover
, add a class that defines a certain background image. Withonmouseout
remove that class.Even easier, use jquery
.hover()
将其添加到 css 后重新定位链接:
reposition your links after adding this to your css:
我以前遇到过这个问题,发现它与图像的大小有关。当图像的某一维度的像素大小为奇数时,Firefox 和 Chrome/Safari 执行的计算(特别是使用
center
时)会略有不同。本质上,它与子像素舍入有关。只需在具有奇数长度的轴上向图像添加或减去一个像素,使它们成为偶数(即,将其变为 100x124,而不是 100x123),您就应该是黄金了。
I've come across this issue before and found that it had to do with the size of the image. When the image is an odd-number pixel size on one of its dimensions, the calculations done by Firefox and Chrome/Safari (particularly when using
center
) are slightly different. Essentially, it has to do with sub-pixel rounding.Simply add or subtract a pixel to your images on the axis that has an odd number length, to make them an even number (ie - instead of 100x123, make it 100x124) and you should be golden.
不需要使用 Javascript,这当然可以仅使用 CSS 来实现。在我看来,最好的选择是使用本文中讨论的有关 CSS Sprites 的技术: http://www .alistapart.com/articles/sprites。
本质上,对于桌面上的每一项,将悬停图像和非悬停图像放置在同一张图像中,一个放在另一个图像的顶部,以便顶部区域具有非悬停状态,底部区域具有悬停状态。您的代码可能看起来像这样修改:
您的桌面图像将是空的,并且您的项目将位于其顶部。
No need to use Javascript, this can certainly be achieved using just CSS. In my opinion, your best bet is to use the technique discussed in this article on CSS Sprites: http://www.alistapart.com/articles/sprites.
Essentially, for each item on your desk, place the hovered and non-hovered image in same image, one on top of each other, so that the top area has the non-hover state, and the bottom area has the hover state. Your code will probably look like this modified:
Your desk image will then be empty, and of your items will just be on top of it.