简体   繁体   English

addClass转换不起作用

[英]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. 制作一个下拉菜单,我需要一个隐藏的ul在悬停时缓慢显示。

Html of it 它的HTML

<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 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 和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. 我也试过将转换专门添加到.dropedmainnav,.dropedunderul,我尝试使用内联样式。

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: 我不确定你用$ .each()做了什么,在每个鼠标进入/离开事件上调用一个函数并让它淡入/淡出是不是更简单:

$(".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. 不确定你究竟想要实现什么,但你当然可以摆脱所有这些jQuery大惊小怪。 All animations and mouse-over effects can be done with just CSS (using :hover ). 所有动画和鼠标悬停效果都可以通过CSS(使用: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> 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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