【问题标题】:Add animate.css class on mouseenter & mouseleave在 mouseenter 和 mouseleave 上添加 animate.css 类
【发布时间】:2016-11-05 04:21:57
【问题描述】:

当用户将鼠标悬停在图像上时,我想使用 animate.css 淡入/淡出内容。

我正在尝试找出将类从 mouseenter 上的“FadeIn”和“FadeInUp”切换到 mouseleave 上的“FadeOut”和“FadeOutDown”的最佳方式。

Codepen Example

CSS

.overlay{
position: absolute;
top: 0;
height: 100%;
width: 100%;
background: rgba(40,40,40,0.88);
left: 0;
padding: 10px;
text-align: left;
display: none;
}
.col-sm-6:hover .overlay{
display: block;
}
.overlay h2{
font-size: 20px;
color: #fff;
}
.overlay p{
color: #bbbbbb;
}
.linkbox{
position: absolute;
height: 50px;
bottom: 0;
left: 0;
background: #000;
width: 100%;
padding: 16px;
text-align: left;
color: #fff;
}
.caption{
display: none;
}

任何帮助/建议/建议将不胜感激!

【问题讨论】:

    标签: jquery html css twitter-bootstrap animate.css


    【解决方案1】:

    试试这个脚本:

    $('.col-sm-6').on('mouseenter', function(){
      $('.overlay').addClass('fadeIn fadeInUp');
      $('.overlay').removeClass('fadeOut fadeOutDown');
      $('.overlay').css('display', 'block');
    });
    
    $('.col-sm-6').on('mouseleave', function(){
      $('.overlay').removeClass('fadeIn fadeInUp');
      $('.overlay').addClass('fadeOut fadeOutDown');
      $('.overlay').css('display', 'block');
      setTimeout(function(){
         $('.overlay').css('display', 'none');
      }, 1000);
    });
    

    【讨论】:

    • 我对其进行了编辑以在动画结束后添加 display none... 因为您的 fadeOut 需要 1 秒才能结束所以添加 display: none after 1 sec.
    【解决方案2】:

    const box = document.querySelector(".box");
    
    box.addEventListener("mouseenter",function(){
      const move = "shake";
      this.classList.add(move);
      
      this.addEventListener("animationend",function(){
        this.classList.remove(move);
      });
      
      this.addEventListener("mouseleave",function(){
        this.classList.remove(move);
      });
      
    });
    .box{
      background: skyblue;
      width: 5em;
      height: 5em;
    }
    <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/animate.css/3.5.2/animate.min.css">
    <div class="box animated "></div>

    【讨论】:

      【解决方案3】:

      改用 jQuery。查看此Codepen

      还有一些小的 CSS 更改,这些更改也在 CSS 代码中进行了注释。比如不直接使用.col-sm-6,因为它们是很常见的类等等。

      作为参考,您的 JS 应如下所示:

      $(".screenshot").hover(function () {
          //stuff to do on mouse enter
          var overlay_div = $(this).find('.overlay');
          overlay_div.removeClass('fadeOut');
          overlay_div.addClass('fadeIn');
          overlay_div.find('.another-animation').removeClass('fadeOutDown');
          overlay_div.find('.another-animation').addClass('fadeInUp');
          overlay_div.css('display', 'block');
      }, 
      function () {
          //stuff to do on mouse leave
          var overlay_div = $(this).find('.overlay');
          $(this).find('.overlay').removeClass('fadeIn');
          $(this).find('.overlay').addClass('fadeOut');
          overlay_div.find('.another-animation').removeClass('fadeInUp');
          overlay_div.find('.another-animation').addClass('fadeOutDown');
          setTimeout(function() {
              overlay_div.css('display', 'none');
          }, 1000);
      });
      

      希望这会有所帮助!

      【讨论】:

      • 最终使用它并进行了一些修改,谢谢!
      • @Androbaut 我的荣幸!
      猜你喜欢
      • 2023-03-14
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多