search
HomeWeb Front-endCSS TutorialHow to achieve flip effect in cs

How to achieve flip effect in cs

Apr 13, 2021 am 10:40 AM
css3 animationfront 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>
    </p><p>
        </p><p>
            <img  src="/static/imghwm/default1.png" data-src="images/pic00.jpg" class="lazy" alt="How to achieve flip effect in cs" >
        </p>
        <p>
            <img  src="/static/imghwm/default1.png" data-src="images/pic01.jpg" class="lazy" alt="How to achieve flip effect in cs" >
        </p>
    
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;}
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;}
(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);}
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
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
@keyframes vs CSS Transitions: What is the difference?@keyframes vs CSS Transitions: What is the difference?May 14, 2025 am 12:01 AM

@keyframesandCSSTransitionsdifferincomplexity:@keyframesallowsfordetailedanimationsequences,whileCSSTransitionshandlesimplestatechanges.UseCSSTransitionsforhovereffectslikebuttoncolorchanges,and@keyframesforintricateanimationslikerotatingspinners.

Using Pages CMS for Static Site Content ManagementUsing Pages CMS for Static Site Content ManagementMay 13, 2025 am 09:24 AM

I know, I know: there are a ton of content management system options available, and while I've tested several, none have really been the one, y'know? Weird pricing models, difficult customization, some even end up becoming a whole &

The Ultimate Guide to Linking CSS Files in HTMLThe Ultimate Guide to Linking CSS Files in HTMLMay 13, 2025 am 12:02 AM

Linking CSS files to HTML can be achieved by using elements in part of HTML. 1) Use tags to link local CSS files. 2) Multiple CSS files can be implemented by adding multiple tags. 3) External CSS files use absolute URL links, such as. 4) Ensure the correct use of file paths and CSS file loading order, and optimize performance can use CSS preprocessor to merge files.

CSS Flexbox vs Grid: a comprehensive reviewCSS Flexbox vs Grid: a comprehensive reviewMay 12, 2025 am 12:01 AM

Choosing Flexbox or Grid depends on the layout requirements: 1) Flexbox is suitable for one-dimensional layouts, such as navigation bar; 2) Grid is suitable for two-dimensional layouts, such as magazine layouts. The two can be used in the project to improve the layout effect.

How to Include CSS Files: Methods and Best PracticesHow to Include CSS Files: Methods and Best PracticesMay 11, 2025 am 12:02 AM

The best way to include CSS files is to use tags to introduce external CSS files in the HTML part. 1. Use tags to introduce external CSS files, such as. 2. For small adjustments, inline CSS can be used, but should be used with caution. 3. Large projects can use CSS preprocessors such as Sass or Less to import other CSS files through @import. 4. For performance, CSS files should be merged and CDN should be used, and compressed using tools such as CSSNano.

Flexbox vs Grid: should I learn them both?Flexbox vs Grid: should I learn them both?May 10, 2025 am 12:01 AM

Yes,youshouldlearnbothFlexboxandGrid.1)Flexboxisidealforone-dimensional,flexiblelayoutslikenavigationmenus.2)Gridexcelsintwo-dimensional,complexdesignssuchasmagazinelayouts.3)Combiningbothenhanceslayoutflexibilityandresponsiveness,allowingforstructur

Orbital Mechanics (or How I Optimized a CSS Keyframes Animation)Orbital Mechanics (or How I Optimized a CSS Keyframes Animation)May 09, 2025 am 09:57 AM

What does it look like to refactor your own code? John Rhea picks apart an old CSS animation he wrote and walks through the thought process of optimizing it.

CSS Animations: Is it hard to create them?CSS Animations: Is it hard to create them?May 09, 2025 am 12:03 AM

CSSanimationsarenotinherentlyhardbutrequirepracticeandunderstandingofCSSpropertiesandtimingfunctions.1)Startwithsimpleanimationslikescalingabuttononhoverusingkeyframes.2)Useeasingfunctionslikecubic-bezierfornaturaleffects,suchasabounceanimation.3)For

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

SublimeText3 English version

SublimeText3 English version

Recommended: Win version, supports code prompts!

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.

ZendStudio 13.5.1 Mac

ZendStudio 13.5.1 Mac

Powerful PHP integrated development environment

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor