【问题标题】:Text animation glitch on hover悬停时的文本动画故障
【发布时间】:2019-10-20 16:26:14
【问题描述】:

我正在尝试创建一个动画,它会向上移动文本并在将鼠标悬停在卡片上时显示一些内容。将鼠标悬停在卡片上时,动画按预期工作,但是当光标放在文本顶部时,会出现这种奇怪的故障,并且文本会不断上下移动。

我已经把它放在了:https://codepen.io/anon/pen/qGwpaG

我的代码

HTML

 <section class="section" id="black">
    <div class="container">
      <p class="display-4 d-flex justify-content-center spacing text-center light bold mt-3" id="case-head"> Make something you love.</p>
    </div>

    <div class="container">
      <div class="row no-gutters">


      <div class="col-lg-4">
          <a href="blog.html" class="hover">

            <div class="image">
              <img src="https://farm4.staticflickr.com/3766/12953056854_b8cdf14f21.jpg" class="">
            </div>
          <p class="img-text color bold">Sample - 1</p>
          <p class="img-description light">Lorem Ipsum  </p>
          <i class="fas fa-long-arrow-alt-right img-description arrow"></i> 
           </a>
        </div>
  </section>

CSS

    .img-text {
    padding: 10px;
    position: absolute;
    bottom: 0px;
    left: 16px;
    font-size: 30px;

  }

  .img-description{


    position: absolute;
    padding: 10px;
    bottom: 35px;
    left: 16px;
    font-size: 20px;
    color: white;

  }

  .image {
    position:relative;
   width: 100%;
   height: auto;
}
.image img {
    width:100%;
    vertical-align:top;
}
.image:after {
    content:'\A';
    position:absolute;
    width:100%; height:100%;
    top:0; left:0;
    background:rgba(0,0,0,0.8);
    opacity:0;
    transition: all 0.5s;
    -webkit-transition: all 0.5s;
}
.image:hover:after {
    opacity:1;
}
.color
{
  color: white!important;
}

JS

$('.img-description').hide();

$(".hover").mouseover(function() {
    $(this).find(".img-text").animate({ bottom: 100 },100);
    $(this).find('.img-description').show();
})

$(".hover").mouseout(function() {
    $(this).find(".img-text").animate({ bottom: 8 });
    $(this).find('.img-description').hide();
})

【问题讨论】:

    标签: javascript html css responsive-design bootstrap-4


    【解决方案1】:

    您需要确保只有父元素触发了事件。当使用 mouseover/mouseout 时,任何子元素也会触发这些你不想要的事件。

    要解决此问题,您可以使用 mouseenter/mouseleave,或者更好的是使用 hover shorthand

    $(".hover").hover(
      function() {
        $(this).find(".img-text").animate({ bottom: 100 }, 100);
        $(this).find(".img-description").show();
      },
      function() {
        $(this).find(".img-text").animate({ bottom: 8 });
        $(this).find(".img-description").hide();
      }
    );
    

    https://codepen.io/anon/pen/mYgxWv

    【讨论】:

    • 知道了。非常感谢!
    猜你喜欢
    • 2021-05-17
    • 2011-12-05
    • 1970-01-01
    • 1970-01-01
    • 2012-06-16
    • 1970-01-01
    • 1970-01-01
    • 2022-09-27
    • 2015-05-12
    相关资源
    最近更新 更多