search
HomeWeb Front-endHTML Tutorial如何实现平滑的“box-shadow”动画效果_html/css_WEB-ITnose

你如何在 CSS 中设置 box-shadow 属性实现动画效果,既不会导致重新绘制每一帧,又不会严重影响你页面的性能?回答是:你并不能实现。因为 box-shadow 的动画变化会损害性能。

这里有一个简单的办法可以实现上述问题效果。如果要实现最小的重新绘制,应该创建一个伪元素并对其 opacity 元素进行动画处理,使其以每秒60帧的动画模仿运动物体相同的效果。

实例

认真观察 这个实例 ,比较我们在其中使用的不同技巧。你是不是会说两者效果看起来一样。唯一不同的是我们如何应用阴影并对其进行动画处理。在左边实例中,我们鼠标 hover (悬浮)时,对 box-shadow 应用了动画效果。而在右边的实例中,我们用 ::after 添加了一个伪元素并对其设置了阴影,并对该元素的 opacity 元素进行了动画处理。

如果你使用开发工具尝试了其中之一,您应该会看到类似这样的东西 (绿色条表示已经绘制,其越少越好):

当你悬停在左边的卡片(在 box-shadow 上应用动画)与悬浮在右边的卡片(对其伪元素的 opacity 应用动画)进行相比时,你会很明显的发现有更多的重新绘制。

为什么我们会看到这种效果?有 很少的 CSS 属性 ,即 opacity 和 transform ,进行动画处理时,不会不断触发重绘每一帧。我们最好坚持只在动画中更改这两个属性实现最小化重绘 (您的浏览器不得不做的工作)。

抛却其它的布局样式,这就是这两种技术之间的 关键区别

/* The slow way */.make-it-slow {  box-shadow: 0 1px 2px rgba(0,0,0,0.15);  transition: box-shadow 0.3s ease-in-out:}/* 鼠标悬停实现更大阴影的过渡 */.make-it-slow:hover {  box-shadow: 0 5px 15px rgba(0,0,0,0.3);}/* The fast way */.make-it-fast {  box-shadow: 0 1px 2px rgba(0,0,0,0.15);}/* 设置更大的阴影并将之隐藏 */.make-it-fast::after {  box-shadow: 0 5px 15px rgba(0,0,0,0.3);  opacity: 0;  transition: opacity 0.3s ease-in-out:}/* 鼠标悬停时实现更大阴影的过渡显示 */.make-it-fast:hover::after {  opacity: 1;}

详细分解

基于上面所述,让我们看看如何创建上面示例中演示的 3D卡片效果 。第一步是将阴影转移到到一个伪元素,就像我们上面所做的一样。让我们也给所有创建的卡片添加布局代码:

/* 创建一个简单白色盒子,并添加阴影 */.box {  position: relative;  display: inline-block;  width: 100px;  height: 100px;  border-radius: 5px;  background-color: #fff;  box-shadow: 0 1px 2px rgba(0,0,0,0.15);  transition: all 0.3s ease-in-out;}/* 创建隐藏的伪元素 *//* 最终样式包含阴影 */.box::after {  content: '';  position: absolute;  z-index: -1;  width: 100%;  height: 100%;  opacity: 0;  border-radius: 5px;  box-shadow: 0 5px 15px rgba(0,0,0,0.3);  transition: opacity 0.3s ease-in-out;}

注意,我们要 .box 和 .box::after 添加了 transition 元素,因为我们要对这两个元素进行动画处理: .box 上应用 transform 元素, .box::after 上应用 opacity 元素.

这些样式给我们展示了一个具有微妙 box-shadow 样式的白色盒子。这里 .box::after 上的阴影已经完全被隐藏,不能与盒子效果相互影响。

若要创建 演示 中相同的效果,现在我们需要做的是:当鼠标悬停在 .box 时,产生放大效果,并且在伪元素和阴影上实现淡化:

/* 放大盒子 */.box:hover {  transform: scale(1.2, 1.2);}/* 伪元素更大阴影的淡化 */.box:hover::after {  opacity: 1;}

总之,这就是所有的CSS样式,包括不同浏览器的兼容样式和一些额外的自定义缓动效果:

.box {  position: relative;  display: inline-block;  width: 100px;  height: 100px;  background-color: #fff;  border-radius: 5px;  box-shadow: 0 1px 2px rgba(0, 0, 0, 0.1);  border-radius: 5px;  -webkit-transition: all 0.6s cubic-bezier(0.165, 0.84, 0.44, 1);  transition: all 0.6s cubic-bezier(0.165, 0.84, 0.44, 1);}.box::after {  content: "";  border-radius: 5px;  position: absolute;  z-index: -1;  top: 0;  left: 0;  width: 100%;  height: 100%;  box-shadow: 0 5px 15px rgba(0, 0, 0, 0.3);  opacity: 0;  -webkit-transition: all 0.6s cubic-bezier(0.165, 0.84, 0.44, 1);  transition: all 0.6s cubic-bezier(0.165, 0.84, 0.44, 1);}.box:hover {  -webkit-transform: scale(1.25, 1.25);  transform: scale(1.25, 1.25);}.box:hover::after {    opacity: 1;}

显然这里与只使用 box-shadow 相比应用了大量的 CSS 来实现相同的动画效果,只是改进性能。这又何必呢?

即使你的桌面处理 box-shadow 动画时没有任何问题,你的手机可能也不会出现问题。但是当你处理更为复杂的布局时,你就会发现你的桌面可能开始表现出症状。

保持只在过渡(transitions)和动画(animations)上应用 transform 和 opacity 元素,你就会达到最佳的性能,那才是最佳的用户体验。

本文根据 @Tobias 的《 How to animate "box-shadow" with silky smooth performance 》所译,整个译文带有我们自己的理解与思想,如果译得不好或有不对之处还请同行朋友指点。如需转载此译文,需注明英文出处: http://tobiasahlin.com/blog/how-to-animate-box-shadow/ 。

静子

在校学生,本科计算机专业。一个积极进取、爱笑的女生,热爱前端,喜欢与人交流分享。想要通过自己的努力做到心中的那个自己。微博:@静-如秋叶

Statement
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
HTML: The Structure, CSS: The Style, JavaScript: The BehaviorHTML: The Structure, CSS: The Style, JavaScript: The BehaviorApr 18, 2025 am 12:09 AM

The roles of HTML, CSS and JavaScript in web development are: 1. HTML defines the web page structure, 2. CSS controls the web page style, and 3. JavaScript adds dynamic behavior. Together, they build the framework, aesthetics and interactivity of modern websites.

The Future of HTML: Evolution and Trends in Web DesignThe Future of HTML: Evolution and Trends in Web DesignApr 17, 2025 am 12:12 AM

The future of HTML is full of infinite possibilities. 1) New features and standards will include more semantic tags and the popularity of WebComponents. 2) The web design trend will continue to develop towards responsive and accessible design. 3) Performance optimization will improve the user experience through responsive image loading and lazy loading technologies.

HTML vs. CSS vs. JavaScript: A Comparative OverviewHTML vs. CSS vs. JavaScript: A Comparative OverviewApr 16, 2025 am 12:04 AM

The roles of HTML, CSS and JavaScript in web development are: HTML is responsible for content structure, CSS is responsible for style, and JavaScript is responsible for dynamic behavior. 1. HTML defines the web page structure and content through tags to ensure semantics. 2. CSS controls the web page style through selectors and attributes to make it beautiful and easy to read. 3. JavaScript controls web page behavior through scripts to achieve dynamic and interactive functions.

HTML: Is It a Programming Language or Something Else?HTML: Is It a Programming Language or Something Else?Apr 15, 2025 am 12:13 AM

HTMLisnotaprogramminglanguage;itisamarkuplanguage.1)HTMLstructuresandformatswebcontentusingtags.2)ItworkswithCSSforstylingandJavaScriptforinteractivity,enhancingwebdevelopment.

HTML: Building the Structure of Web PagesHTML: Building the Structure of Web PagesApr 14, 2025 am 12:14 AM

HTML is the cornerstone of building web page structure. 1. HTML defines the content structure and semantics, and uses, etc. tags. 2. Provide semantic markers, such as, etc., to improve SEO effect. 3. To realize user interaction through tags, pay attention to form verification. 4. Use advanced elements such as, combined with JavaScript to achieve dynamic effects. 5. Common errors include unclosed labels and unquoted attribute values, and verification tools are required. 6. Optimization strategies include reducing HTTP requests, compressing HTML, using semantic tags, etc.

From Text to Websites: The Power of HTMLFrom Text to Websites: The Power of HTMLApr 13, 2025 am 12:07 AM

HTML is a language used to build web pages, defining web page structure and content through tags and attributes. 1) HTML organizes document structure through tags, such as,. 2) The browser parses HTML to build the DOM and renders the web page. 3) New features of HTML5, such as, enhance multimedia functions. 4) Common errors include unclosed labels and unquoted attribute values. 5) Optimization suggestions include using semantic tags and reducing file size.

Understanding HTML, CSS, and JavaScript: A Beginner's GuideUnderstanding HTML, CSS, and JavaScript: A Beginner's GuideApr 12, 2025 am 12:02 AM

WebdevelopmentreliesonHTML,CSS,andJavaScript:1)HTMLstructurescontent,2)CSSstylesit,and3)JavaScriptaddsinteractivity,formingthebasisofmodernwebexperiences.

The Role of HTML: Structuring Web ContentThe Role of HTML: Structuring Web ContentApr 11, 2025 am 12:12 AM

The role of HTML is to define the structure and content of a web page through tags and attributes. 1. HTML organizes content through tags such as , making it easy to read and understand. 2. Use semantic tags such as, etc. to enhance accessibility and SEO. 3. Optimizing HTML code can improve web page loading speed and user experience.

See all articles

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
1 months agoBy尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Best Graphic Settings
1 months agoBy尊渡假赌尊渡假赌尊渡假赌
Will R.E.P.O. Have Crossplay?
1 months agoBy尊渡假赌尊渡假赌尊渡假赌

Hot Tools

MinGW - Minimalist GNU for Windows

MinGW - Minimalist GNU for Windows

This project is in the process of being migrated to osdn.net/projects/mingw, you can continue to follow us there. MinGW: A native Windows port of the GNU Compiler Collection (GCC), freely distributable import libraries and header files for building native Windows applications; includes extensions to the MSVC runtime to support C99 functionality. All MinGW software can run on 64-bit Windows platforms.

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

WebStorm Mac version

WebStorm Mac version

Useful JavaScript development tools

ZendStudio 13.5.1 Mac

ZendStudio 13.5.1 Mac

Powerful PHP integrated development environment

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor