简体   繁体   English

使用css:hover在另一张图片上显示图片时出现意外的闪烁效果

[英]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.

相关问题 CSS-使用悬停效果时无法居中图像。 - CSS - Unable to center my image when using hover effect. 将鼠标悬停在另一只鼠标上时如何触发图像悬停效果? - How to trigger an image hover effect when hold mouse over another one? 将鼠标悬停在一个div上,在另一个div中生成图像的不透明度(使用jquery) - Hover over one div, effect the opacity of an image in another div (with jquery) jQuery:在另一个元素上显示一个元素,并在其上禁用鼠标悬停事件 - jquery: display one element on top of another and disable mouse hover event on it 使用CSS在另一个图像上方的可调图像 - Adjustable image on top of an another image using CSS 在使用自定义 hover 图像效果时,当另一个 html 元素添加到 body 时,图片的方向会在 hover 上发生变化 - While using custom hover image effect, when another html element is added to body, the picture's orientation changes on hover 悬停在一个div中,触发效果悬停在另一个中 - hover in one div that trigger the effect hover in another 当顶部还有另一个标签时,如何将css悬停分配给标签? - How to assign css hover to tag when there is another tag on top? 在HTML和CSS中使用图像链接时,如何在悬停时的其他位置显示图像 - How to display an image in a different spot on hover when using image links with HTML and CSS 当鼠标 hover 位于另一个鼠标之上时调整 div 大小 - Resize div when mouse hover on top of another one
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM