jquery - 切换jquery的过渡效果

标签 jquery css transition

我正在尝试使用 jquery 切换创建一个隐藏菜单。 我有一个带有单击事件的按钮和一个带有切换事件的类菜单。 我想在菜单上进行 CSS 过渡,或者显示效果。

我有一个侧边栏,我希望菜单从左到右显示,并带有过渡效果或其他效果。

$('.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>

提前致谢

最佳答案

如果我理解正确的话,您希望从左到右为菜单设置动画。

您可以在菜单上切换类并添加一些过渡和样式。 toggle()display:none 更改为 display:block,这是一个不可动画的属性。

使用toggleClass()来...好吧...切换:)/更改菜单上的类并设置这些类的样式。

因此,请使用 left:-100%left:200px (如果这是您想要放置菜单的位置)并结合过渡或不透明度。

见下文

$('.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>

关于jquery - 切换jquery的过渡效果,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50874992/

相关文章:

javascript - 为每个 img 分配一个不同的随机类

javascript - 如何在 onclick 方法后更改 div 的 CSS 样式

java - 应用 TranslateAnimation 时 ImageView 消失

css - 带有剪辑路径、动画比例或宽度的 svg 图像

javascript - 如何使用 jQuery 只允许定义的字符作为输入?

javascript - 动态更改指向箭头以指向点击源

javascript - 如何使用变量获取元素的 CSS 信息?

html - 侧边栏和内容的 div 高度相同

CSS - 将鼠标悬停在 li 元素(下拉菜单)上时执行转换

javascript - 让div不断随机移动