使用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;
      }
相关推荐
软件技术NINI5 小时前
盗墓笔记html+css+js 2页
css·笔记·html
用户0595401744619 小时前
大模型对话记忆持久化踩坑实录:用 Playwright 自动化测了 300 次,终于揪出会话丢失的真凶
前端·css
林焱_RPAAI21 小时前
影刀RPA操作数据库进阶:事务处理与批量写入优化
css
牧艺1 天前
cos-design PhotoAlbum:用 CSS 3D 做一个「能翻页」的实体相册
前端·css·交互设计
muddjsv2 天前
CSS 盒模型进阶约束:极值尺寸、固有尺寸与外边距折叠
前端·css
布兰妮甜2 天前
暗黑模式一键切换完整方案(CSS 变量 + 本地存储)
javascript·css·web开发·用户体验·前端工程化
muddjsv2 天前
CSS 盒模型完全指南:content-box 与 border-box 尺寸计算原理
前端·css
米码收割机2 天前
【前端】html+css 甘肃天水旅游(源码+文档)【独一无二】
前端·css·html
米码收割机2 天前
【前端】html_css 沐光公益阅读馆(源码+文档)【独一无二】
前端·css·html
布兰妮甜2 天前
原子化 CSS 深度解析:Tailwind 原理、自定义配置、大型项目利弊
css·性能优化·tailwind·前端工程化·设计系统