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
What is the difference between an HTML tag and an HTML attribute?What is the difference between an HTML tag and an HTML attribute?May 14, 2025 am 12:01 AM

HTMLtagsdefinethestructureofawebpage,whileattributesaddfunctionalityanddetails.1)Tagslike,,andoutlinethecontent'splacement.2)Attributessuchassrc,class,andstyleenhancetagsbyspecifyingimagesources,styling,andmore,improvingfunctionalityandappearance.

The Future of HTML: Evolution and TrendsThe Future of HTML: Evolution and TrendsMay 13, 2025 am 12:01 AM

The future of HTML will develop in a more semantic, functional and modular direction. 1) Semanticization will make the tag describe the content more clearly, improving SEO and barrier-free access. 2) Functionalization will introduce new elements and attributes to meet user needs. 3) Modularity will support component development and improve code reusability.

Why are HTML attributes important for web development?Why are HTML attributes important for web development?May 12, 2025 am 12:01 AM

HTMLattributesarecrucialinwebdevelopmentforcontrollingbehavior,appearance,andfunctionality.Theyenhanceinteractivity,accessibility,andSEO.Forexample,thesrcattributeintagsimpactsSEO,whileonclickintagsaddsinteractivity.Touseattributeseffectively:1)Usese

What is the purpose of the alt attribute? Why is it important?What is the purpose of the alt attribute? Why is it important?May 11, 2025 am 12:01 AM

The alt attribute is an important part of the tag in HTML and is used to provide alternative text for images. 1. When the image cannot be loaded, the text in the alt attribute will be displayed to improve the user experience. 2. Screen readers use the alt attribute to help visually impaired users understand the content of the picture. 3. Search engines index text in the alt attribute to improve the SEO ranking of web pages.

HTML, CSS, and JavaScript: Examples and Practical ApplicationsHTML, CSS, and JavaScript: Examples and Practical ApplicationsMay 09, 2025 am 12:01 AM

The roles of HTML, CSS and JavaScript in web development are: 1. HTML is used to build web page structure; 2. CSS is used to beautify the appearance of web pages; 3. JavaScript is used to achieve dynamic interaction. Through tags, styles and scripts, these three together build the core functions of modern web pages.

How do you set the lang attribute on the  tag? Why is this important?How do you set the lang attribute on the tag? Why is this important?May 08, 2025 am 12:03 AM

Setting the lang attributes of a tag is a key step in optimizing web accessibility and SEO. 1) Set the lang attribute in the tag, such as. 2) In multilingual content, set lang attributes for different language parts, such as. 3) Use language codes that comply with ISO639-1 standards, such as "en", "fr", "zh", etc. Correctly setting the lang attribute can improve the accessibility of web pages and search engine rankings.

What is the purpose of HTML attributes?What is the purpose of HTML attributes?May 07, 2025 am 12:01 AM

HTMLattributesareessentialforenhancingwebelements'functionalityandappearance.Theyaddinformationtodefinebehavior,appearance,andinteraction,makingwebsitesinteractive,responsive,andvisuallyappealing.Attributeslikesrc,href,class,type,anddisabledtransform

How do you create a list in HTML?How do you create a list in HTML?May 06, 2025 am 12:01 AM

TocreatealistinHTML,useforunorderedlistsandfororderedlists:1)Forunorderedlists,wrapitemsinanduseforeachitem,renderingasabulletedlist.2)Fororderedlists,useandfornumberedlists,customizablewiththetypeattributefordifferentnumberingstyles.

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 Article

Hot Tools

Dreamweaver Mac version

Dreamweaver Mac version

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

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

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