jquery - mouseover mouseleave translateY不重复

标签 jquery html css

我正在尝试在 jquery 中使用一个函数,我使用 mouseover 从顶部出现一个 div,然后使用 mouseleave 使其消失。我可以执行此操作一次,但如果我再次执行鼠标悬停,则不再有效。

html

<div id="box">
<div id="box_appear"></div>
</div>

CSS

#box{position:relative; width:300px; height:300px; overflow:hidden}
#box_appear{position:absolute; width:300px; height:300px; transform:translateY(-100%); top:0;left:0}

@keyframes slideInDown {
from {
-webkit-transform: translate3d(0, -100%, 0);
transform: translate3d(0, -100%, 0);
visibility: visible;
}

to {
-webkit-transform: translate3d(0, 0, 0);
transform: translate3d(0, 0, 0);
  }
 }

.slideInDown {
 -webkit-animation-name: slideInDown;
 animation-name: slideInDown;
 -webkit-animation-duration: 0.5s;
 animation-duration: 0.5s;
 -webkit-animation-fill-mode: both;
 animation-fill-mode: both;
}

@keyframes slideInUp {
from {
-webkit-transform: translate3d(0, 0, 0);
transform: translate3d(0, 0, 0);
visibility: visible;
}

to {
-webkit-transform: translate3d(0, -100%, 0);
transform: translate3d(0, -100%, 0);
}
}

.slideInUp {
-webkit-animation-name: slideInUp;
animation-name: slideInUp;
-webkit-animation-duration: 0.5s;
animation-duration: 0.5s;
-webkit-animation-fill-mode: both;
animation-fill-mode: both;
}

j查询

$('#box').on('mouseover', function () {

   $('#box_appear').addClass('slideInDown');


});
$('#box').on('mouseleave', function () {       
       $('#box_appear').addClass('slideInUp');
});

最佳答案

这只是一个优化提示,但由于您多次调用 $('$box_appear') 我会缓存它以减少查找。您也可以执行相同的#or $('#box')

就您的主要问题而言,您需要删除在 mouseover/mouseleave 上添加的类。否则他们会互相否定:

$box = $('#box');
$boxAppear = $('#box_appear');

$box.on('mouseover', function () {
  $boxAppear
    .removeClass('slideInUp')
    .addClass('slideInDown');
}).on('mouseleave', function() {
  $boxAppear
    .removeClass('slideInDown');
    .addClass('slideInUp');
});

关于jquery - mouseover mouseleave translateY不重复,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40962716/

相关文章:

html - div 不水平和垂直居中

php - 循环遍历序列化数据以将更新插入 MySQL DB

jquery - 使用jquery `<li><span>`时如何下推 `.slidedown()`

javascript - 如何在 JSON 中使用占位符

java - Java Swing href 的问题

html - 我怎样才能让css支持旧版本的浏览器

css - 使用 float 将文章和侧边栏放在一起

javascript - 向下滑动显示更多内容

javascript - 单击浏览器滚动条关闭弹出窗口

javascript - 使用 jquery .each() 增加 attr 值