新闻中心
实现平滑滑出动画效果:优化页面元素过渡

本文旨在解决在网页中实现平滑滑出动画时可能出现的白色间隙问题。通过分析问题根源,提供了三种解决方案:利用`position: sticky`属性、使用css transitions以及web animations api。重点在于确保动画同步,避免视觉上的不流畅感,从而提升用户体验。
在网页开发中,经常需要实现一些动画效果来提升用户体验。其中,滑出动画是一种常见的交互方式,例如在页面顶部显示一个通知面板,用户点击“知道了”按钮后,面板滑出并消失,同时页面主体向上移动以填充面板留下的空间。然而,在实现这种动画时,有时会遇到动画不够平滑,出现白色间隙的问题。本文将深入探讨这个问题,并提供多种解决方案。 ### 问题分析 造成动画不平滑的原因通常在于: 1. **元素定位方式不当:** 使用`position: fixed`虽然能使元素固定在屏幕某个位置,但在不同屏幕尺寸下可能导致元素重叠或出现间隙。 2. **动画不同步:** 如果滑出面板和页面主体向上移动的动画距离和时间不一致,就会出现视觉上的不协调,导致动画不流畅。 为了解决这些问题,我们需要采用更合适的定位方式和更精确的动画控制。 ### 解决方案一:利用 `position: sticky` `position: sticky` 是一种结合了 `position: relative` 和 `position: fixed` 特性的定位方式。当元素在视口中时,它的行为类似于 `position: relative`;当元素滚动到特定位置时,它的行为则类似于 `position: fixed`。 **优点:** * 无需J*aScript计算位置,简化代码。 * 避免了`position: fixed`可能导致的元素重叠问题。 * 动画效果自然流畅,减少视觉抖动。 **代码示例:** ```htmlBy accessing and using this website, you acknowledge that you h*e read and understand our Cookie Policy, Privacy Policy, and our Terms of Service.
Hello! I'm Dylan Anderton
Consult, Design, and Develop Websites
H*e something great in mind? Feel free to contact me.
I'll help you to make it happen.
.panel{
z-index: 1;
position: sticky;
top: 0;
transition:
margin-bottom 1s,
transform 1s;
}
.hero {
position: relative;
}
*{
margin:0;
padding:0;
}
html {--smokeGrey:#eee}
.notif-panel{
display:grid;
grid-template-columns: 1fr;
grid-template-rows: 1fr;
grid-template-areas: "panel-content";
}
.panel-content{
display:flex;
justify-content:center;
align-items:center;
background-color:var(--smokeGrey);
}
.panel-text{
display:inline;
}
.cookie, .privacy, .tos{
text-decoration:none;
}
.panel-button{
display:inline;
padding: 10px 16px;
background-color: #007bc1;
color: white;
border: none;
border-radius: 2px;
}
.panel-button:hover{
background-image: linear-gradient(rgba(0, 0, 0, 0.1) 0 0);
}
@media(max-width:675px){
.notif-panel{
height: 10%;
}
.panel-content{
padding: 0 5px;
}
.panel-text{
padding-right: 15px;
}
.panel-button{
font-size: 17px;
}
}
@media(max-width:605px){
.notif-panel{
height: 15%;
}
.panel-content{
align-items: left;
display: block;
padding: 10px;
font-size: 18px;
}
.panel-text{
padding-bottom: 10px;
display: block;
}
.panel-button{
font-size: 17px;
}
}
.main-page{
width: 100%;
}
.hero{
display: flex;
justify-content: center;
align-items: center;
background-image: linear-gradient(rgba(0,74,117,.52),rgba(0,74,117,.52)), url('assets/work-desk__dustin-lee.jpg');
height: 600px;
}
.logo{
height: 50px;
width: 50px;
position: absolute;
left: 30px;
top: 25px;
}
.hero-content{
margin:0;
text-align: center;
color: white;
}
.name{
font-size: 30px;
}
.contact-text{
margin-top: 8px;
padding-bottom: 25px;
}
.contact-button{
font-weight: bold;
color: white;
background-color: transparent;
border: white 2px solid;
padding: 12px 15px;
border-radius: 2px;
}
.contact-button:hover{
color: var(--blue);
background-color: white;
}const panel = document.getElementById('panel');
const page = document.getElementById('main-page');
function closePanel(){
const {bottom} = panel.getBoundingClientRect();
panel.style.marginBottom = 0 + "px";
panel.addEventListener("transitionend", () => panel.replaceWith());
// marginBottom shrinks the element;
// translateY() slides it out.
setTimeout(() => {
panel.style.marginBottom = `${-bottom}px`;
panel.style.transform = `translateY(${-bottom}px)`;
}, 0);
}注意事项:
- position: sticky 需要指定 top、right、bottom 或 left 属性,才能生效。
- 部分旧版本浏览器可能不支持 position: sticky,需要进行兼容性处理。
解决方案二:使用 CSS Transitions
CSS Transitions 允许您在 CSS 属性值发生变化时,平滑地过渡到新的值。通过精确控制滑出面板和页面主体的 top 属性,可以实现同步的动画效果。
代码示例:
<div class="panel" id="panel">
<div class="notif-panel" id="notifPanel">
<div class="panel-content">
<p class="panel-text">By accessing and using this website, you acknowledge that you h*e read and
<span class="brCust" id="brCust"></span>understand our
<a class="cookie" href="#">Cookie Policy</a>,
<a class="privacy" href="#">Privacy Policy</a>, and our
<a class="tos" href="#">Terms of Service</a>.
</p>
<button class="panel-button" onclick="closePanel()">Got it</button>
</div>
</div>
</div>
<div class="main-page" id="main-page">
<section class="hero" id="hero">
@@##@@
<div class="hero-content">
<p class="name">Hello! I'm Dylan Anderton</p>
<h2 class="slogan">Consult, Design, and Develop Websites</h2>
<p class="contact-text">H*e something great in mind? Feel free to contact me.<br> I'll help you to make it happen.</p>
<button class="contact-button">LET'S MAKE CONTACT</button>
</div>
</section>
</div>/* I had to move position:fixed to this top-level element */
.panel{
z-index: 1;
position: fixed;
}
*{
margin:0;
padding:0;
}
html {--smokeGrey:#eee}
.notif-panel{
display:grid;
grid-template-columns: 1fr;
grid-template-rows: 1fr;
grid-template-areas: "panel-content";
}
.panel-content{
display:flex;
justify-content:center;
align-items:center;
background-color:var(--smokeGrey);
}
.panel-text{
display:inline;
}
.cookie, .privacy, .tos{
text-decoration:none;
}
.panel-button{
display:inline;
padding: 10px 16px;
background-color: #007bc1;
color: white;
border: none;
border-radius: 2px;
}
.panel-button:hover{
background-image: linear-gradient(rgba(0, 0, 0, 0.1) 0 0);
}
@media(max-width:675px){
.notif-panel{
height: 10%;
}
.panel-content{
padding: 0 5px;
}
.panel-text{
padding-right: 15px;
}
.panel-button{
font-size: 17px;
}
}
@media(max-width:605px){
.notif-panel{
height: 15%;
}
.panel-content{
align-items: left;
display: block;
padding: 10px;
font-size: 18px;
}
.panel-text{
padding-bottom: 10px;
display: block;
}
.panel-button{
font-size: 17px;
}
}
.main-page{
position: absolute;
width: 100%;
}
.hero{
display: flex;
justify-content: center;
align-items: center;
background-image: linear-gradient(rgba(0,74,117,.52),rgba(0,74,117,.52)), url('assets/work-desk__dustin-lee.jpg');
height: 600px;
}
.logo{
height: 50px;
width: 50px;
position: absolute;
left: 30px;
top: 25px;
}
.hero-content{
margin:0;
text-align: center;
color: white;
}
.name{
font-size: 30px;
}
.contact-text{
margin-top: 8px;
padding-bottom: 25px;
}
.contact-button{
font-weight: bold;
color: white;
background-color: transparent;
border: white 2px solid;
padding: 12px 15px;
border-radius: 2px;
}
.contact-button:hover{
color: var(--blue);
background-color: white;
}const panel = document.getElementById('panel');
const page = document.getElementById('main-page');
function closePanel(){
const rectPanel = panel.getBoundingClientRect();
panel.style.top = 0;
page.style.top = rectPanel.bottom + "px";
panel.style.transition = "top 1s ease-in-out";
page.style.transition = "top 1s ease-in-out";
// Don't forget to remove it from the DOM
panel.addEventListener("transitionend", () => panel.replaceWith());
setTimeout(() => {
panel.style.top = -rectPanel.bottom + "px";
page.style.top = 0;
}, 0);
}代码解释:
- 获取面板高度: 使用 panel.getBoundingClientRect() 获取面板的尺寸和位置信息。
-
设置初始位置:
将面板的 top 属性设置为 0,并将页面主体的 top 属性设置为面板的高度,使其紧贴面板下方。 - 添加过渡效果: 为面板和页面主体添加 transition 属性,指定过渡的属性(top)、持续时间(1s)和缓动函数(ease-in-out)。
- 触发动画: 使用 setTimeout 延迟一段时间后,将面板的 top 属性设置为 -rectPanel.bottom + "px",使其向上滑出,同时将页面主体的 top 属性设置为 0,使其向上移动填充面板留下的空间。
- 移除DOM元素: 在动画结束后,将面板从DOM中移除,避免影响页面布局。
注意事项:
- 确保面板和页面主体的过渡时间和缓动函数一致,以实现同步的动画效果。
- 使用 setTimeout 延迟触发动画,可以避免浏览器优化导致的动画卡顿。
解决方案三:使用 Web Animations API
Web Animations API 提供了更强大的动画控制能力,允许您使用 J*aScript 创建复杂的动画效果。
AI Surge Cloud
低代码数据分析平台,帮助企业快速交付深度数据
87
查看详情
代码示例:
<div class="panel" id="panel">
<div class="notif-panel" id="notifPanel">
<div class="panel-content">
<p class="panel-text">By accessing and using this website, you acknowledge that you h*e read and
<span class="brCust" id="brCust"></span>understand our
<a class="cookie" href="#">Cookie Policy</a>,
<a class="privacy" href="#">Privacy Policy</a>, and our
<a class="tos" href="#">Terms of Service</a>.
</p>
<button class="panel-button" onclick="closePanel()">Got it</button>
</div>
</div>
</div>
<div class="main-page" id="main-page">
<section class="hero" id="hero">
@@##@@
<div class="hero-content">
<p class="name">Hello! I'm Dylan Anderton</p>
<h2 class="slogan">Consult, Design, and Develop Websites</h2>
<p class="contact-text">H*e something great in mind? Feel free to contact me.<br> I'll help you to make it happen.</p>
<button class="contact-button">LET'S MAKE CONTACT</button>
</div>
</section>
</div>/* I had to move position:fixed to this top-level element */
.panel{
z-index: 1;
position: fixed;
}
*{
margin:0;
padding:0;
}
html {--smokeGrey:#eee}
.notif-panel{
display:grid;
grid-template-columns: 1fr;
grid-template-rows: 1fr;
grid-template-areas: "panel-content";
}
.panel-content{
display:flex;
justify-content:center;
align-items:center;
background-color:var(--smokeGrey);
}
.panel-text{
display:inline;
}
.cookie, .privacy, .tos{
text-decoration:none;
}
.panel-button{
display:inline;
padding: 10px 16px;
background-color: #007bc1;
color: white;
border: none;
border-radius: 2px;
}
.panel-button:hover{
background-image: linear-gradient(rgba(0, 0, 0, 0.1) 0 0);
}
@media(max-width:675px){
.notif-panel{
height: 10%;
}
.panel-content{
padding: 0 5px;
}
.panel-text{
padding-right: 15px;
}
.panel-button{
font-size: 17px;
}
}
@media(max-width:605px){
.notif-panel{
height: 15%;
}
.panel-content{
align-items: left;
display: block;
padding: 10px;
font-size: 18px;
}
.panel-text{
padding-bottom: 10px;
display: block;
}
.panel-button{
font-size: 17px;
}
}
.main-page{
position: absolute;
width: 100%;
}
.hero{
display: flex;
justify-content: center;
align-items: center;
background-image: linear-gradient(rgba(0,74,117,.52),rgba(0,74,117,.52)), url('assets/work-desk__dustin-lee.jpg');
height: 600px;
}
.logo{
height: 50px;
width: 50px;
position: absolute;
left: 30px;
top: 25px;
}
.hero-content{
margin:0;
text-align: center;
color: white;
}
.name{
font-size: 30px;
}
.contact-text{
margin-top: 8px;
padding-bottom: 25px;
}
.contact-button{
font-weight: bold;
color: white;
background-color: transparent;
border: white 2px solid;
padding: 12px 15px;
border-radius: 2px;
}
.contact-button:hover{
color: var(--blue);
background-color: white;
}const panel = document.getElementById('panel');
const page = document.getElementById('main-page');
function closePanel(){
const {bottom} = panel.getBoundingClientRect();
const duration = 1000; // ms
panel.animate(
{ top: [0, `${-bottom}px`] },
{ duration }
).finished.then(() => {
panel.replaceWith(); // Don't forget to remove element from DOM!
});
page.animate(
{ top: [`${bottom}px`, 0] },
{ duration }
);
}代码解释:
- 获取面板高度: 与 CSS Transitions 方案相同,首先获取面板的高度。
- 创建动画: 使用 panel.animate() 和 page.animate() 创建动画对象,指定动画的属性(top)、起始值和结束值,以及动画的持续时间。
- 移除DOM元素: 使用 finished.then() 监听面板动画的结束,并在动画结束后将面板从DOM中移除。
注意事项:
- Web Animations API 提供了更多的动画控制选项,例如缓动函数、循环次数、延迟等。
- 可以使用 keyframe format 指定属性值在不同时间点的变化。
总结
本文介绍了三种解决滑出动画不平滑问题的方法:使用 position: sticky、CSS Transitions 和 Web Animations API。每种方法都有其优缺点,您可以根据实际需求选择最合适的方案。
- position: sticky 最简单易用,但可能存在兼容性问题。
- CSS Transitions 适用于简单的动画效果,代码简洁易懂。
- Web Animations API 提供了更强大的动画控制能力,适用于复杂的动画效果。
无论选择哪种方案,都需要确保动画同步,避免视觉上的不协调,从而提升用户体验。此外,还应注意代码的兼容性,确保在不同浏览器下都能正常运行。


以上就是实现平滑滑出动画效果:优化页面元素过渡的详细内容,更多请关注其它相关文章!
# 使其
# 聊城seo排名工具
# 成交量网站建设
# 坂田网络推广和网站优化
# 网站为什么要做广告推广
# 深圳快速网站推广哪里好
# 海口网站建设发布
# 建设网站展示
# 赤峰银川网站推广
# 外贸企业网站建设找哪家
# 宁波seo关键词策划
# 类似于
# 三种
# 适用于
# 单选框
# 是一种
# css
# 移除
# 表单
# 设置为
# 滑出
# ai
# access
# edge
# app
# 浏览器
# cookie
# go
# html
# java
# javascript
相关栏目:
【
科技资讯46185 】
【
网络学院92790 】
相关推荐:
虫虫漫画精品漫画官网_虫虫漫画精品漫画官网进入精品漫画
Go语言HTML解析:利用Goquery精准获取指定元素内容
免费抖音短视频入口_抖音网页版短视频免费通道
TypeScript/J*aScript:高效查找数组中首个唯一ID对象
J*a里如何使用N*igableMap进行导航操作_可导航Map操作技巧解析
《噬血代码2》新预告片发布 展示游戏剧情
魅族17怎样用浏览器译外语网页_iPhone魅族17浏览器译外语网页【即时翻译】
CSS如何设置hover状态颜色_hover伪类调整背景或文字颜色
在Runstone环境中高效处理TasteDive API的JSON数据
抓大鹅无需下载版 抓大鹅秒玩版入口
CSS Box Model与弹性按钮:维持布局稳定的动画实践
Composer的 "conflict" 字段有什么用_如何声明不兼容的包以避免依赖冲突
提升Kafka消费者健壮性:会话超时处理与消息处理语义
win11 arm版怎么安装 M1/M2 Mac虚拟机安装ARM win11的方法
Typer应用中动态命令行参数的解析与处理
React项目中导航栏Logo自适应布局:避免裁剪与布局溢出
解决J*aScript中重复选择项的确认对话框显示问题
c++项目目录结构应该如何组织_c++工程化项目结构规范
mysql如何设置表访问权限_mysql表访问权限配置
c++如何使用TBB库进行任务并行_c++ Intel线程构建模块
win11专注助手在哪 Win11免打扰模式设置与自动化规则【指南】
JUnit5/Mockito:优雅测试内部依赖与异常处理的实践
Spring Boot嵌入式服务器与J*a EE:功能支持深度解析
lar*el怎么安全地存储和获取配置文件中的敏感信息_lar*el敏感信息安全存储方法
怎么在mac上运行html代码_mac运行html代码方法【指南】
QQ邮箱在线登录平台 QQ邮箱个人邮箱网页版入口
1688商家版怎样分析买家画像精准供货_1688商家版分析买家画像精准供货【供货策略】
UE5.7引擎表现爆炸优化无敌!5090跑4K稳定60FPS
CSS布局:解决全屏元素100%尺寸与外边距导致的页面溢出问题
解决Flask中Quill编辑器内容提交失败及TypeError的指南
支付宝如何设置安全保护_支付宝安全设置的全面教程
微信语音通话掉线如何解决 微信语音通话稳定优化方法
正确连接J*aScript到HTML实现可点击图片与自定义事件处理
J*a递归快速排序中静态变量导致数据累积的陷阱与解决方案
12306选座如何查看座位示意图_12306座位示意图解读与使用
Descript怎样用AI剪辑自动去噪_Descript用AI剪辑自动去噪【自动降噪】
动漫花园资源网使用步骤_动漫花园资源网下载流程
俄罗斯Yandex搜索引擎入口_Yandex官网免登录一键访问
python3时间如何用calendar输出?
Golang如何实现Web文件静态资源服务器_Golang静态资源服务器开发与实践
支付宝如何管理隐私设置_支付宝隐私保护的配置技巧
C++如何生成随机数_C++ random库使用方法与范围设置
poki网页游戏推荐_poki免费游戏平台入口
高德地图家和公司地址在哪设置 高德地图通勤路线设置方法【超详细】
192.168.1.1管理中心入口 192.168.1.1路由器网页设置平台
cad如何更改注释性对象的比例_cad注释性比例调整方法
C++编译期如何执行复杂计算_C++模板元编程(TMP)技巧与应用
使用 Pandas 高效处理 .dat 文件:数据清洗与数值计算实战
C++如何检测键盘输入_C++ _kbhit与_getch函数非阻塞输入
Golang如何通过reflect操作map_Golang reflect map操作与遍历技巧


2025-10-19
浏览次数:次
返回列表
将面板的 top 属性设置为 0,并将页面主体的 top 属性设置为面板的高度,使其紧贴面板下方。