【问题标题】:Hover with jquery and div encapsulation悬停与 jquery 和 div 封装
【发布时间】:2015-04-13 10:16:38
【问题描述】:

我在一个 div 中有一个 div,带有一个填充。

像这样:http://jsfiddle.net/qevoxh5f/3/

这很奇怪,因为这个 jsfiddle 是我项目的极简副本,我有不同的结果......

在我的项目中,当我的鼠标在日历 div 上时,悬停正在工作,但是当我决定移动到 clndr div(在日历 div 内)时,jQuery 决定删除类,并立即重置它,这给出了一个小的闪烁效果...

奇怪的是,这在 jsfiddle 上正常工作(在 chrome 和 firefox 上)...... 更糟糕的是:悬停闪烁效果仅在我的谷歌浏览器项目中可见。

http://media.giphy.com/media/26BkMl08chKm9kq5O/giphy.gif gif 我的问题

有什么想法吗?

$('#calendar').mouseover(function () {
  $(this).addClass('hover-month')
}).mouseout(function () {
  $(this).removeClass('hover-month')
});

$('.week').mouseover(function (e) {
  e.stopPropagation()
  $(this).addClass('hover-week')
}).mouseout(function () {
  $(this).removeClass('hover-week')
});

【问题讨论】:

  • 在鼠标悬停时添加一个“布尔”变量并限制发生的事件:
    $('#calendar').mouseover(function () { if(!childMouseovered) { $(this).addClass('hover-month'); } }).mouseout(function () { $(this).removeClass('hover-month') });
    $('.week').mouseover(function (e) { e.stopPropagation() $(this).addClass('hover-week'); childMouseovered = true; }).mouseout(function () { $(this).removeClass('hover-week') childMouseovered = false; });
  • 只需 $('#calendar').hover(function () { $(this).toggleClass('hover-month') });$('.week').hover(function () { $(this).toggleClass('hover-week') });
  • Ty Jquery King,但是使用此代码,当我在日历中的 div 内开始使用鼠标时,悬停效果会反转 :) 而且我不希望日历 div 保持悬停状态在里面选择一个div,这就是我使用stopPropagation的原因
  • @victor175 我对这段代码有同样的问题(看我的 gif)media.giphy.com/media/26BkMl08chKm9kq5O/giphy.gif

标签: jquery


【解决方案1】:
$('#calendar').hover(function () { 
  $(this).toggleClass('hover-month') ;
}); 

$('.week').hover(function () { 
  $(this).toggleClass('hover-week'); 
  $('#calendar').toggleClass('hover-month');
});  

【讨论】:

  • 这是您问题的答案吗?或者您尝试编辑您的问题?
  • 其实这是我的问题的答案,但我今天接受不了
猜你喜欢
  • 2011-01-11
  • 1970-01-01
  • 1970-01-01
  • 2011-10-22
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-02-08
  • 1970-01-01
相关资源
最近更新 更多