jquery - 如何制作光滑的 block 状外观?

标签 jquery html css

CSS

.sidenavv {
display: none;
height: 100%;
position: fixed;
z-index: 1;
top: 0;
left: 0;
background-color: #111;
overflow-x: hidden;
-webkit-transition: 0.5s;
transition: width 0.5s;
padding-top: 60px;
text-align: center; }

.sidenavv.is-active{
  display:block!important;
  -webkit-transition: width 1s;
  -ms-transition: width 1s;
  -o-transition: width 1s;
  -moz-transition: width 1s;
  transition: width 1s;
}

JQuery

const $menu = $('#mySidenavv');

 $(document).mouseup(e => {
  if (!$menu.is(e.target) // if the target of the click isn't the container...
  && $menu.has(e.target).length === 0) // ... nor a descendant of the container
    {
         $menu.removeClass('is-active');
         $menu.removeClass('width');
    }
  });
    $('#burger-openn').on('click', () => {
        $menu.toggleClass('width');
        $('.width').css('width', '83%');
        $menu.toggleClass('is-active');
    });

HTML

    <div id="mySidenavv" class="sidenavv" style="width: 0px;">
         <nav id="site-navigation-mobile-blog">
            <ul>
            </ul>
         </nav><!-- #site-navigation -->
    </div>
    <span id="burger-openn" class="openn">☰</span>

目前,通过点击汉堡,这个 block 变成了一个显示 block ,宽度为 83%。我需要他顺利出现在这 83%,然后点击屏幕上的空白点 - 他也顺利关闭

最佳答案

你不能为 display:none 元素设置动画,因为元素不在 DOM 中

您可以使用下面的 transform:translate(x) 示例制作动画。

const $menu = $('#mySidenavv');

$(document).mouseup(e => {
  if (!$menu.is(e.target) // if the target of the click isn't the container...
      && $menu.has(e.target).length === 0) // ... nor a descendant of the container
  {
    $menu.removeClass('is-active');
  }
});
$('#burger-openn').on('click', () => {
  $menu.toggleClass('is-active');
});
.sidenavv {
  height: 100%;
  position: fixed;
  z-index: 1;
  top: 0;
  left: 0;
  background-color: #111;
  overflow-x: hidden;
  width: 83%;
  transform: translateX(-100%);
  -webkit-transition: transform 1s;
  -ms-transition: transform 1s;
  -o-transition: transform 1s;
  -moz-transition: transform 1s;
  transition: transform 1s;
  padding-top: 60px;
  text-align: center; }

.sidenavv.is-active{
  transform: translateX(0%);
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<div id="mySidenavv" class="sidenavv">
  <nav id="site-navigation-mobile-blog">
    <ul>
    </ul>
  </nav><!-- #site-navigation -->
</div>
<span id="burger-openn" class="openn">☰</span>

关于jquery - 如何制作光滑的 block 状外观?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59640512/

相关文章:

c# - 从 .aspx 中永远不会在 aspx.cs 中调用 delete 函数

javascript - 使用 dataTable.js Bootstrap 表排序、过滤、分页

javascript - slicknav 没有显示

javascript - 在 Kendo 编辑器工具中使用 jquery 中的类创建按钮单击事件

javascript - setTimeout 到另一个里面的 JS 函数

javascript - 为什么 jQuery SlideToggle 在最小化/最大化 div 时不渲染动画/过渡?

css - 如何从 userContent.css 的正则表达式中排除两个术语?

jquery - 当内容到达顶部时,删除 Bootstrap 粘性导航栏上的故障

javascript - 如何使用 jquery 验证所需的标题字段集?

html - dia 导出为 html