-webkit-transform:translate3d(0,0,0)触发GPU加速,让网页动画更流畅

通过应用translate3d(0,0,0)等CSS3属性触发硬件加速,显著提升了Chrome浏览器中动画的流畅度,改进前后性能对比明显。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

前段时间,按照美拍的视频效果写了一个效果类似的网页版的动画。电脑上安装了三种浏览器:IE、Chrome、Firefox。分别作了测试,结果显示Chrome在这方面的渲染效果最差,经常出现卡顿现象,ff表现最好。一直百思不得其解,尤其是之前使用canvas标签做图片滤镜效果,chrome浏览器居然显示不了滤镜效果,然而其他浏览器均能正常显示,大谷歌你肿么了...对渲染多张图片和动画效果表示放弃了么...

昨天和高人聊天,说道这个情况,他说道了transform:translate3d(0,0,0)可以触发硬件加速,然后楼主今天就百度了一下这个CSS3提供的3D效果,果然个属性都会开启GPU硬件加速模式,从而让浏览器在渲染动画时从CPU转向GPU。

于是乎,楼主今天给文件里添加了如下代码:

[javascript]  view plain  copy
  1. <span style="white-space:pre">    </span>transform:translate3d(0,0,0);  
  2.     -webkit-transform:translate3d(0,0,0);  
  3.     -o-transform:translate3d(0,0,0);  
  4.     -moz-transform:translate3d(0,0,0);  
  5.     -webkit-backface-visibility:hidden;  
  6.     -webkit-perspective:1000;  
  7.     -moz-backface-visibility:hidden;  
  8.     -moz-perspective:1000;  
  9.     backface-visibility:hidden;  
  10.     perspective:1000;  
然后利用Chrome的developer Tools 中的Timeline工具,检测整个动画运行性能参数,为了做到最好的对比效果,先测试了改进后的版本,然后再测未改进版本的,以防止由于缓存来带的影响。先测试改进版的页面,图片会缓存,这样子再测未改进版本的,只能说对未改进版本的更有利,这样的情况下进行对比,反而更能说明问题,哈哈。

改进版本参数:




未改进参数:


未改进版本的fps(每秒传输帧数,值越高画面就越流畅)一半多的时间都是小于30fps,而改进后的动画fps基本都超过60fps,楼主自己去围观动画效果,也发现了明显的改进,Chrome浏览器的不流畅效果得到极大的改进。当然对于整个效果,楼主CSS3与jQ动画结合使用,jQ的动画效果比不上CSS3,所以等楼主进一步改进,用强大的CSS3来完成整个动画效果,应该就会棒棒哒~

<style> /* 使用唯一前缀避免样式冲突 */ .fp-carousel-module { position: relative; width: 100%; max-width: 80%; height: 500px; overflow: hidden; margin: 20px auto; perspective: 1200px; box-sizing: border-box; border-radius: 8px; box-shadow: 0 5px 15px rgba(0,0,0,0.1); } .fp-carousel-module .fp-carousel-container { position: relative; width: 100%; height: 100%; isolation: isolate; /* 创建新的层叠上下文 */ } .fp-carousel-module .fp-carousel-item { position: absolute; top: 50%; left: 50%; width: 60%; height: 80%; transform: translate(-50%, -50%); border-radius: 12px; box-shadow: 0 15px 35px rgba(0,0,0,0.25); transition: transform 0.7s cubic-bezier(0.25, 0.1, 0.25, 1), filter 0.7s ease, opacity 0.7s ease; z-index: 1; overflow: hidden; background: #fff; will-change: transform; cursor: pointer; } .fp-carousel-module .fp-carousel-item img { width: 100%; height: 100%; object-fit: cover; transition: transform 0.4s ease; } .fp-carousel-module .fp-description { position: absolute; bottom: 0; left: 0; right: 0; background: linear-gradient(transparent, rgba(0,0,0,0.8)); color: white; padding: 20px 15px 15px; transform: translateY(100%); transition: transform 0.5s ease-out; text-align: center; font-size: 16px; font-weight: 500; pointer-events: none; } /* 当前激活项样式 */ .fp-carousel-module .fp-carousel-item.fp-active { transform: translate(-50%, -50%) scale(1.15); z-index: 100; box-shadow: 0 25px 50px rgba(0,0,0,0.3); cursor: default; } .fp-carousel-module .fp-carousel-item.fp-active .fp-description { transform: translateY(0); } /* 堆叠项样式 */ .fp-carousel-module .fp-carousel-item.fp-prev { transform: translate(calc(-150% - 30px), -50%) scale(0.85); z-index: 20; opacity: 0.85; filter: blur(1px); } .fp-carousel-module .fp-carousel-item.fp-next { transform: translate(calc(50% + 30px), -50%) scale(0.85); z-index: 20; opacity: 0.85; filter: blur(1px); } .fp-carousel-module .fp-carousel-item.fp-prev-far { transform: translate(calc(-250% - 60px), -50%) scale(0.7); z-index: 15; opacity: 0.7; filter: blur(2px); } .fp-carousel-module .fp-carousel-item.fp-next-far { transform: translate(calc(150% + 60px), -50%) scale(0.7); z-index: 15; opacity: 0.7; filter: blur(2px); } .fp-carousel-module .fp-carousel-item.fp-hidden { opacity: 0; z-index: 0; transform: translate(calc(-50% + 300px), -50%) scale(0.5); } /* 悬停效果 */ .fp-carousel-module .fp-carousel-item.fp-prev::after, .fp-carousel-module .fp-carousel-item.fp-next::after, .fp-carousel-module .fp-carousel-item.fp-prev-far::after, .fp-carousel-module .fp-carousel-item.fp-next-far::after { content: ''; position: absolute; top: 0; left: 0; right: 0; bottom: 0; background: rgba(255,255,255,0.1); transition: background 0.3s ease; } .fp-carousel-module .fp-carousel-item.fp-prev:hover::after, .fp-carousel-module .fp-carousel-item.fp-next:hover::after, .fp-carousel-module .fp-carousel-item.fp-prev-far:hover::after, .fp-carousel-module .fp-carousel-item.fp-next-far:hover::after { background: rgba(255,255,255,0.3); } /* 导航按钮 */ .fp-carousel-module .fp-nav-btn { position: absolute; top: 0; height: 100%; width: 15%; z-index: 200; display: flex; align-items: center; justify-content: center; cursor: pointer; opacity: 0.5; transition: opacity 0.3s ease; } .fp-carousel-module .fp-nav-btn::before { content: ''; position: absolute; top: 0; bottom: 0; width: 100%; z-index: -1; } .fp-carousel-module .fp-nav-btn:hover { opacity: 1; } .fp-carousel-module .fp-nav-btn.fp-prev-btn { left: 0; } .fp-carousel-module .fp-nav-btn.fp-prev-btn::before { background: linear-gradient(90deg, rgba(0,0,0,0.4), transparent); } .fp-carousel-module .fp-nav-btn.fp-prev-btn .fp-arrow { transform: rotate(180deg); margin-left: -20px; } .fp-carousel-module .fp-nav-btn.fp-next-btn { right: 0; } .fp-carousel-module .fp-nav-btn.fp-next-btn::before { background: linear-gradient(270deg, rgba(0,0,0,0.4), transparent); } .fp-carousel-module .fp-arrow { display: block; width: 40px; height: 40px; border-radius: 50%; background: rgba(255,255,255,0.8); display: flex; align-items: center; justify-content: center; font-size: 24px; font-weight: bold; color: #333; box-shadow: 0 2px 10px rgba(0,0,0,0.2); pointer-events: auto; transition: transform 0.2s ease, background 0.2s ease; } .fp-carousel-module .fp-arrow:hover { background: rgba(255,255,255,1); transform: scale(1.1); } /* 响应式设计 */ @media (max-width: 768px) { .fp-carousel-module { height: 400px; } .fp-carousel-module .fp-carousel-item { width: 70%; height: 85%; } .fp-carousel-module .fp-nav-btn { width: 20%; } } @media (max-width: 480px) { .fp-carousel-module { height: 300px; } .fp-carousel-module .fp-carousel-item { width: 85%; height: 90%; } .fp-carousel-module .fp-description { font-size: 14px; padding: 15px 10px 10px; } } </style> <div class="fp-carousel-module"> <div class="fp-carousel-container"> <div class="fp-carousel-item fp-active" data-index="0"> <img src="https://picsum.photos/id/1015/1000/750" alt="Mountain View"> <div class="fp-description">Majestic mountain range at sunrise</div> </div> <div class="fp-carousel-item fp-next" data-index="1"> <img src="https://picsum.photos/id/1035/1000/750" alt="Ocean Waves"> <div class="fp-description">Turquoise ocean waves crashing on rocks</div> </div> <div class="fp-carousel-item fp-next-far" data-index="2"> <img src="https://picsum.photos/id/1040/1000/750" alt="Forest Path"> <div class="fp-description">Mystical forest path covered in moss</div> </div> <div class="fp-carousel-item fp-prev" data-index="3"> <img src="https://picsum.photos/id/1031/1000/750" alt="Desert Dunes"> <div class="fp-description">Golden desert dunes at sunset</div> </div> <div class="fp-carousel-item fp-prev-far" data-index="4"> <img src="https://picsum.photos/id/1018/1000/750" alt="City Skyline"> <div class="fp-description">Urban city skyline at twilight</div> </div> <div class="fp-nav-btn fp-prev-btn"> <div class="fp-arrow">❮</div> </div> <div class="fp-nav-btn fp-next-btn"> <div class="fp-arrow">❯</div> </div> </div> </div> <script> (function() { // 封装整个轮播逻辑避免全局污染 const initCarousel = function(module) { const container = module.querySelector('.fp-carousel-container'); const items = Array.from(module.querySelectorAll('.fp-carousel-item')); const prevBtn = module.querySelector('.fp-prev-btn'); const nextBtn = module.querySelector('.fp-next-btn'); let currentIndex = 0; // 初始化位置 updatePositions(); // 事件绑定函数 const bindEvents = function() { // 图片点击事件 items.forEach(item => { item.addEventListener('click', function(e) { e.stopPropagation(); if (this.classList.contains('fp-next') || this.classList.contains('fp-next-far')) { navigate(1); } else if (this.classList.contains('fp-prev') || this.classList.contains('fp-prev-far')) { navigate(-1); } }); }); // 容器区域点击 container.addEventListener('click', function(e) { const rect = container.getBoundingClientRect(); const clickX = e.clientX - rect.left; if (clickX > rect.width * 3/4) { navigate(1); } else if (clickX < rect.width * 1/4) { navigate(-1); } }); // 导航按钮 prevBtn.addEventListener('click', function(e) { e.stopPropagation(); navigate(-1); }); nextBtn.addEventListener('click', function(e) { e.stopPropagation(); navigate(1); }); // 键盘导航 - 仅在当前轮播激活时响应 document.addEventListener('keydown', function(e) { // 检查事件是否发生在当前轮播模块内 if (document.activeElement.closest('.fp-carousel-module') !== module) return; if (e.key === 'ArrowLeft') { navigate(-1); } else if (e.key === 'ArrowRight') { navigate(1); } }); // 响应式调整 window.addEventListener('resize', function() { updatePositions(); }); }; // 导航函数 function navigate(direction) { currentIndex = (currentIndex + direction + items.length) % items.length; updatePositions(); } // 新位置 function updatePositions() { items.forEach((item, index) => { item.classList.remove('fp-active', 'fp-prev', 'fp-next', 'fp-prev-far', 'fp-next-far', 'fp-hidden'); const position = (index - currentIndex + items.length) % items.length; if (position === 0) { item.classList.add('fp-active'); } else if (position === 1) { item.classList.add('fp-next'); } else if (position === 2) { item.classList.add('fp-next-far'); } else if (position === items.length - 1) { item.classList.add('fp-prev'); } else if (position === items.length - 2) { item.classList.add('fp-prev-far'); } else { item.classList.add('fp-hidden'); } }); } // 初始化事件绑定 bindEvents(); }; // 初始化页面上的所有轮播模块 document.querySelectorAll('.fp-carousel-module').forEach(initCarousel); })(); </script> 这段代码表现出的容器右边上下两个圆角出现闪烁问题怎么修改?左边的箭头方向错了
最新发布
06-15
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值