【问题标题】:replace one div with another on hover在悬停时用另一个替换一个 div
【发布时间】:2013-10-03 22:46:14
【问题描述】:

当鼠标悬停在一个 div 上时,我想用另一个 div 替换它。具体来说会有一个平均的话,比如“苦苦挣扎”或者“超出预期”,我想当用户将鼠标悬停在平均的话上时用数值平均代替。

#html

<div class="switch avg_words float_left">
  A+ (hover to see score)  
</div>
<div class="avg_num">
  AVG = 98.35%
</div>

#css

.avg_num {
display: none;
}

#jquery

$('.switch').hover(function() {
    $(this).closest('.avg_words').hide();
    $(this).next('div').closest('.avg_num').show();
}, function() {
    $(this).next('div').closest('.avg_num').hide();
    $(this).closest('.avg_words').show();
});

隐藏第一个 div 并用第二个替换它很容易,但问题在于当悬停结束时将其恢复正常的代码。现在悬停时,divs 只是在彼此之间来回闪烁。

http://jsfiddle.net/uXeg2/1/

【问题讨论】:

  • 你怎么能只用 CSS 做到这一点?有什么好的资源吗?
  • 接受的答案是最好和最简单的方法,我认为
  • 我还是管理了:jsbin.com/nazase/3/edit?html,css,output。欢迎任何建议
  • 哇,这是一个非常漂亮的解决方案,做得很好!

标签: jquery html hover dom-manipulation


【解决方案1】:

这也可以在纯 CSS 中完成,无需依赖 JQuery:

#html
<div class="switch">
<div class="avg_words float_left">
  A+ (hover to see score)
</div>
<div class="avg_num">
  AVG = 98.35%
</div>
</div>

#css
.avg_words {
  display:block
}

.avg_num {
  display:none
}

.switch:hover .avg_words {
  display:none
}

.switch:hover .avg_num {
  display:block
}


【讨论】:

    【解决方案2】:

    我会为此使用 mouseovermouseout

    $('.switch .avg_words').mouseover(function() {
        $(this).hide();
        $(this).closest('.avg_num').show();
    });
    
    $('.switch .avg_num').mouseout(function() {
        $(this).hide();
        $(this).closest('.avg_words').show();
    });
    

    【讨论】:

    • @agrothe 不是真的,悬停适用于同一个元素,这种替代方法适用于第一个元素的鼠标悬停,然后是第二个元素的鼠标悬停;)
    • 确实如此。我只是尽量避免自己编写这样的代码,但我猜这只是个人喜好。
    【解决方案3】:

    您遇到了问题,因为您隐藏了与悬停事件绑定的相同元素。尝试更改您的标记:

    <div class="switch float_left">
        <div class="avg_words">
            A+ (hover to see score)  
        </div>
        <div class="avg_num">
          AVG = 98.35%
        </div>
    </div>
    

    然后把你的javascript改成这样:

    $('.switch').hover(function() {
            $(this).find('.avg_words').hide();
            $(this).find('.avg_num').show();
        }, function() {
            $(this).find('.avg_num').hide();
            $(this).find('.avg_words').show();
    });
    

    【讨论】:

      【解决方案4】:

      将 switch 类移动到外部 div,像这样

      <div class="switch">
      <div class="avg_words float_left">
        A+ (hover to see score)  
      </div>
      <div class="avg_num">
        AVG = 98.35%
      </div>
      </div>
      
      $('.switch').hover(function() {
              $(this).find('.avg_words').hide();
              $(this).find('.avg_num').show();
          }, function() {
              $(this).find('.avg_num').hide();
              $(this).find('.avg_words').show();
      });
      

      更新小提琴:http://jsfiddle.net/uXeg2/2/

      【讨论】:

      • 在您的选择器的上下文中查找搜索,因为divs 在同一级别上没有这些搜索。嵌套并不总是能让代码变得漂亮,但在这种情况下确实会有所帮助。
      【解决方案5】:

      闪烁是您设置课程方式的结果。因为.switch.avg_words 是完全相同的元素,所以会发生这种情况:

      1. 你悬停.switch
      2. .avg_words是隐藏的,也就是说.switch是隐藏的(是同一个div!)
      3. 由于 .switch 已隐藏,您不再悬停它
      4. .avg_words 立即显示
      5. 您现在再次悬停.switch,因为它刚刚显示(在第 4 步中)
      6. 返回步骤 1

      相反,请确保 .switch 是一个包裹在 .avg_words 周围的元素,这样当您将其悬停时它就不会被隐藏。

      【讨论】:

      • 我认为这可能是问题的一部分——我玩了一段时间,但由于 div 的层数而无法正常工作。 (无法找出要使用的next 的正确排列方式)。谢谢!
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-12-28
      • 2013-03-03
      • 2015-06-22
      • 2019-03-22
      • 1970-01-01
      相关资源
      最近更新 更多