你真的会写侧边栏收起动画吗?

业务背景

侧边栏的展开与收起, 是非常常见的前端交互. 下面这段代码是个基础版的demo, 使用html+tailwind编写的

<button id="toggle-btn" type="button">toggle</button>
<div class="w-[500px] h-[300px] flex">
  <section class="flex-1 h-full bg-gray-500">
    <div class="text-lime-50">New Yorkers are facing the winter chill with less warmth this year as the city's most revered soup stand unexpectedly shutters, following a series of events that have left the community puzzled.</div>
  </section>
  <section id="section-right" class="w-[200px] h-full bg-gray-600 transition-all duration-300">
    <div class="text-lime-100">New Yorkers are facing the winter chill with less warmth this year as the city's most revered soup stand unexpectedly shutters, following a series of events that have left the community puzzled.</div>
  </section>
</div>

这段代码的视觉效果就是下面这样的. 通过点击左上角的toggle来进行宽度切换.

image-20240328195036875

为了实现toggle的效果, 我们+上一段js代码

const toggleBtn = document.querySelector('#toggle-btn');
const sectionRight = document.querySelector('#section-right');
toggleBtn.addEventListener('click', () => {
  if (sectionRight.style.width === '0px') {
    sectionRight.style.width = '200px';
  } else {
    sectionRight.style.width = '0';
  }
});

总结下, 使用两栏布局, 右侧固定200px, 左侧自适应, 点击toggle可以隐藏和展示右侧区域. 动画如下

问题

功能已经实现, 而且逻辑也非常简单. 但是却存在一个体验问题, 就是右侧区域在宽度变化至0时, 内容也受到了挤压. 虽然动画时间比较快, 但是仍然能够被人眼捕捉. 让我们放慢速度再看一次

解决方案

首先我们来分析为什么会产生这样的现象, 是因为右侧固定200px, 变成了0px, 又因为设置了transition, 所以宽度是逐渐从200px一点点减小到0的, 我相信找到这个原因对大家来说并不难. 那么我们怎么解决呢?

非常简单! 倒推结果, 想要内容不被压缩, 那么内容宽度一定要始终为200px, 但是这又和200px->0px的transition效果冲突了. 因此, 我们可以在外面再套一个容器, 外部容器起着宽度变化的任务. 那么他里面的容器宽度难道不会变吗? 答案是可以不变. 让外部容器成为relative, 而内部容器是绝对定位且一直贴着容器的left: 0且宽度和外部容器保持一致. 这样在动画执行的时候, 外部容器虽然宽度在变化, 但是内部容器的宽度始终没有变化, 且位置会跟随外部容器发生变化.

直接上代码

<section id="section-right" class="relative w-[200px] h-full bg-gray-600 transition-all" style="transition-duration: 3000ms;">
  <div class="absolute top-0 left-0 w-[200px] h-full">
    <div class="text-lime-100">New Yorkers are facing the winter chill with less warmth this year as the city's most revered soup stand unexpectedly shutters, following a series of events that have left the community puzzled.</div>
  </div>
</section>

我相信大家看到内容超出了容器, 就能理解是怎么一回事儿了. 此时在外部容器+上overflow-hidden即可.

坦白说, css不是我的强项, 也许有更好的办法, 你有什么其他的解决方案吗?


我是前夕, 专注于前端和成长, 希望我的内容可以帮助到你. 公众号: 前夕小课堂

image-20240403101717261

本文禁止转载!

好的,我可以为您提供相关的代码示例。首先,我们需要一个 HTML 结构作为侧边栏容器,以及一个按钮来触发展开和收起的操作。HTML 代码如下: ```html <div class="sidebar"> <!-- 左侧菜单内容 --> </div> <button class="toggle-btn">展开/收起</button> ``` 接下来,我们使用 CSS3 中的 transition 属性为侧边栏添加动画效果,并使用 transform 属性移动侧边栏的位置。同时,我们为按钮添加一个点击事件,通过 JavaScript 来切换侧边栏的展开和收起状态。完整的 CSS 和 JavaScript 代码如下: ```css .sidebar { position: fixed; top: 0; left: -200px; /* 初始位置在左侧屏幕外 */ width: 200px; height: 100%; background-color: #333; transition: transform 0.3s ease-in-out; } .sidebar.active { transform: translateX(200px); /* 展开时移动到屏幕内 */ } .toggle-btn { position: fixed; top: 20px; right: 20px; padding: 10px; background-color: #fff; border: none; border-radius: 50%; box-shadow: 0 0 10px rgba(0, 0, 0, 0.3); cursor: pointer; transition: transform 0.3s ease-in-out; } .toggle-btn.active { transform: rotate(180deg); /* 切换为收起图标 */ } ``` ```javascript const toggleBtn = document.querySelector('.toggle-btn'); const sidebar = document.querySelector('.sidebar'); toggleBtn.addEventListener('click', () => { sidebar.classList.toggle('active'); toggleBtn.classList.toggle('active'); }); ``` 这样,我们就完成了一个简单的侧边栏展开收起效果,希望能对您有所帮助。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值