CSS3 flip card number sample code

CSS3 flip card number sample code

I received a task from the company today, and the effect diagram is as follows:

I didn’t specify what effect I wanted to achieve, but I thought pure digital conversion was too simple, so I just made a card flipping effect.

Effect preview, open in a new window: https://codepen.io/andy-js/pen/ExaGxaL

First do some page layout:

<ul></ul>
 body{background: #000;}
     ul{
         list-style: none;
         margin:100px 0;
         display: flex;
         justify-content:center;
         line-height: 56px;
         height:56px;
         font-size: 39.6px;
         color: #FFFFFF;
         transform-style:preserve-3d;
         perspective:1000px;
     }
     li{
        height:56px;
        margin:0 10px;
        background:none;
        width:16px;
        position: relative;
     }
     .num{
        width:46px;
        transform-style:preserve-3d;
        perspective:1000px;
        transform:rotateY(0deg);
        transition: 1s all ease;
    }
    p{
        height:56px;
        width:46px;
        text-align: center;
        background: #EC2C5C;
        border-radius: 2.57px;
        position: absolute;
    }
    
    p:nth-child(2){
        transform: scalex(-1) translateZ(-1px);
    }

Then, through dynamic insertion, such an effect is simulated. Transition is used to make the animation. Transform:rotateY is used to make the flip. Before flipping, another number scalex is mirrored left and right, and then translateZ (-1px) is moved to the back of the other number. In this way, you can see the mirrored number when you flip it over. It is not perfect, and there are still many places that can be improved. The full code is as follows:

<script>
var number=9999993,
    numArr=addComma(number),
    aNum=[],
    oUl=document.getElementsByTagName('ul')[0];
for(let i=0;i<numArr.length;i++){
    let li = document.createElement('li');
    oUl.appendChild(li);
    if(numArr[i]==',')
    li.innerHTML=',';
    else
    li.innerHTML='<p>'+numArr[i]+'</p><p></p>',
    li.className='num',
    li.deg=0,
    aNum.push(li);
};

setInterval(function(){
    let changeNum=number+1+'';
    let changeArr = addComma(changeNum),
        difference=changeArr.length-numArr.length;
    if(difference){
        for(let i=0;i<difference;i++){
            let li = document.createElement('li');
            oUl.insertBefore(li,oUl.children[0]);
            if(changeArr[i]==',')
            li.innerHTML=',';
            else
            li.innerHTML='<p>'+changeArr[i]+'</p><p></p>',
            li.className='num',
            li.deg=0,
            aNum.unshift(li);
        };
    };
    number+='';
    for(let i=changeNum.length-number.length;i<changeNum.length;i++){
        if(changeNum[i]==number[i])continue;
        let deg = aNum[i].deg;
        aNum[i].deg=deg=deg+180;
        let index=(deg/180)%2;
        aNum[i].children[index].innerHTML=changeNum[i];
        aNum[i].style.transform='rotateY('+deg+'deg)';
    };
    number=Number(changeNum);
    numArr=changeArr;
},2000);


function addComma(num){ //Add a bean number for every three digits return (num || 0).toString().replace(/(\d)(?=(?:\d{3})+$)/g, '$1,');
};
</script>

The above is the full content of this article. I hope it will be helpful for everyone’s study. I also hope that everyone will support 123WORDPRESS.COM.

<<:  Introduction to the use of data custom attributes in HTML and plug-in applications

>>:  Semantics: Is Html/Xhtml really standards-compliant?

Recommend

Detailed summary of MySQL and connection-related timeouts

MySQL and connection related timeouts Preface: To...

Example of how to embed H5 in WeChat applet webView

Preface WeChat Mini Programs provide new open cap...

Teach you to connect to MySQL database using eclipse

Preface Since errors always occur, record the pro...

Detailed explanation of the process of installing msf on Linux system

Or write down the installation process yourself! ...

js memory leak scenarios, how to monitor and analyze them in detail

Table of contents Preface What situations can cau...

Node.js makes a simple crawler case tutorial

Preparation First, you need to download nodejs, w...

js uses Canvas to merge multiple pictures into one implementation code

Solution function mergeImgs(list) { const imgDom ...

CSS solution for centering elements with variable width and height

1. Horizontal center Public code: html: <div c...

Vue implements simple calculator function

This article example shares the specific code of ...

Front-end AI cutting tips (experience)

AI image cutting needs to be coordinated with PS....

Example of automatic stop effect after text scrolling

The effect is very simple, just copy the following...

A Brief Analysis of MySQL Connections and Collections

Join query A join query refers to a matching quer...

Detailed explanation of the use of Vue.js draggable text box component

Table of contents Registering Components Adding C...

Detailed example of removing duplicate data in MySQL

Detailed example of removing duplicate data in My...