Table of Contents
第一个概念
使字母变倾斜
一个用于阴影的函数
1. 组装在一起
3. 改变字体颜色
4. 触发这个动画
Home Web Front-end HTML Tutorial 用 CSS 实现 Netflix Logo 动画_html/css_WEB-ITnose

用 CSS 实现 Netflix Logo 动画_html/css_WEB-ITnose

Jun 21, 2016 am 08:49 AM

本文译自: Netflix Logo In CSS

这篇博客是 Gregor Adams 讲他如何用 CSS 重现 Netflix 商标效果。Gregor 是 CSS 方面冉冉升起的新星。能在这里分析他的案例也是非常荣幸的。

我尝试使用 Netflix(译者注:一家在线影片租赁提供商)时,立即就把我吸引住了。我观看了一些不得不在它处才能观看的节目。每一集电视剧或者电影都以 Netflix 动画作为开始。

在观看了几集电视剧之后,我想到可以用 CSS 来实现 Netflix 的 logo 动画,于是我看了几部作品之后,就用 CodePen 来重现这个 logo。

第一个概念

因为我想要尝试某些技术方案导致我的第一种实现方式有些累赘。

例如:我想使用纯 CSS 技术来实现它,并且我也想当我点击按钮的时候,这个动画再执行一次,所以我要使用一些不可思议技巧。幸运的是当我写 CSS 代码的时候,总会有一些小技巧会在我的脑海里涌现。

我们来谈论一下实际的动画。

我录下这个动画并且在 Quicktime 中循环播放,这样可以详细检查。我倾向于这么做,能让我停在某些特定帧弄清楚到底发生了什么。

这个商标:

  1. 以一个白屏幕开始。
  2. 弹出白色的 3D 字母。
  3. 投射阴影。
  4. 消失。
  5. 把字体颜色变成红色。

这就是我需要重现的动画步骤。但是这里有另外一些关于这个 logo 的东西需要解决: 字母在商标中心是倾斜的。

大家一直问我如何做到这些。 都是从积累中获取地 :)

我做过许多 3D 案例,所以这对我来说不是很难。

使字母变倾斜

以这个词 “Netflix” 的一些基本标记开始。

<div class="logo">  <span>N</span>  <span>E</span>  <span>T</span>  <span>F</span>  <span>L</span>  <span>I</span>  <span>X</span></div>
Copy after login

我用类 logo 做了一个包裹,并且用 span 标签包裹每一个字母。

然后我在Y轴上旋转这个字母并且在 X 轴上缩放这个字母以保持它的原始宽度。重要的部分是在 class=”logo” 包装上设置一个 perspective ,并且定义它的 perspective-origin 。

/* 基础的字母样式 */span {    font-size: 8em;    font-family: impact;    display: block;}/* 开启三维效果 */.logo {    perspective: 1000px;    perspective-origin: 50% 0;}/* 给字母变换 */.logo span {    transform-origin: 0 0;    transform: scaleX(80) rotateY(89.5deg);}
Copy after login

这里还有一些其它的方式来实现这些技巧,例如使用一个不同 perspective(比如500px),旋转角度(比如9deg)和缩放(比如0.5),但是这些值能最大满足我的需求。

下面是在 CodePen 实现的小例子:(译者注:原 demo 是页面中嵌入的 iframe 实现嵌入 CodePen ,但是 markdown 没有嵌入 iframe 的方法,所以采用 CodePen 来展示,并且把原 demo 的 jade 和 scss 写法转换成 html 和 css 方便没有使用过两种技术的读者阅读)

  • 使用 jade 和 scss 完成的 demo
  • 转换成 html 和 css 完成的 demo

实际效果

接下来我要对所有的字母应用这个样式,中间的字母不要变化。右边的字母朝着相反的方向倾斜,并且字母高度发生变化。

为了实现这些需要增加一些新逻辑:我使用 Sass 的标准语法来实现。

Sass 代码:

.logo {  perspective: 1000px;  perspective-origin: 50% 0;  font-size: 8em;  display: inline-flex;  span {    font-family: impact;    display: block;    $letters: 7;    @for $i from 1 through $letters {      $offset: $i - ceil($letters / 2);      $trans: if($offset > 0, -89.5deg, 89.5deg);      &:nth-child(#{$i}) {        // trans/de-form the letters        transform-origin: 50% + 50%/$offset 200%;        font-size: if($offset == 0,          0.85em,          0.9em + 0.015*pow(abs($offset),2));        transform:            if($offset == 0, scale(1, 1), scale(95.9 - abs($offset) * 10, 1))            if($offset == 0, translatey(0%), rotatey($trans));      }    }  }}
Copy after login

为了方便不懂 scss 同学理解,这是我编译后的 css 代码:

.logo {   perspective: 1000px;   perspective-origin: 50% 0;   font-size: 8em;   display: inline-flex;}.logo span {   font-family: impact;   display: block;}.logo span:nth-child(1) {   transform-origin: 33.33333333% 200%;   font-size: 1.035em;   transform: scale(65.9, 1) rotatey(89.5deg);}.logo span:nth-child(2) {   transform-origin: 25% 200%;   font-size: 0.96em;   transform: scale(75.9, 1) rotatey(89.5deg);}.logo span:nth-child(3) {   transform-origin: 0% 200%;   font-size: 0.915em;  transform: scale(85.9, 1) rotatey(89.5deg);}.logo span:nth-child(4) {   transform-origin: Infinity% 200%;   font-size: 0.85em;   transform: scale(1, 1) translatey(0%);}.logo span:nth-child(5) {   transform-origin: 100% 200%;   font-size: 0.915em;   transform: scale(85.9, 1) rotatey(-89.5deg);}.logo span:nth-child(6) {   transform-origin: 75% 200%;   font-size: 0.96em;   transform: scale(75.9, 1) rotatey(-89.5deg);}.logo span:nth-child(7) {   transform-origin: 66.66666667% 200%;   font-size: 1.035em;   transform: scale(65.9, 1) rotatey(-89.5deg);}
Copy after login

下面是在 CodePen 实现的小例子:(译者注:原 demo 是页面中嵌入的 iframe 实现嵌入 CodePen ,但是 markdown 没有嵌入 iframe 的方法,所以采用 Codepen 来展示,并且把原 demo 的 jade 和 scss 写法转换成 html 和 css 方便没有使用过两种技术的读者阅读)。

  • 使用 jade 和 scss 完成的 demo
  • 转换成 html 和 css 的 demo

实际效果:

一个用于阴影的函数

写一个实现 3d 效果和阴影的函数。我把视频停在某一帧,并仔细查看细节。

正如你所看到的,当这个阴影到达右下角,3d 效果的消失点在中间。现在知道我们函数需要做什么了。

我们将会在 keyframes 中调用这个函数,所以我们希望他能处理一些值,例如:

  • color
  • x
  • y
  • blur
  • mix

我们还需要一个参数来定义阴影的深度或者 3d 效果。

下面就是用来处理这些需求的函数:

/// 在特定方向创创建三维阴影/// @author Gregor Adams/// @param {Number} $depth - 阴影长度/// @param {Unit} $color - 阴影颜色/// @param {Unit} $x - 在x轴上到下一个阴影的距离/// @param {Unit} $y - 在y轴上到下一个阴影的距离/// @param {Unit} $blur - text-shadow的模糊距离/// @param {Color|false} $mix - 添加一个可选的颜色来混合/// @return {List} - 返回text-shadow列表@function d3($depth, $color, $x: 1px, $y: 1px, $blur: 0, $mix: false) {  $shadow: ();  @for $i from 1 through $depth {    // append to the existing shadow    @if type-of($mix) != 'color' {      $shadow: append($shadow, round($i * $x) round($i * $y) $blur $color, comma);    } @else {      $shadow: append($shadow, round($i * $x) round($i * $y) $blur mix($mix, $color, 0.3%*$i), comma);    }  }  @return $shadow;}
Copy after login

这个函数对于 Sass 菜鸟或者只使用基本语言特性的开发者和设计师来说可能有点难理解,所以让我来详细解释一下。

我以一个 $shadow 的变量开始, list 是一个空的列表。

$shadow: ();
Copy after login

我是从1开始循环到 $depth 。在 Sass 中会使迭代器迭代到 through 这个值。

  • from 0 to 5 返回 0, 1, 2, 3, 4
  • from 0 through 5 返回 0, 1, 2, 3, 4, 5

每一次迭代我都添加一个 text-shadow 到这个列表。所以最后这个列表看起来就是下面这个样子:

$shadow: (0 1px 0 red, 1px 2px 0 red, 2px 3px 0 red, ...);
Copy after login

使用的时候就像下面这样:

text-shadow: d3(5, red, [$x], [$y], [$blur], [$mix]);
Copy after login

$x,$y,$blur 和 $mix 都是可选的参数。我已经提到我将会在 keyframes 中调用这个函数,所以我需要可选择性地改变他们。 $mix 允许添加第二个颜色,实现这个阴影从一种颜色淡出成另外一种颜色。

下面是在 CodePen 实现的小例子:(译者注:原 demo 是页面中嵌入的 iframe 实现嵌入 CodePen ,但是 markdown 没有嵌入 iframe 的方法,所以采用 CodePen 来展示,并且把原 demo 的 jade 和 scss 写法转换成 html 和 css 方便没有使用过两种技术的读者阅读)

  • 使用 jade 和 scss 完成的 demo
  • 转成 html 和 css 的 demo

实际效果:

组装在一起

因为我已经创造了许多我需要的部分,现在可以建立动画了。

1. 组装在一起

我使用两个上面已经定义的变量 $offset 和 $trans ,动画有三个阶段,我需要仔细地决定何时到达某帧。

@keyframes pop-out {  0% {    transform:      if($offset == 0, scale(1, 1), scale(95.9 - abs($offset) * 10, 1))      if($offset == 0, translatey(0%), rotatey($trans));    text-shadow:      d3(15, rgba($c_3d, 0), 0, 0),      d3(50, rgba($c_shadow, 0), 0, 0);  }  50% {    transform:      if($offset == 0, scale(1.2, 1.2), scale(126.2 - abs($offset) * 10, 1.2))      if($offset == 0, translatey(-16%), rotatey($trans));    text-shadow:      d3(15, $c_3d, if($offset == 0, 0, -0.25px * $offset), 1px),      d3(50, $c_shadow, 1px, 3px, 3px, $c_shadow-mix);  }  100% {    transform:      if($offset == 0, scale(1.1, 1.1), scale(116.2 - abs($offset) * 10, 1.1))      if($offset == 0, translatey(-12%), rotatey($trans));    text-shadow:      d3(15, $c_3d, if($offset == 0, 0, -0.25px * $offset), 1px),      d3(50, $c_shadow, 1px, 3px, 3px, $c_shadow-mix);  }}
Copy after login

2. 淡出(动画结尾)同样的步骤实现淡出的效果。

@keyframes fade-back {  0% {    transform:      if($offset == 0, scale(1.1, 1.1), scale(116.2 - abs($offset) * 10, 1.1))      if($offset == 0, translatey(-12%), rotatey($trans));    text-shadow:      d3(15, $c_3d, if($offset == 0, 0, -0.25px * $offset), 1px),      d3(50, $c_shadow, 1px, 3px, 3px, $c_shadow-mix);  }  20% {    transform:      if($offset == 0, scale(1.05, 1.05), scale(105.9 - abs($offset) * 10, 1.05))      if($offset == 0, translatey(-7%), rotatey($trans));    text-shadow:      d3(15, rgba($c_3d, 0), 0, 0),      d3(50, rgba($c_shadow, 0), 0, 0);  }  100% {    transform:      if($offset == 0, scale(1, 1), scale(95.9 - abs($offset) * 10, 1))      if($offset == 0, translatey(0%), rotatey($trans));    text-shadow:      d3(15, rgba($c_3d, 0), 0, 0),      d3(50, rgba($c_shadow, 0), 0, 0);  }}
Copy after login

3. 改变字体颜色

还需要提供一个动画改变字体颜色。

@keyframes change-color {  0% {    color: $c_bg;  }  100% {    color: $c_fg;  }}
Copy after login

4. 触发这个动画

现在我们可以像下面这样把动画连接在一起。

animation-name: pop-out, fade-back, change-color;animation-duration: 4s, 2s, 0.1s;animation-delay: 0s, 2s, 3.2s
Copy after login

上面的代码只是一个近似的实现,每个字母有不同的动画延迟和间隔,可以点击 这里 查看最终的实现效果。

最后注意一下,我使用了一些不可思议的技巧来实现在纯 CSS 中再次触发动画,我将会在接下来的文章中解释。

写案例的时候并不是十分满意,因为写文章的时候我又想到了其它几个提高效果的方法。

为了写这篇文章我重新写了整个 Sass 代码,但是我仍然觉得我能提升一些。这就是我不间断做案例的主要原因。让我变得更加聪明,和在一些以前没有涉足过的方向有新的突破。

我几乎没有在实际的项目中用到这样的技术,但是我经常使用函数来提升效果。不论如何希望你喜欢这篇文章。

Gregor Adams 是一位来自 Hamburg 的前端开发者,他对 CSS 和 Sass 有极大的热情。从他的 CodePen 中可以看出他强大的 CSS 技术。

欢迎关注

  • 新浪微博:前端外刊评论
  • 博客:前端外刊评论
  • 个人博客
  • 如果有疏漏的地方欢迎批评指正:[email protected]
Statement of this Website
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

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

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Hot Topics

Java Tutorial
1663
14
PHP Tutorial
1266
29
C# Tutorial
1239
24
Understanding HTML, CSS, and JavaScript: A Beginner's Guide Understanding HTML, CSS, and JavaScript: A Beginner's Guide Apr 12, 2025 am 12:02 AM

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

The Roles of HTML, CSS, and JavaScript: Core Responsibilities The Roles of HTML, CSS, and JavaScript: Core Responsibilities Apr 08, 2025 pm 07:05 PM

HTML defines the web structure, CSS is responsible for style and layout, and JavaScript gives dynamic interaction. The three perform their duties in web development and jointly build a colorful website.

HTML, CSS, and JavaScript: Essential Tools for Web Developers HTML, CSS, and JavaScript: Essential Tools for Web Developers Apr 09, 2025 am 12:12 AM

HTML, CSS and JavaScript are the three pillars of web development. 1. HTML defines the web page structure and uses tags such as, etc. 2. CSS controls the web page style, using selectors and attributes such as color, font-size, etc. 3. JavaScript realizes dynamic effects and interaction, through event monitoring and DOM operations.

HTML: The Structure, CSS: The Style, JavaScript: The Behavior HTML: The Structure, CSS: The Style, JavaScript: The Behavior Apr 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 Design The Future of HTML: Evolution and Trends in Web Design Apr 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.

The Future of HTML, CSS, and JavaScript: Web Development Trends The Future of HTML, CSS, and JavaScript: Web Development Trends Apr 19, 2025 am 12:02 AM

The future trends of HTML are semantics and web components, the future trends of CSS are CSS-in-JS and CSSHoudini, and the future trends of JavaScript are WebAssembly and Serverless. 1. HTML semantics improve accessibility and SEO effects, and Web components improve development efficiency, but attention should be paid to browser compatibility. 2. CSS-in-JS enhances style management flexibility but may increase file size. CSSHoudini allows direct operation of CSS rendering. 3.WebAssembly optimizes browser application performance but has a steep learning curve, and Serverless simplifies development but requires optimization of cold start problems.

HTML vs. CSS vs. JavaScript: A Comparative Overview HTML vs. CSS vs. JavaScript: A Comparative Overview Apr 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: Building the Structure of Web Pages HTML: Building the Structure of Web Pages Apr 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.

See all articles