下拉菜单淡出

时间:2016-04-26 17:19:06

标签: javascript jquery html css css3

我创建了一个下拉菜单。当用户将鼠标悬停在导航按钮上时,使用CSS3 Transitions,类'dropdown-content'会淡入,如下面的演示所示:

https://jsfiddle.net/4xk78qse/

我无法弄清楚当用户没有悬停在任何按钮上时,如何淡出'下拉内容'。

我确实尝试添加

animation fadeout 2s;

@keyframes fadeout {
   from { opacity: 1; }
   to { opacity: 0; }
}

基本上与jsfiddle示例中的动画淡出相反,但这不起作用。

总之,当用户没有悬停在任何按钮上时,我希望淡出。

3 个答案:

答案 0 :(得分:2)

这里的诀窍是使用CSS Transitions而不是CSS动画。您将设置opacity.dropdown-content { visibility: hidden; position: absolute; background-color: #f9f9f9; min-width: 160px; box-shadow: 0px 8px 16px 0px rgba(0,0,0,0.2); -webkit-transition: opacity 2s, visibility 2s; -moz-transition: opacity 2s, visibility 2s; -ms-transition: opacity 2s, visibility 2s; -o-transition: opacity 2s, visibility 2s; transition: opacity 2s, visibility 2s; opacity: 0; } .dropdown:hover .dropdown-content { visibility: visible; opacity: 1; } 而不是显示。

$hours = 0.5;
$rate = 100;

$overtime = max($hours - 40, 0);
$pay = $overtime * $rate * 1.5;
$pay += ($hours - $overtime) * $rate;
$pay = $pay + ($pay * 0.05);

echo "Hours Traveled: " . $hours . "<br>";
echo "Transport rate (per hour): $" . number_format($rate, 2) . "<br>";
echo "Additional Hours: " . $overtime . "<br>";
echo "Your Total is: $" . number_format($pay, 2) . "<br>";

以下是您要完成的工作演示: https://jsfiddle.net/4xk78qse/9/

答案 1 :(得分:1)

你最好使用过渡来代替这样的关键帧动画:

.dropdown-content {
    position: absolute;
    background-color: #f9f9f9;
    min-width: 160px;
    box-shadow: 0px 8px 16px 0px rgba(0,0,0,0.2);
    opacity: 0;
    transition: all 1s;
}

Fiddle

答案 2 :(得分:0)

你需要这个css,因为当悬停结束你的悬停类变换&#34; .dropdown。dropdown-content&#34;。然后你只为下拉内容调用fadeout。

.dropdown .dropdown-content{
  display: block;
    opacity: 1;
    -webkit-animation: fadeout 2s;
    -moz-animation: fadeout 2s;
    -ms-animation: fadeout 2s;
    -o-animation: fadeout 2s;
    animation: fadeout 2s;
    opacity: 0;
}
@keyframes fadeout {
   from { opacity: 1; }
   to { opacity: 0; }
}