javascript - 滑动面板在鼠标悬停时振荡

标签 javascript jquery html css

我用 HTML 和 jQuery 构建了一个 div 容器,当您将鼠标悬停在它上面时,另一个面板会滑过顶部。但是我遇到的滑动面板“yoyo”一下。我认为这是因为当面板滑过 div 时,悬停状态被触发然后丢失,因此面板再次连续上下弹出。

我想要发生的是,当您将鼠标悬停在面板显示的正方形中的任何地方时,当您不在正方形中时,它不会显示。

这是一个 fiddle ,所以你可以看到问题是什么,也许这样更容易解释。

https://jsfiddle.net/xa5gtd2u/3/

还有代码

HTML

<div class="container">
  <div class="services-blocks">
    <img class="alignnone size-full wp-image-1974" src="http://placehold.it/250x250" alt="Design" width="250" height="250" />
    <h3>title here</h3>
  </div>
  <div class="services-slide-panel">
  <h3>title here</h3>
    Some text here
    Some text here
    Some text here
    Some text here
  </div>
</div>

CSS

    .services-slide-panel {

  background: #f3535e;
  width: 100%;
  position: absolute;
  left: 0;
  bottom: 0;
  top: 0;
  display: none;
  color: #ffffff;

}

.services-slide-panel h3 {

  color: #ffffff;

}

.services-blocks h3 {

  text-align: center;
  position: absolute;
  bottom: 5%;
  width: 100%;
  color: #ffffff;

}

.container {

  width: 250px;
  height: 250px;
  position: relative;
}

jQuery

      $(document).ready(function() {


    $(".services-blocks").hover(function() {

      $(this).next(".services-slide-panel").slideToggle("slow");

    });

  });

最佳答案

CSS3 过渡始终是实现此类效果的更好选择。此外你必须 hover().container否则,滑入的 div 将改为悬停,并且将调用 hover() 的 hoverout 部分。功能。

  $(document).ready(function() {


    $(".container").hover(function() {

      $(this).find(".services-slide-panel").addClass("slow");

    }, function() {
      $(this).find(".services-slide-panel").removeClass("slow");

    });

  });
.services-slide-panel {
  background: #f3535e;
  width: 100%;
  position: absolute;
  left: 0;
  bottom: 0;
  top: 110%;
  color: #ffffff;
  transition: all 0.3s ease;
}

.services-slide-panel.slow {
  top: 0%;
}

.services-slide-panel h3 {
  color: #ffffff;
}

.services-blocks h3 {
  text-align: center;
  position: absolute;
  bottom: 5%;
  width: 100%;
  color: #ffffff;
}

.container {
  width: 250px;
  height: 250px;
  position: relative;
  overflow:hidden;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div class="container">
  <div class="services-blocks">
    <img class="alignnone size-full wp-image-1974" src="http://placehold.it/250x250" alt="Design" width="250" height="250" />
    <h3>title here</h3>
  </div>
  <div class="services-slide-panel">
    <h3>title here</h3> Some text here Some text here Some text here Some text here
  </div>
</div>

Fiddle here

关于javascript - 滑动面板在鼠标悬停时振荡,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36626699/

相关文章:

javascript - 在 jQuery 中访问 PHP $_SESSION 变量并执行错误处理和页面重定向

javascript - 在 JavaScript 中将数据属性转换为访问器属性,反之亦然

jquery - 使用来自 Lavish 的自定义 Twitter Bootstrap 主题获取错误 Glyphicon-halflings-regular.ttf/woff/woff2

html - 如何仅使用内联 CSS 将滚动框定位在图像顶部

JavaScript 返回数组中最小和最大数的索引

javascript - 如何在按下 `Stop` 按钮时停止动画并卡住图像

JQuery:使包含不区分大小写

javascript - 弹出窗口中的级联下拉菜单

jquery - Bootstrap Modal 未关闭数据关闭 (Rails)

javascript - HTML 为什么 drop 函数被调用四次而不是一次?