[英]Unexpected flashing effect when using css :hover to display an image on top of another one
I'm trying to create buttons with pixel art images to make a button for a browser game. 我正在尝试使用像素艺术图像创建按钮,以使浏览器游戏成为一个按钮。 I succeeded to put two images at the same position but when using .nav_button_activated:hover css code to make the top image transparent, hidden or with lower z-index value, I get what I want but the image quickly appears and disappear as I move the mouse over it.
我成功地将两个图像放置在同一位置,但是当使用.nav_button_activated:hover CSS代码使顶部图像透明,隐藏或具有较低的z-index值时,我得到了想要的图像,但是随着移动,图像迅速出现并消失了鼠标移到它上面。 To describe it properly, the flashing effect stops when I stop the mouse on the picture, and the two pictures can be both at the time or on of them hidden.
为了正确描述,当我将鼠标停在图片上时,闪烁效果会停止,并且两个图片都可以同时显示或隐藏。 I tried different approaches using CSS only but with the flashing problem.
我只使用CSS尝试了不同的方法,但是遇到了闪烁的问题。 I find my problem nowhere, can you help me please?
我无处发现我的问题,请你能帮我吗? If not possible with CSS, could you propose a fix with JavaScript?
如果无法使用CSS,您可以建议使用JavaScript进行修复吗?
I tried different techniques I found on Stackoverflow or elsewhere such as this nice example , or Show image over another image on hover but none of them solved the problem. 我尝试了在Stackoverflow或其他地方发现的其他技术(例如此示例) ,或在悬停时将图像显示在另一个图像上,但没有一个能够解决问题。 The StackOverflow doesn't work actually.
StackOverflow实际上不起作用。 I should add that I work with pixelated picture and that I use the following code
我应该补充一点,我使用像素化图片,并且使用以下代码
.nav_bar img {
transform-origin: top left;
transform: scale(2); /* To increase the size of image, use transform: scale() */
/* remove anti-aliasing */
image-rendering: optimizeSpeed; /* STOP SMOOTHING, GIVE ME SPEED */
image-rendering: -moz-crisp-edges; /* Firefox */
image-rendering: -o-crisp-edges; /* Opera */
image-rendering: -webkit-optimize-contrast; /* Chrome (and eventually Safari) */
image-rendering: pixelated; /* Chrome */
image-rendering: optimize-contrast; /* CSS3 Proposed */
-ms-interpolation-mode: nearest-neighbor;
}
But It doesn't seems to create the problem. 但这似乎并没有造成问题。
My html code : 我的html代码:
<div class='nav_button'>
<img src="./sprites/genome_button_selected.png"/>
<img class='nav_button_activated' src="./sprites/genome_button.png"/>
</div>
My CSS code (with flash effect I would like to understand and suppress) : 我的CSS代码(具有闪光效果,我想了解和抑制):
.nav_button {
position: relative;
float: right;
margin-right: 20px;
}
.nav_button img {
margin-right: 80px;
margin-left: 30px;
}
.nav_button_activated {
position: absolute;
top: 0px;
right: 0px;
}
.nav_button_activated:hover {
z-index: -99;
}
I just want to have a nice hover effect, seeing the illuminated title picture only when the mouse hovers it, with no flashing effect. 我只想有一个很好的悬停效果,只有当鼠标悬停时才能看到发光的标题图片,而没有闪烁效果。 Please, don't forget to explain the origin of the issue, so that I won't do it again.
请不要忘记解释问题的根源,这样我就不会再做。
Problem with the code is when you hover, you move it from under the cursor so it is no longer hovered and it repeats. 代码的问题是,当您将鼠标悬停时,您将其从光标下方移开,因此不再悬停并重复执行。
So put the hover on the parent 因此,将鼠标悬停在父对象上
.nav_button:hover .nav_button_activated {
z-index: -99;
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.