javascript - 使用 css :hover to display an image on top of another one 时意外的闪烁效果

标签 javascript html css image

我正在尝试使用像素艺术图像创建按钮,以便为浏览器游戏制作按钮。我成功地将两个图像放在同一位置,但是当使用 .nav_button_activated:hover CSS 代码使顶部图像透明、隐藏或具有较低的 z-index 值时,我得到了我想要的,但图像在我移动时快速出现和消失鼠标悬停在上面。准确地说,当我将鼠标停在图片上时闪烁效果停止,并且两张图片可以同时隐藏或隐藏其中的一张。我尝试了仅使用 CSS 的不同方法,但存在闪烁问题。我找不到我的问题,你能帮帮我吗? 如果 CSS 不可行,您能否建议使用 JavaScript 进行修复?

我尝试了在 Stackoverflow 或其他地方找到的不同技术,例如 this nice example , 或 Show image over another image on hover但他们都没有解决问题。 StackOverflow 实际上不起作用。我应该补充一点,我使用像素化图片并使用以下代码

.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;
}

但这似乎并没有造成问题。

我的 html 代码:

<div class='nav_button'>
<img src="./sprites/genome_button_selected.png"/>
<img class='nav_button_activated' src="./sprites/genome_button.png"/>
</div>

我的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;
}

我只是想有一个很好的悬停效果,只有当鼠标悬停时才能看到发光的标题图片,没有闪烁效果。请不要忘记解释问题的根源,这样我就不会再犯了。

最佳答案

代码的问题是当你悬停时,你将它从光标下方移开,因此它不再悬停并重复。

因此将鼠标悬停在父节点上

.nav_button:hover .nav_button_activated {
  z-index: -99;
}

关于javascript - 使用 css :hover to display an image on top of another one 时意外的闪烁效果,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57222545/

相关文章:

html - 修复 rtl bootstrap 导航栏目录

javascript - react-router-dom 中的链接删除 css

javascript - 替换 Angular 的 forEach() 中的对象

javascript - 从另一个页面触发 JavaScript 函数

用于过滤的 jQuery 同位素不起作用

HTML-CSS : Is it really necessary to use "inherit" value for the style properties in HTML design?

javascript - 在 IndexedDB 中搜索复合索引

javascript - webpack 模块中的函数范围

html - 搜索焦点和出局

javascript - 如何使元素成为浏览器窗口的高度?