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

Steps to enable TLS in Docker for secure configuration

Preface I had previously enabled Docker's 237...

jQuery implements simple button color change

In HTML and CSS, we want to set the color of a bu...

Four ways to create objects in JS

Table of contents 1. Create objects by literal va...

Detailed explanation of MySQL trigger trigger example

Table of contents What is a trigger Create a trig...

Why is it not recommended to use index as key in react?

1. Compare the old virtual DOM with the new virtu...

A complete list of commonly used HTML tags and their characteristics

First of all, you need to know some characteristi...

How to enable Flash in Windows Server 2016

I recently deployed and tested VMware Horizon, an...

Button is stretched on both sides in IE

When you write buttons (input, button), you will f...

Docker commands are implemented so that ordinary users can execute them

After installing docker, there will usually be a ...

Solution for applying CSS3 transforms to background images

CSS transformations, while cool, have not yet bee...

Css3 realizes seamless scrolling and anti-shake

question The seamless scrolling of pictures and t...