使用css实现打开抽屉效果(css过渡动画)

方法一、使用高度方式,高度从0到展示进行过渡

html:

cpp 复制代码
 <div class="container">
       <div class="menu">菜单</div>
       <div class="box">
       </div>
    </div>

css:

cpp 复制代码
  .box {
        width: 400px;
        height: 0;
        background-color: red;
        transition: all 1s ease;
      }
      .menu{
        height: 50px;
      }
      .menu:hover ~ .box{
        height: 300px;
      }

方法二、使用定位方法,相对较麻烦

HTML代码:

cpp 复制代码
 <div class="container">
       <div class="menu">菜单</div>
       <div class="box">
          <div class="drawer"></div>
       </div>
    </div>

css

cpp 复制代码
.container{
        background-color: #fff;
      }
      .box{
        position: relative;
        overflow: hidden;
      }
       .drawer {
        width: 400px;
        height: 300px;
        background-color: red;
        overflow: hidden;
        position: absolute;
        top: -300px;
        transition: all 1s ease;
      }
      .menu{
        height: 50px;
      }
      .menu:hover ~ .box{ /* 悬浮时,在此处绘制容器宽高,可以避免在抽屉未打开时,占用高度 */
        width: 400px;
        height: 300px;
      }
      .menu:hover ~ .box .drawer{
        top: 0px;
      }
相关推荐
用户059540174462 天前
AI Agent记忆测试踩坑实录:Mock骗了我一周,Mem0+pytest一招破局
前端·css
Darling噜啦啦2 天前
CSS 3D 变换与 Flex 布局实战:从零打造旋转立方体
前端·css
用户059540174463 天前
把待办应用从Electron换成Tauri,内存占用狂降90%,打包体积仅5MB
前端·css
小月土星3 天前
CSS 3D 从入门到炫技:手把手教你写一个旋转立方体
前端·css
xingpanvip3 天前
星盘接口开发文档:本命盘接口指南
android·开发语言·css·php·lua
HjhIron3 天前
CSS 3D 世界:从盒子模型到三维空间动画
javascript·css
参宿73 天前
CSS 悬挂空白与选区溢出
前端·css
黄敬峰3 天前
纯 CSS3 打造 3D 旋转魔方:从文档流、Flex 布局到空间变换的硬核复盘
css
JieE2123 天前
手把手带你用纯 CSS 实现一个 3D 旋转魔方,这些前端基础你能打几分?
前端·css·html
JYeontu3 天前
开箱流水加载动画
前端·javascript·css