Table of Contents
Animation effect
2. Problems encountered:
Home Web Front-end CSS Tutorial How to achieve flip effect in cs

How to achieve flip effect in cs

Apr 13, 2021 am 10:40 AM
css3 animation front end

How to achieve the flip effect in css: 1. Set the perspective of the outer element; 2. Flip the second wrapping layer 180 degrees and set the transition speed; 3. Set "backface-visibility"; 4. Set "z-index" attribute; 5. Let "back" flip 180 degrees at the beginning.

How to achieve flip effect in cs

CSS3 realizes the flip effect

Today, we use relatively simple CSS3 to realize the flip effect.

Animation effect

How to achieve flip effect in cs

How to achieve flip effect in cs

##Effect analysis

When the mouse slides over the containing block , the element is flipped 180 degrees as a whole to achieve switching between the "front" and "reverse" sides.

HTML Analysis

Analysis:

.container, .flip are prepared to achieve animation effects. .front,.backEach package contains one picture. The HTML to achieve this effect is as follows:

<p class="container">
    <p class="flip">
        <p class="front">
            <img src="images/pic00.jpg" alt="">
        </p>
        <p class="back">
            <img src="images/pic01.jpg" alt="">
        </p>
    </p>
</p>
Copy after login
CSS analysis

1. Element layout

In order to achieve the above effect, element layout is performed first. Absolutely position

.front and .back relative to .flip so that they overlap at the same position. The code for the layout part is as follows:

    .container,.front,.back{width:380px;height:270px;}
    .flip{position:relative;}
    .front,.back{position:absolute;top: 0px;left: 0px;}
Copy after login
After setting up, we found that the picture of

.back is above .front, so .frontSet .fornt{z-index:2;}

Note: Do not set the overflow attribute to prevent elements from overflowing, this will cause 3D effects cannot be achieved.

w3 spec describes:

The following CSS property values ​​require the user agent to create a flattened representation of the descendant elements before they can be applied, and therefore force the used value of transform-style to flat:

  • overflow: any value other than visible.

  • opacity: any value less than 1.

  • filter: any value other than none.

  • clip: any value other than auto.

2. Implementation of animation effects

(1) In order to achieve animation effects, first set the following attributes to the ancestor elements

.container,.flip to trigger the 3D effect And set animation:

.container{perspective:1000;transform-style:preserve-3d;}
.flip{transition:0.6s;transform-style:preserve-3d;}
Copy after login
(2) Next, in order to prevent the back side from being exposed when the picture is flipped, set

backface-visibility to .front,.back Attributes:
.front,.back{backface-visibility:hidden;}

(3) In order to allow the containing block to flip 180 degrees when the mouse slides over it , to achieve switching between "positive" and "reverse" sides. Set

transform:rotateY(-180deg) to the element on the back, then we will not be able to see .back.

(4) Finally, when the user's mouse slides over the

.container containing block, .flip flips 180 degrees, so that .frontFlip 180 degrees, because the back side is hidden, so it cannot be seen; and .back, after flipping 180 degrees, returns to 0 degrees, showing the front side, so that we can see the back side .

The code is as follows:

    .container{perspective:1000;transform-style:preserve-3d;}
    .container,.front,.back{width:380px;height:270px;}
    .flip{position:relative;transition:0.6s;transform-style:preserve-3d;}
    .front,.back{position:absolute;top: 0px;left: 0px;backface-visibility:hidden;}
    .front{z-index:2;}
    .back{transform:rotateY(-180deg);}
    .container:hover .flip{transform:rotateY(180deg);}
Copy after login
Vertical flip effect implementation

The vertical effect is the same as the horizontal flip. But if you just replace rotateY with rotateX, then you will find that the picture is flipped with the top line.

Please note: In the above CSS code, I did not set the width and height for
.flip, so when applying transform:rotateY(180deg) to .flip , according to the default transform-origin value, the center point of the element is used as the basic point for flipping. The height of .flip here is 0, so of course it is flipped based on the top line. So there are two solutions:

  1. Set the same width for

    .flip as .front, .back high.

  2. Set the

    transform-origin:100% 135px/*half the height*/ attribute to .flip. OK, then you will find that vertical flipping is the effect you want!

Summary

1. Idea

(1) Set the outermost element

perspective to achieve 3D effect. (2) When the mouse slides over the outermost element, the second wrapping layer flips 180 degrees and sets the transition speed.
(3) The two flip blocks are absolutely positioned to achieve superposition at the same position. Also set
backface-visibility to avoid exposing the backside when implementing animation effects. (4) Set the
z-index attribute to .front so that it is in front when writing code and displaying. (5) Let
.back turn 180 degrees at the beginning to show people from the back.

2. Problems encountered:

(1) In order to make two pictures of different sizes the same size in the package block, the overflow attribute is used, and the 3D effect cannot be achieved. . Solution: Set width:100%;height:100%; for img
(2) Did not realize that the height of .flip is 0, Therefore, the standard point error causes different effects when flipping vertically.
(3) Only by writing more can you find more errors, and then you will know how to find errors and how to solve them.

[Recommended learning: css video tutorial]

The above is the detailed content of How to achieve flip effect in cs. For more information, please follow other related articles on the PHP Chinese website!

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)

An article about memory control in Node An article about memory control in Node Apr 26, 2023 pm 05:37 PM

The Node service built based on non-blocking and event-driven has the advantage of low memory consumption and is very suitable for handling massive network requests. Under the premise of massive requests, issues related to "memory control" need to be considered. 1. V8’s garbage collection mechanism and memory limitations Js is controlled by the garbage collection machine

Explore how to write unit tests in Vue3 Explore how to write unit tests in Vue3 Apr 25, 2023 pm 07:41 PM

Vue.js has become a very popular framework in front-end development today. As Vue.js continues to evolve, unit testing is becoming more and more important. Today we’ll explore how to write unit tests in Vue.js 3 and provide some best practices and common problems and solutions.

Let's talk in depth about the File module in Node Let's talk in depth about the File module in Node Apr 24, 2023 pm 05:49 PM

The file module is an encapsulation of underlying file operations, such as file reading/writing/opening/closing/delete adding, etc. The biggest feature of the file module is that all methods provide two versions of **synchronous** and **asynchronous**, with Methods with the sync suffix are all synchronization methods, and those without are all heterogeneous methods.

How to solve cross-domain issues? A brief analysis of common solutions How to solve cross-domain issues? A brief analysis of common solutions Apr 25, 2023 pm 07:57 PM

Cross-domain is a scenario often encountered in development, and it is also an issue often discussed in interviews. Mastering common cross-domain solutions and the principles behind them can not only improve our development efficiency, but also perform better in interviews.

PHP and Vue: a perfect pairing of front-end development tools PHP and Vue: a perfect pairing of front-end development tools Mar 16, 2024 pm 12:09 PM

PHP and Vue: a perfect pairing of front-end development tools. In today's era of rapid development of the Internet, front-end development has become increasingly important. As users have higher and higher requirements for the experience of websites and applications, front-end developers need to use more efficient and flexible tools to create responsive and interactive interfaces. As two important technologies in the field of front-end development, PHP and Vue.js can be regarded as perfect tools when paired together. This article will explore the combination of PHP and Vue, as well as detailed code examples to help readers better understand and apply these two

Learn more about Buffers in Node Learn more about Buffers in Node Apr 25, 2023 pm 07:49 PM

At the beginning, JS only ran on the browser side. It was easy to process Unicode-encoded strings, but it was difficult to process binary and non-Unicode-encoded strings. And binary is the lowest level data format of the computer, video/audio/program/network package

Questions frequently asked by front-end interviewers Questions frequently asked by front-end interviewers Mar 19, 2024 pm 02:24 PM

In front-end development interviews, common questions cover a wide range of topics, including HTML/CSS basics, JavaScript basics, frameworks and libraries, project experience, algorithms and data structures, performance optimization, cross-domain requests, front-end engineering, design patterns, and new technologies and trends. . Interviewer questions are designed to assess the candidate's technical skills, project experience, and understanding of industry trends. Therefore, candidates should be fully prepared in these areas to demonstrate their abilities and expertise.

How to use Go language for front-end development? How to use Go language for front-end development? Jun 10, 2023 pm 05:00 PM

With the development of Internet technology, front-end development has become increasingly important. Especially the popularity of mobile devices requires front-end development technology that is efficient, stable, safe and easy to maintain. As a rapidly developing programming language, Go language has been used by more and more developers. So, is it feasible to use Go language for front-end development? Next, this article will explain in detail how to use Go language for front-end development. Let’s first take a look at why Go language is used for front-end development. Many people think that Go language is a

See all articles