前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >CSS实现从下至上弹出的抽屉动画

CSS实现从下至上弹出的抽屉动画

作者头像
Jensen_97
发布2023-07-19 17:20:28
7490
发布2023-07-19 17:20:28
举报
文章被收录于专栏:技术客栈

从下至上展开抽屉动画

代码语言:javascript
复制
<!DOCTYPE html>
<html>

<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="initial-scale=1.0, maximum-scale=1.0, user-scalable=no" />
  <title></title>
  <style>
    .container {
      margin: auto;
      top: 460px;
      width: 320px;
      height: 30px;
      position: relative;
      background-color: rgba(0, 0, 0, 0.4);
      overflow-y: auto;
      scroll-behavior: smooth;
      border-radius: 20px;
    }

    /* 隐藏滚动条 */
    ::-webkit-scrollbar {
      display: none;
    }

    .expend {
      animation: expend ease 1s forwards;
    }

    .close-container {
      animation: no-expend ease 1s forwards;
    }

    @keyframes expend {
      from {
        top: 460px;
        height: 30px;
      }

      to {
        height: 330px;
        top: 160px;
      }
    }

    @keyframes no-expend {
      from {
        height: 330px;
        top: 160px;
      }

      to {
        top: 460px;
        height: 30px;
      }
    }

    .close {
      color: aliceblue;
      right: 0;
      margin: 5px 10px;
      position: absolute;
    }

    .title {
      color: aliceblue;
      height: 30px;
      line-height: 30px;
      margin: 0 10px;
      position: absolute;
    }

    .list {
      display: flex;
      /* 子元素换行 */
      flex-wrap: wrap;
      white-space: nowrap;
      padding-top: 24px;
    }

    .item {
      width: 80px;
      height: 80px;
      margin: 40px;
      display: flex;
      flex-direction: column;
      justify-content: space-around;
      align-items: center;
    }

    .item-img {
      width: 40px;
      height: 40px;
    }
  </style>
</head>

<body>

  <div class='container' id='container'>
    <div class='title'>抽屉标题</div>
    <div class='close' onclick='closeHandle()'>↑</div>
    <div class='list'>
      <div class='item'>
        <img src="./Icon_template.png" class="item-img">
        <span class='item-text'>内容1</span>
      </div>
      <div class='item'>
        <img src="./Icon_template.png" class="item-img">
        <span class='item-text'>内容2</span>
      </div>
      <div class='item'>
        <img src="./Icon_template.png" class="item-img">
        <span class='item-text'>内容3</span>
      </div>
      <div class='item'>
        <img src="./Icon_template.png" class="item-img">
        <span class='item-text'>内容4</span>
      </div>
      <div class='item'>
        <img src="./Icon_template.png" class="item-img">
        <span class='item-text'>内容4</span>
      </div>
    </div>
  </div>

  <script>
    const closeHandle = () => {
      console.log('关闭和展开');
      const dom = document.getElementById('container');
      const closeDom = document.getElementsByClassName('close')[0];
      if (!dom.className.match(/(?:^|\s)expend(?!\S)/)) {
        dom.className = "container expend";
        setTimeout(() => {
          closeDom.innerText = 'X'
        }, 100)
      } else {
        dom.className = "container close-container";
        setTimeout(() => {
          closeDom.innerText = '↑'
        }, 100)
      }
    }
  </script>
</body>

</html>
本文参与 腾讯云自媒体同步曝光计划,分享自作者个人站点/博客。
如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • 从下至上展开抽屉动画
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档