gpt4 book ai didi

JavaScript 的 onmouseover 和 onmouseout 事件导致闪烁

转载 作者:行者123 更新时间:2023-11-29 18:08:21 25 4
gpt4 key购买 nike

我对 JavaScript、onmouseoveronmouseout 事件有疑问。当鼠标到达所需元素的底部边缘时,鼠标悬停和鼠标移开效果开始闪烁。这是示例:

function menuHover(field)
{
var img = field.nextElementSibling;
var height = img.height;
var bottom = 0 - Math.floor(height / 2);
bottom += 'px';
img.style.display = 'block';
img.style.bottom = bottom;
}

function menuHoverOut(field)
{
var img = field.nextElementSibling;
img.style.display = 'none';
}

演示:https://jsfiddle.net/index23/ety2z0zu/9/

有解决办法吗?

最佳答案

建议使用 CSS。但这是您问题的答案https://jsfiddle.net/ety2z0zu/11/ .请从您的 JavaScript 中删除 img.style.bottom = bottom;

function menuHover(field) {
var img = field.nextElementSibling;
var height = img.height;
var bottom = 0 - Math.floor(height / 2);
bottom += 'px';
img.style.display = 'block';
// console.log(height);
}

function menuHoverOut(field) {
var img = field.nextElementSibling;
img.style.display = 'none';
}
a,
a:link {
color: inherit;
text-decoration: none;
}
body {
background-color: #2a4b8b;
}
header nav {
text-align: center;
position: relative;
}
.main-nav {
margin: 0 auto;
padding-right: 25px;
list-style: none;
display: inline-block;
}
.main-nav li {
float: left;
margin-right: 23px;
display: block;
padding-top: 50px;
-webkit-box-shadow: 0 4px 8px rgba(2, 3, 2, .39);
-moz-box-shadow: 0 4px 8px rgba(2, 3, 2, .39);
box-shadow: 0 4px 8px rgba(2, 3, 2, .39);
position: relative;
}
.main-nav li a {
display: block;
padding-left: 25px;
padding-right: 25px;
padding-bottom: 3px;
border-bottom: 5px solid #6173ad;
border-bottom-right-radius: 5px;
border-bottom-left-radius: 5px;
}
.hover-img {
display: none;
position: absolute;
left: 0px;
width: 100%;
height: auto;
}
<header>
<nav>
<ul class='main-nav'>
<li>
<a href='#' onmouseover='menuHover(this);' onmouseout='menuHoverOut(this);'>LINK</a>
<img alt='' class='hover-img' src='https://dl.dropboxusercontent.com/u/32550463/menu-hover.png' />
</li>
<li>
<a href='#' onmouseover='menuHover(this);' onmouseout='menuHoverOut(this);'>LINK</a>
<img alt='' class='hover-img' src='https://dl.dropboxusercontent.com/u/32550463/menu-hover.png' />
</li>
</ul>
</nav>
</header>

关于JavaScript 的 onmouseover 和 onmouseout 事件导致闪烁,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29670153/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com