使用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;
      }
相关推荐
labixiong35 分钟前
transition 写在 CSS 变量上为什么不生效?给变量补张「类型身份证」
前端·css·html
水月清辉4 小时前
CSS 常用语法详解
前端·css
言乐68 小时前
Python加速器2视频网页加速器
前端·javascript·css·python·音视频
今天AI了吗1 天前
2026年三大AI桌面智能体横评:Codex vs Hermes vs WorkBuddy
大数据·前端·css·人工智能·架构
HwJack201 天前
用 HML + CSS + JS 三段式写鸿蒙界面
javascript·css·harmonyos
豆约翰2 天前
css自制图书封面效果
前端·css
CoderYanger2 天前
前端基础——JavaScript(WebAPI)代码案例
java·开发语言·前端·javascript·css·前端框架·html5
EatFan3 天前
HTML前端利用flex解决input定位的问题
前端·css·html·css3
labixiong3 天前
告别scroll 监听:CSS 滚动驱动动画,主线程堵死也仍跟手
前端·css·html
King of fraud3 天前
HTML 页面的 CSS 选择器详解
前端·css·html