【问题标题】:Mouseover Mouseout with overlapping content内容重叠的鼠标悬停鼠标悬停
【发布时间】:2016-11-13 14:02:44
【问题描述】:

当我使用class=background(演示中的绿色小方块)将鼠标悬停在 div 上时,我使用class=hover 淡入 div(在演示中显示灰色和蓝色 div)。

灰色部分与.background 重叠,我可以在其中移动鼠标而不会触发.background 上的mouseout

但是..

如果我将鼠标移到灰色 div 之外(例如悬停在蓝色上),则 .background 上的 mouseout 会被触发。

如何防止这种情况发生,这样只要我将鼠标悬停在新显示的.hover div 上,'.background' 上的mouseout 就不会被触发?

$('.AddDiv').on('click', function() {
  var html = '<div class="container"><div class="background"></div><div class="hover"></div></div>';
  $('.Wrap').prepend(html);
});

$(".Wrap").on("mouseover", ".background", function() {
  $(this).next(".hover").fadeIn(500);
});

$(".Wrap").on("mouseout", ".hover", function() {
  $(this).fadeOut(200);
});
.Wrap {
  width: 650px;
  height: 800px;
}
.container {
  position: relative;
  top: 5px;
  left: 5px;
  width: 200px;
  height: 200px;
  background-color: red;
  float: left;
  margin-left: 5px;
  margin-top: 5px;
}
.AddDiv {
  position: absolute;
  top: 0px;
}
.background {
  width: 20px;
  height: 20px;
  background-color: green;
  position: absolute;
  left: 170px;
  top: 10px;
}
.content {
  width: 170px;
  height: 120px;
  background-color: grey;
  position: relative;
  left: 15px;
  top: 15px;
}
.navigation {
  width: 190px;
  height: 40px;
  background-color: blue;
  position: relative;
  top: 30px;
  left: 5px;
}
.hover {
  width: 200px;
  height: 200px;
  background-color: rgba(255, 255, 255, 0.8);
  position: absolute;
  z-index: 1001;
  display: none;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div class="Wrap">
  <div class="container">
    <div class="background"></div>
    <div class="hover">
      <div class="content"></div>
      <div class="navigation"></div>
    </div>
  </div>
</div>
<button class=AddDiv>AddDiv</button>

【问题讨论】:

  • 你想做什么?
  • 请花点时间将您的问题编辑成我们可以理解的内容,记住:我们可以看到您的代码,但我们不知道您的代码背后的意图。而且,目前,您的问题文本很难解析成您想要发生的事情的明确陈述。
  • 我想将鼠标悬停在绿色框上并显示 div class="hover",当我将鼠标从 class="hover" 移出时,class="hover" 隐藏
  • 你的意思是,当鼠标移到“悬停”内的“内容”和“导航”上时,所有内容都不会消失?

标签: javascript jquery html css


【解决方案1】:

使用mouseleave 代替mouseout

$('.AddDiv').on('click', function() {
    $('.Wrap').prepend($('<div class="container"><div class="background"></div><div class="hover"></div></div>'));
});

$(".Wrap").on("mouseover", ".background", function () {
    $(this).next(".hover").fadeIn(500);
});


$(".Wrap").on("mouseleave", ".hover", function () {
$(this).fadeOut(200);
});

【讨论】:

    猜你喜欢
    • 2011-08-04
    • 2018-09-03
    • 1970-01-01
    • 2016-05-26
    • 1970-01-01
    • 2015-04-22
    • 2011-05-26
    • 2017-10-14
    • 1970-01-01
    相关资源
    最近更新 更多