Swap DIV class on hover with jQuery

白昼怎懂夜的黑 提交于 2019-12-08 04:28:38

问题


This is what I'm trying to achieve :

2 DIV's, on page load the first DIV is visible, on hover it switches to the second DIV (which is initially hidden).

Both DIV's are the same height and width and are positioned absolutely within the wrapper.

This is what i've got so far, but it's not working properly -

JS :

 (function($) { 
 $(".wrap").hover(function() {
 $(".first,.second", this).toggle();
 })
 })( jQuery );

CSS :

 .wrap {position:relative;}

 .first {
 position:absolute;
 top:0;
 padding:20px;
 width:80px;
 height:80px;
 background:green;
 color:white;
 display:block;
 }

 .second {
 position:absolute;
 top:0;
 padding:20px;
 width:80px;
 height:80px; 
 background:yellow;
 color:blue;
 display:block;
 visibility:hidden;
 }

HTML :

<div class="wrap">
<div class="first">FIRST DIV</div>
<div class="second">SECOND DIV</div>
</div>

Here is a working FIDDLE so you can see what's happening.

Any suggestions?


回答1:


Dont Use visibility:hidden, instead use display:none

.second {
      position:absolute;
      top:0;
      padding:20px;
      width:80px;
      height:80px; 
      background:yellow;
      color:blue;
      display:none;
}

Working Demo



来源:https://stackoverflow.com/questions/24351070/swap-div-class-on-hover-with-jquery

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!