【问题标题】:mouseenter/mouseleave caption swap not working when moving mouse too quicklymouseenter/mouseleave 标题交换在鼠标移动太快时不起作用
【发布时间】:2017-10-16 15:35:26
【问题描述】:

我在使用 mouseenter/mouseleave 来触发行为时遇到问题,在这种情况下,将大部分内容过快地移动到目标元素上会导致不良行为。我见过一些有类似问题的线程,但似乎没有一个能解决我的问题。

我正在创建一个带有“标题”标题的图像网格。我试图做到这一点,以便当用户将鼠标悬停在网格中的图像上时,标题标题会淡出,并在其位置显示“描述”标题。然后在 mouseleave 上反转,标题标题重新出现。

我已经使用 mouseenter / mouseleave 和 fadeIn / FadeOut 来实现效果 - 在块之间缓慢移动时它可以正常工作,但如果移动太快,“标题”和“描述”都会同时显示。这是我的代码:

$(".stage").mouseenter(function() {
  var desc = $(".desc", this);
  var title = $(".title", this);
  title.fadeOut(200, "swing", function() {
    desc.fadeIn(100, "swing");
  });
}).mouseleave(function() {
  var desc = $(".desc", this);
  var title = $(".title", this);
  desc.fadeOut(0, "swing", function() {
    title.fadeIn(0, "swing");
  });
});
.stage {
  height: 200px;
  width: 200px;
  background-color: #1d2452;
  color: white;
  position: relative;
  display: inline-block;
}

.stage > .caption {
  position: absolute;
  top: 50%;
  left: 50%;
  margin-right: -50%;
  transform: translate(-50%, -50%);
  text-align: center;
}

.desc {
  display: none;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div class="stage">
  <div class="caption">
    <h2 class="title">Title</h2>
    <p class="desc">Description Text</p>
  </div>
</div>

<div class="stage">
  <div class="caption">
    <h2 class="title">Title</h2>
    <p class="desc">Description Text</p>
  </div>
</div>

<div class="stage">
  <div class="caption">
    <h2 class="title">Title</h2>
    <p class="desc">Description Text</p>
  </div>
</div>

这里还有 JSfiddle - https://jsfiddle.net/y16nufd7/1/

【问题讨论】:

    标签: jquery fadein fadeout mouseenter mouseleave


    【解决方案1】:

    使用stop() 来实现:

    $(".stage").mouseenter(function() {
      var desc = $(".desc", this);
      var title = $(".title", this);
      title.stop().fadeOut(200, "swing", function() {
        desc.stop().fadeIn(100, "swing");
      });
    }).mouseleave(function() {
      var desc = $(".desc", this);
      var title = $(".title", this);
      desc.stop().fadeOut(0, "swing", function() {
        title.stop().fadeIn(0, "swing");
      });
    });
    .stage {
      height: 200px;
      width: 200px;
      background-color: #1d2452;
      color: white;
      position: relative;
      display: inline-block;
    }
    
    .stage > .caption {
      position: absolute;
      top: 50%;
      left: 50%;
      margin-right: -50%;
      transform: translate(-50%, -50%);
      text-align: center;
    }
    
    .desc {
      display: none;
    }
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
    <div class="stage">
      <div class="caption">
        <h2 class="title">Title</h2>
        <p class="desc">Description Text</p>
      </div>
    </div>
    
    <div class="stage">
      <div class="caption">
        <h2 class="title">Title</h2>
        <p class="desc">Description Text</p>
      </div>
    </div>
    
    <div class="stage">
      <div class="caption">
        <h2 class="title">Title</h2>
        <p class="desc">Description Text</p>
      </div>
    </div>

    【讨论】:

    • 啊,行得通!我正在使用 .stop(true, true) 没有让我到达那里,但这可以解决问题,谢谢!
    • 很高兴为您提供帮助 =)
    【解决方案2】:

    值得注意的是,这在没有 JS 的情况下也可以实现。 CSS 为我们提供了悬停动画过渡所需的一切:

    .stage {
      height: 200px;
      width: 200px;
      background-color: #1d2452;
      color: white;
      position: relative;
      display: inline-block;
    }
    
    .title,
    .desc {
      display: block;
      position: absolute;
      left: 0;
      right: 0;
      margin: 0;
      padding: 0;
      top: 50%;
      text-align: center;
      transform: translate(0%, -50%);
    }
    
    .desc {
      opacity: 0;
    }
    
    .stage:hover .caption .title {
      opacity: 0;
      -webkit-transition: opacity 200ms cubic-bezier(.02, .01, .47, 1);
      -moz-transition: opacity 200ms cubic-bezier(.02, .01, .47, 1);
      -ms-transition: opacity 200ms cubic-bezier(.02, .01, .47, 1);
      -o-transition: opacity 200ms cubic-bezier(.02, .01, .47, 1);
    }
    
    .stage:hover .caption .desc {
      opacity: 1;
      -webkit-transition: opacity 100ms cubic-bezier(.02, .01, .47, 1) 200ms;
      -moz-transition: opacity 100ms cubic-bezier(.02, .01, .47, 1) 200ms;
      -ms-transition: opacity 100ms cubic-bezier(.02, .01, .47, 1) 200ms;
      -o-transition: opacity 100ms cubic-bezier(.02, .01, .47, 1) 200ms;
    }
    <div class="stage">
      <div class="caption">
        <h2 class="title">Title</h2>
        <p class="desc">Description Text</p>
      </div>
    </div>
    
    <div class="stage">
      <div class="caption">
        <h2 class="title">Title</h2>
        <p class="desc">Description Text</p>
      </div>
    </div>
    
    <div class="stage">
      <div class="caption">
        <h2 class="title">Title</h2>
        <p class="desc">Description Text</p>
      </div>
    </div>

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2011-09-02
      • 1970-01-01
      • 1970-01-01
      • 2011-06-17
      • 1970-01-01
      • 1970-01-01
      • 2013-03-05
      • 2023-03-21
      相关资源
      最近更新 更多