Transition effect on toggle jquery

戏子无情 提交于 2021-01-28 01:26:36

问题


I am trying to to make a hidden menu with jquery toggle. I have a button with a click event and a class menu with a toggle event. I want to make a CSS transition on the menu, or an effect to appear.

I have a sidebar and I want to the menu to appear from left to right with a transition effect or something.

$('.menu__btn').click(function(){
  $('.menu').toggle();
})
.menu {
  background: #000;
  width: 240px;
  height: 100%;
  position:fixed;
  margin-left: 100px;
  display: none
  -webkit-box-shadow: inset 1px 0px 1px 0px rgba(232,232,232,1);
  -moz-box-shadow: inset 1px 0px 1px 0px rgba(232,232,232,1);
  box-shadow: inset 1px 0px 1px 0px rgba(232,232,232,1);
}

.menu__container {
  padding: 85px 10px 10px 10px;
  position: relative;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>

<div class="menu__btn">
  <button class="menu__icon">Button</button>
</div>
<div class="menu">
  <div class="menu__container">
    Some content
  </div>
</div>

Thanks in advance


回答1:


If i understand you correctly, you want to animate your menu from left to right.

You can toggle class on the menu and add some transitions and styling. toggle() changes from display:none to display:block , a property which is not animatable.

Use toggleClass() to ...well...toggle :)/change classes on the menu and style those classes.

So use left:-100% and left:200px ( if that is where you want to position the menu ) and together with transition or maybe opacity.

See below

$('.menu__btn').click(function(){
  $('.menu').toggleClass('show-menu');
})
.menu{
  background: #000;
  width: 240px;
  height: 100%;
  position:fixed;
  left:-100%;
 
  -webkit-box-shadow: inset 1px 0px 1px 0px rgba(232,232,232,1);
  -moz-box-shadow: inset 1px 0px 1px 0px rgba(232,232,232,1);
  box-shadow: inset 1px 0px 1px 0px rgba(232,232,232,1);
  transition:1s ease-out;
  opacity:0;
}
.menu__container {
  padding: 85px 10px 10px 10px;
  position: relative;
  }
  .show-menu {
     left:200px;
     opacity:1;
  }
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div class="menu__btn">
 <button class="menu__icon">Button</button>
</div>
<div class="menu">
   <div class="menu__container">
    Some content
   </div>
 </div>


来源:https://stackoverflow.com/questions/50874992/transition-effect-on-toggle-jquery

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