【发布时间】:2016-08-25 14:06:58
【问题描述】:
为什么 jQuery 的 .fadeIn() 函数会破坏我的 CSS 悬停功能?当您将鼠标悬停在它上面时,我的 div 应该显示它的菜单。当我的页面显示我将使菜单淡入时,它应该具有我的常规悬停功能。
这个JSFiddle很好地说明了这个问题。
为什么它会破坏我的悬停功能?如何同时保留淡入和悬停功能?
注意我需要保留 display: flex 以确保菜单 div 比其父 div 宽时不会换行。
.widget {
position: relative;
width: 300px;
height: 300px;
background-color: #ddd;
}
.widget-menu {
width: 400px;
height: 100px;
background-color: #555;
position: absolute;
top: 0;
display: none;
}
.widget:hover .widget-menu {
display: flex;
/* used to ensure wrapping doesn't occur if the menu is wider than the widget container */
}
.widget:enabled .widget-menu {
display: none;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div class="widget">
<div class="widget-menu">
<button>Btn 1</button>
</div>
<br/>
<br/>
<br/>
<br/>
<br/>
<p>Hover me to see menu appear. Then click the below button. Now my hovering is screwed up after the animation is complete.</p>
</div>
<br/>
<button>Fade In</button>
【问题讨论】:
-
你到底想达到什么目的?菜单在淡入时可见,因此当您将鼠标悬停在文本上时,它已经显示了您喜欢的内容,对吧?
-
@Roberrrt 在页面加载时菜单将淡入,因此用户知道它在那里。然后在第一次悬停和悬停/鼠标离开后,该菜单应该像往常一样消失。现在那没有发生
-
哦,这是因为 jQuery 为您的元素添加了内联样式,从而推翻了您的悬停