实现高度的过渡效果

前言

在实现鼠标移入某个元素,展开某个列表的功能时,可能列表的长度是不固定的,就无法通过写死高度去实现过渡效果。

公共样式

html 复制代码
<div class="button">
  <div class="text">Hover me</div>
  <div class="list">
    <div class="content">
      Change, a constant and inevitable force in our lives, often brings a mix
      of excitement, fear, and uncertainty. It's the engine of progress,
      pushing us out of our comfort zones and into new territories. While it
      can be daunting, embracing change can lead to personal growth and new
      opportunities.
    </div>
  </div>
</div>
css 复制代码
.button {
  padding: 10px;
  width: fit-content;
  background: aquamarine;
  border-radius: 10px;
}

 .button:hover .list {
   visibility: visible;
}

 .list {
   position: absolute;
   visibility: hidden;
   width: 200px;
   background: antiquewhite;
   border-radius: 10px;
}

尝试 height: auto

那如果 hover 后把列表的高度改为 auto 呢,代码如下:

css 复制代码
 .button:hover .list {
   height: auto;
}

 .list {
   overflow: hidden;
   height: 0;
   transition: 0.3s;
}

实现效果:

可以看到没出现过渡效果,这是因为过渡 transition 只对于属性值是数值的才有效,显然 auto 并不是数值类型。

尝试 max-height

height 改为 max-height

css 复制代码
.button:hover .list {
  max-height: 800px;
}

.list {
  overflow: hidden;
  max-height: 0;
  transition: 0.3s;
}

过渡的效果和预想的不太一样,速度有点快。因为过渡是按照设置的 800px 去计算的。

尝试 transform: scale()

css 复制代码
.button:hover .list {
  transform: scaleY(1);
}

.list {
  overflow: hidden;
  transform: scaleY(0);
  transform-origin: top;
  transition: 0.3s;
}

可以看到里面的内容也被压缩了,效果不太好。

尝试 grid 布局

css 复制代码
.button:hover .list {
  grid-template-rows: 1fr;
}

.list {
  display: grid;
  grid-template-rows: 0fr;
  transition: 0.3s;
}

.list .content {
  overflow: hidden;
}

使用grid可以完美的实现想要的效果,虽然主流的浏览器都已经支持,但在实际使用中还是要注意下兼容性问题。

使用 js

js 复制代码
const button = document.querySelector(".button");
const list = document.querySelector(".list");

button.addEventListener("mouseover", () => {
  list.style.height = "auto";
  const height = list.clientHeight;
  list.style.transition = "0.3s";
  list.style.height = 0;
  list.clientHeight; // 强制回流,让 list 高度变为 0
  list.style.height = `${height}px`;
});
button.addEventListener("mouseout", () => {
  list.style.height = 0;
});

实现原理: FLIP

实现思路:

  1. 鼠标移入按钮时,先把list的高度改为auto,让其自动计算高度。
  2. 记录计算出的高度,这里使用了clientHeight会使浏览器回流,虽然此时浏览器还没有重绘,但可以获取到listheightauto时的高度。
  3. 加入过渡效果,这里可以写在css里。设置list高度为0
  4. 读取listclientHeight使浏览器回流,让list的实际高度为0
  5. 设置list的高度为之前记录的值。
相关推荐
undefined&&懒洋洋9 分钟前
Web和UE5像素流送、通信教程
前端·ue5
大前端爱好者2 小时前
React 19 新特性详解
前端
随云6322 小时前
WebGL编程指南之着色器语言GLSL ES(入门GLSL ES这篇就够了)
前端·webgl
随云6322 小时前
WebGL编程指南之进入三维世界
前端·webgl
寻找09之夏3 小时前
【Vue3实战】:用导航守卫拦截未保存的编辑,提升用户体验
前端·vue.js
多多米10054 小时前
初学Vue(2)
前端·javascript·vue.js
柏箱4 小时前
PHP基本语法总结
开发语言·前端·html·php
新缸中之脑4 小时前
Llama 3.2 安卓手机安装教程
前端·人工智能·算法
hmz8564 小时前
最新网课搜题答案查询小程序源码/题库多接口微信小程序源码+自带流量主
前端·微信小程序·小程序
看到请催我学习4 小时前
内存缓存和硬盘缓存
开发语言·前端·javascript·vue.js·缓存·ecmascript