【问题标题】:Mouseover and Mouseout not behaving as expectedMouseover 和 Mouseout 的行为不符合预期
【发布时间】:2015-05-25 07:15:36
【问题描述】:

尝试使用这些 jQuery 方法在它所描述的图像的悬停时显示描述......然后将在鼠标悬停和鼠标悬停时显示和隐藏它们(全部)。但我希望在第一次鼠标悬停之前隐藏描述。我也遇到了将图像/描述悬停与每个实例匹配的问题。我是 OOP 的新手,但我认为这在这里会派上用场。无论如何,任何提示都表示赞赏!

这是我的代码:

    $('.image').mouseover(function() {
      $('.description').show();
    })
    
    $('.image').mouseout(function() {
      $('.description').hide();
    })
    .col-lg-3 h2 {
      font-size: 1.1em;
      color: black;
      font-family: 'Arial';
      margin-left: .1em;
      font-weight: 500;
      display: none;
    
    }
    
    .col-lg-3 p {
      font-size: .9em;
      margin-left: .2em;
      margin-top: -.3em;
      font-weight: 400;
      display: none;
      <div class="col-lg-3 project1">
        <a class = "image" href="" target="_blank"> <img width="100%" height="100%" src="" alt=""></a> 
        <div class = "description">
          <h2>  </h2> 
          <p>  </p>
        </div>

如果我希望它仅适用于图像/描述对的实例,我可能也应该添加一些 .this 东西,不是吗?

【问题讨论】:

  • 能不能放到jsfiddle里分享一下?
  • @jQuery.PHP.Magento.com 已修复 :)
  • I am new at OOP OOP 在哪里?而且你在这里根本没有使用过javascript,它只是jQuery。
  • @HiteshSiddhapura Sweet。解决了一半。仍然需要弄清楚我应该如何使用 .this 以便一次显示唯一正确的描述。目前,只要我将鼠标悬停在任何图像上,它就会显示所有描述。
  • @HiteshSiddhapura,太棒了,谢谢。

标签: javascript jquery mouseevent mouseover mouseout


【解决方案1】:

查看以下解决方案

JS PART:更改 jQuery 选择器

$('.image').mouseover(function() {
  $('.description *', $(this).parent()).show();
})

$('.image').mouseout(function() {
  $('.description *', $(this).parent()).hide();
})

CSS 部分:没有变化

.col-lg-3 h2 {
  font-size: 1.1em;
  color: black;
  font-family: 'Arial';
  margin-left: .1em;
  font-weight: 500;
  display: none;

}

.col-lg-3 p {
  font-size: .9em;
  margin-left: .2em;
  margin-top: -.3em;
  font-weight: 400;
  display: none;
}

HTML PART:添加正确的结尾&lt;/div&gt;

<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div class="col-lg-3 ye">
            <a class = "image" href="targethere" target="_blank"> <img width="10%" height="10%" src="http://srdtu.org/assets/img/events/quiz-.jpg.jpg" alt="quiz"></a> 
            <div class = "description">
            <h2> quiz </h2> 
            <p> a quote guessing game </p>
            </div>
      </div>

【讨论】:

  • 请阅读OP的问题I dont set description display to none,
  • @jQuery.PHP.Magento.com 是的,我已经删除了那个 css 部分
【解决方案2】:

不要使用 mouseout 和 mouseover。在使用这些功能时,您需要考虑很多事情。我建议您使用悬停功能。..

$('.image').hover(function() { $('.description').show(); }, function() { $('.description').hide(); });

第一个参数是鼠标悬停时执行的函数,第二个参数是鼠标移出时执行的函数。

【讨论】:

猜你喜欢
  • 2017-04-12
  • 2012-04-24
  • 1970-01-01
  • 1970-01-01
  • 2011-03-03
  • 1970-01-01
  • 2014-03-09
  • 2011-04-13
  • 1970-01-01
相关资源
最近更新 更多