search
HomeWeb Front-endHTML Tutorial用CSS创建翻转动画_html/css_WEB-ITnose

  CSS动画是非常有意思的,用一些简单的属性就可以创建出来,从优雅的淡入到更加惊艳的效果都可以,翻页效果就是其中之一,它通过在容器的正面跟反面包含不同的内容来加以实现。本文就是用尽可能简单的方法来实现这种效果。

卡片翻转效果

查看演示

  注:本文不是第一篇讲述这种效果怎么做的文章,但是我发现很多将这个的文章都过于复杂,添加了很多额外的样式代码到案例里面,让读者搞不清那些代码是需要的哪些不是。本文不会有这个问题,只含有一些必要的样式,你可以随意美化翻转的每个页。

HTML结构

  以下HTML结构就是为了达到有两面的效果的:

<div class="flip-container" ontouchstart="this.classList.toggle('hover');">    <div class="flipper">        <div class="front">            <!-- 正面内容 -->        </div>        <div class="back">            <!-- 反面内容 -->        </div>    </div></div>

  正如你预料的,有两个内容面板:正面跟反面。你将惊奇的发现包含的CSS样式不多:

/* 整个容器,包括透视 */.flip-container {    perspective: 1000;}/* 鼠标放上去的时候翻转 */.flip-container:hover .flipper, .flip-container.hover .flipper {    transform: rotateY(180deg);}.flip-container, .front, .back {    width: 320px;    height: 480px;}/* 翻转速度设置 */.flipper {    transition: 1.5s;    transform-style: preserve-3d;    position: relative;}/* 翻转页的时候隐藏背面 */.front, .back {    backface-visibility: hidden;    position: absolute;    top: 0;    left: 0;}/* 前面板放在上面 */.front {    z-index: 2;    /* for firefox 31 */    transform: rotateY(0deg);}/* 背面初始的时候隐藏 */.back {    transform: rotateY(180deg);}

  我们来看下大体的实现过程:

  • 最外面的容器设置了整个动画区域的透视范围
  • 内层的容器是真正用于翻转的元素,父容器被激活的时候翻转了180度。这也是控制翻转速度的地方,把翻转角度改成-180度就会反方向翻转了。
  • 正面和反面的元素被决定定位了,这样他们就可以很好的层叠在彼此之上。他们的 backface-visibility 属性是 hidden, 所以当正面或者反面显示的时候,背面就隐藏起来。
  • 正面元素比反面元素有一个比较高的 z-index 属性值所以它可以预先编码和显示在上层。
  • 反面元素旋转了180度,成了背面。

  以上就是所有要做的了,将这个简单的结构放在你想放的地方,修改你需要的正面跟反面的样式。

来自CSS动画专家Ana Tudor的说明

  在卡片元素上给属性设置确定的值(比如 overflow: hidden)将禁止有3D变换的子元素。我相信这是很重要的,因为我就曾这样设置过,导致所有的子元素都成了一样的3D变换了。

CSS翻转开关

  如果你想用JavaScript代码来控制元素的翻转,一个简单的CSS类样式就可以了

.flip-container:hover .flipper, .flip-container.hover .flipper, .flip-container.flip .flipper {    transform: rotateY(180deg);}

  添加这个翻转的类样式到你想要用JavaScript控制的元素里面,使用下面的JavaScript代码就可以起作用了。

document.querySelector("#myCard").classList.toggle("flip")

CSS垂直翻转

  想要实现垂直翻转就需要添加 transform-origin 坐标轴的值。原来的翻转样式需要更新才能实现垂直地翻转。

.vertical.flip-container {    position: relative;}.vertical .back {    transform: rotateX(180deg);}.vertical.flip-container .flipper {    transform-origin: 100% 213.5px; /* 高度值的一半 */}.vertical.flip-container:hover .flipper {    transform: rotateX(-180deg);}

  你将看到绕着X轴翻转的效果。

支持IE浏览器

  要兼容IE浏览器就需要对代码进行一些改动,因为IE还没兼容所有现代 transform 的属性。 本质上还是正面跟反面元素同时翻转。

/* 整个容器,包括透视 */.flip-container {    perspective: 1000;    transform-style: preserve-3d;}/*  注意这里有修改 */.flip-container:hover .back {    transform: rotateY(0deg);}.flip-container:hover .front {    transform: rotateY(180deg);}.flip-container, .front, .back {    width: 320px;    height: 480px;}/* 翻转速度设置 */.flipper {    transition: 0.6s;    transform-style: preserve-3d;    position: relative;}/* 翻转页的时候隐藏背面 */.front, .back {    backface-visibility: hidden;    transition: 0.6s;    transform-style: preserve-3d;    position: absolute;    top: 0;    left: 0;}/* 注意这里有修改 */.front {    z-index: 2;    transform: rotateY(0deg);}/* 背面初始的时候隐藏 */.back {    transform: rotateY(-180deg);}/*     垂直翻转的修改*/.vertical.flip-container {    position: relative;}.vertical .back {    transform: rotateX(180deg);}.vertical.flip-container:hover .back {    transform: rotateX(0deg);}.vertical.flip-container:hover .front {    transform: rotateX(180deg);}

  上面的代码就会在IE10以上工作了。

查看演示

  CSS翻转动画是很经典的,是CSS动画运用的代表性例子。稍加深入,就是3D CSS动画效果了,CSS代码真的算是很少了。这个效果对于HTML5游戏是很优雅的。对于卡片式的效果,简直完美。

本文译自https://davidwalsh.name/css-flip

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 as a Markup Language: Its Function and PurposeHTML as a Markup Language: Its Function and PurposeApr 22, 2025 am 12:02 AM

The function of HTML is to define the structure and content of a web page, and its purpose is to provide a standardized way to display information. 1) HTML organizes various parts of the web page through tags and attributes, such as titles and paragraphs. 2) It supports the separation of content and performance and improves maintenance efficiency. 3) HTML is extensible, allowing custom tags to enhance SEO.

The Future of HTML, CSS, and JavaScript: Web Development TrendsThe Future of HTML, CSS, and JavaScript: Web Development TrendsApr 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: 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.

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

Video Face Swap

Video Face Swap

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

Hot Tools

SecLists

SecLists

SecLists is the ultimate security tester's companion. It is a collection of various types of lists that are frequently used during security assessments, all in one place. SecLists helps make security testing more efficient and productive by conveniently providing all the lists a security tester might need. List types include usernames, passwords, URLs, fuzzing payloads, sensitive data patterns, web shells, and more. The tester can simply pull this repository onto a new test machine and he will have access to every type of list he needs.

WebStorm Mac version

WebStorm Mac version

Useful JavaScript development tools

Atom editor mac version download

Atom editor mac version download

The most popular open source editor

EditPlus Chinese cracked version

EditPlus Chinese cracked version

Small size, syntax highlighting, does not support code prompt function

DVWA

DVWA

Damn Vulnerable Web App (DVWA) is a PHP/MySQL web application that is very vulnerable. Its main goals are to be an aid for security professionals to test their skills and tools in a legal environment, to help web developers better understand the process of securing web applications, and to help teachers/students teach/learn in a classroom environment Web application security. The goal of DVWA is to practice some of the most common web vulnerabilities through a simple and straightforward interface, with varying degrees of difficulty. Please note that this software