简体   繁体   中英

addClass transition doesn't work

Seems like a common problem, but none of the answers i found so far work Everything seems like a pretty standard and straightforward code, yet can't make the damn thing work. Making a dropdown menu, and I need a hidden ul appear slowly on hover.

Html of it

<div class="main-nav">
  <ul>
    <li>
      <a href="wtvr">Title</a>
      <ul class="dropedmainnav t50 ">
        <li class="subnavli" style="position:relative;">
          <div class="superimage" style="position:absolute;  right:-100%;">
            <img src="" alt="{{link.handle}}">
          </div>
        </li>
        <li class="t50 subnavli">
          <a href="wtvr">Subnav link</a>
        </li>
        <div class="subnavsides"></div>
      </ul>
    </li>        
  </ul>
</div>   

js of it

$(".main-nav ul li").each(function(){
    $(this).mouseover(function(){$(this).find("ul").addClass("dropedunderul");});
    $(this).mouseleave(function(){$(this).find("ul").removeClass("dropedunderul"); });
    $(this).find("ul").mouseover(function(){$(this).addClass("dropedunderul");});
    $(this).find("ul").mouseleave(function(){$(this).removeClass("dropedunderul");});

});

and css

.t50 {-webkit-transition: all 0.5s; -moz-transition: all 0.5s; -ms-transition: all 0.5s; -o-transition: all 0.5s; transition: all 0.5s;}
.dropedmainnav {display:none; padding:15px 0 10px 0; overflow:hidden;width:500px; }
.dropedunderul {display:table !important; min-height:150px; border-top:2px solid transparent; z-index:2; }
.main-nav ul li {position:relative;}
.main-nav ul li ul {position:absolute;background-color:rgba(255,255,255,0); overflow:visible;}
.main-nav ul li ul li {float:none !important; text-align:left !important;}

I think that's about it for relevant styles. I've also tried adding transition specifically to .dropedmainnav, .dropedunderul and i've tried using inline styles.

I'm not sure what you're doing with the $.each(), wouldn't it be much simpler to call a function on each mouse enter/leave event and have it fade in/out:

$(".main-nav ul li").mouseenter(function() {
      $(this).children('dropedmainnav').fadeIn(500);
     }
 }).mouseleave(function() {
      $(this).children('dropedmainnav').fadeOut(500);  
     }
 });

Not sure what exactly you're trying to achieve, but you can certainly get rid of all this jQuery fuss. All animations and mouse-over effects can be done with just CSS (using :hover ). Try the snippet below and feel free to comment if that is not related to what you want.

 body { font-family: arial; } .onhover {-webkit-transition: all 0.5s; -moz-transition: all 0.5s; -ms-transition: all 0.5s; -o-transition: all 0.5s; transition: all 0.5s;} li .onhover { height: 0; width: 200px; overflow: hidden; border: 1px solid transparent; } li:hover .onhover { height: 20px; border-color: #ccc; } 
 <div> <ul> <li> <div>Item 1</div> <div class="onhover">You are hovering item 1</div> </li> <li> <div>Item 2</div> <div class="onhover">You are hovering item 2</div> </li> </ul> </div> 

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM