使用css3实现骰子动画

思路:骰子有6个面,那么创建6个div为6个面,然后用css3的旋转属性旋转每个面到相应位置,每个面里面的点使用绝对定位确实位置。骰子旋转写一个动画改变rotate值。
效果预览:https://leeseean.github.io/cs…

结构html:

<div class="diceWrap">
        <div class="dice dice1">
            <div class="dot"></div>
        </div>
        <div class="dice dice1 inner"></div>
        <div class="dice dice2">
            <div class="dot"></div>
            <div class="dot"></div>
        </div>
        <div class="dice dice2 inner"></div>
        <div class="dice dice3">
            <div class="dot"></div>
            <div class="dot"></div>
            <div class="dot"></div>
        </div>
        <div class="dice dice3 inner"></div>
        <div class="dice dice4">
            <div class="dot"></div>
            <div class="dot"></div>
            <div class="dot"></div>
            <div class="dot"></div>
        </div>
        <div class="dice dice4 inner"></div>
        <div class="dice dice5">
            <div class="dot"></div>
            <div class="dot"></div>
            <div class="dot"></div>
            <div class="dot"></div>
            <div class="dot"></div>
        </div>
        <div class="dice dice5 inner"></div>
        <div class="dice dice6">
            <div class="dot"></div>
            <div class="dot"></div>
            <div class="dot"></div>
            <div class="dot"></div>
            <div class="dot"></div>
            <div class="dot"></div>
        </div>
        <div class="dice dice6 inner"></div>
        <dov class="dice cover x"></dov>
        <dov class="dice cover y"></dov>
        <dov class="dice cover z"></dov>
    </div>

css样式:

        body {
            background-color: darkkhaki;
            perspective: 500px;
        }

        .diceWrap {
            position: relative;
            transform-style: preserve-3d;
            animation: roll infinite 10s; 
            width: 100px;
            height: 100px;
            left: 50%;
            top: 200px;
        }

        .diceWrap>.dice {
            width: 100px;
            height: 100px;
            border-radius: 10px;
            position: absolute;
            background-color: black;
            box-shadow: #D2D2D2 0px 0px 10px 0px inset;
        }
        .dice.dice1 {
            transform: translateZ(50px);
        }
        .dice.dice1.inner {
            transform: translateZ(49px);
        }

        .dice.dice2 {
            transform: rotateX(-180deg) translateZ(50px);
        }
        .dice.dice2.inner {
            transform: rotateX(-180deg) translateZ(49px);
        }

        .dice.dice3 {
            transform: rotateX(90deg) translateZ(50px);
        }
        .dice.dice3.inner {
            transform: rotateX(90deg) translateZ(49px);
        }

        .dice.dice4 {
            transform: rotateX(-90deg) translateZ(50px);
        }
        .dice.dice4.inner {
            transform: rotateX(-90deg) translateZ(49px);
        }

        .dice.dice5 {
            transform: rotateY(90deg) translateZ(50px);
        }
        .dice.dice5.inner {
            transform: rotateY(90deg) translateZ(49px);
        }

        .dice.dice6 {
            transform: rotateY(-90deg) translateZ(50px);
        }
        .dice.dice6.inner {
            transform: rotateY(-90deg) translateZ(49px);
        }

        .diceWrap>.dice>.dot {
            position: absolute;
            width: 20px;
            height: 20px;
            border-radius: 50%;
        }

        .diceWrap>.dice1>.dot {
            left: 50%;
            top: 50%;
            margin-left: -10px;
            margin-top: -10px;
            background-color: red;
        }

        .diceWrap>.dice2>.dot {
            background-color: orange;
        }

        .diceWrap>.dice2>.dot:nth-child(1) {
            left: 5%;
            top: 5%;
        }

        .diceWrap>.dice2>.dot:nth-child(2) {
            right: 5%;
            bottom: 5%;
        }

        .diceWrap>.dice3>.dot {
            background-color: yellow;
        }

        .diceWrap>.dice3>.dot:nth-child(1) {
            left: 5%;
            top: 5%;
        }

        .diceWrap>.dice3>.dot:nth-child(2) {
            left: 50%;
            top: 50%;
            margin-left: -10px;
            margin-top: -10px;
        }

        .diceWrap>.dice3>.dot:nth-child(3) {
            right: 5%;
            bottom: 5%;
        }

        .diceWrap>.dice4>.dot {
            background-color: green;
        }

        .diceWrap>.dice4>.dot:nth-child(1) {
            left: 5%;
            top: 5%;
        }

        .diceWrap>.dice4>.dot:nth-child(2) {
            left: 5%;
            bottom: 5%;
        }

        .diceWrap>.dice4>.dot:nth-child(3) {
            right: 5%;
            top: 5%;
        }

        .diceWrap>.dice4>.dot:nth-child(4) {
            right: 5%;
            bottom: 5%;
        }

        .diceWrap>.dice5>.dot {
            background-color: greenyellow;
        }

        .diceWrap>.dice5>.dot:nth-child(1) {
            left: 5%;
            top: 5%;
        }

        .diceWrap>.dice5>.dot:nth-child(2) {
            left: 5%;
            bottom: 5%;
        }

        .diceWrap>.dice5>.dot:nth-child(3) {
            left: 50%;
            top: 50%;
            margin-left: -10px;
            margin-top: -10px;
        }

        .diceWrap>.dice5>.dot:nth-child(4) {
            right: 5%;
            top: 5%;
        }

        .diceWrap>.dice5>.dot:nth-child(5) {
            right: 5%;
            bottom: 5%;
        }

        .diceWrap>.dice6>.dot {
            background-color: purple;
        }

        .diceWrap>.dice6>.dot:nth-child(1) {
            left: 5%;
            top: 5%;
        }

        .diceWrap>.dice6>.dot:nth-child(2) {
            left: 5%;
            bottom: 5%;
        }

        .diceWrap>.dice6>.dot:nth-child(3) {
            left: 5%;
            top: 50%;
            margin-top: -10px;
        }

        .diceWrap>.dice6>.dot:nth-child(4) {
            right: 5%;
            top: 5%;
        }

        .diceWrap>.dice6>.dot:nth-child(5) {
            right: 5%;
            top: 50%;
            margin-top: -10px;
        }

        .diceWrap>.dice6>.dot:nth-child(6) {
            right: 5%;
            bottom: 5%;
        }
        .diceWrap>.cover, .diceWrap>.inner {
            background: rgb(65,65,65);
            box-shadow: none;
        }
        .diceWrap>.cover {
            border-radius: 0;
            transform: translateZ(0px);
        }
        .diceWrap>.cover.x {
            transform: rotateY(90deg);
        }
        .diceWrap>.cover.z {
            transform: rotateX(90deg);
        }
        @keyframes roll {
            0% {
                -webkit-transform: rotate3d(1, 1, 1, 0deg) rotateY(0deg) scale3d(0.5, 0.5, 0.5);
                transform: rotate3d(1, 1, 1, 0deg) rotateY(0deg) scale3d(0.5, 0.5, 0.5);
            }
            50% {
                -webkit-transform: rotate3d(1, 1, 1, 360deg) rotateY(360deg) scale3d(1, 1, 1);
                transform: rotate3d(1, 1, 1, 360deg) rotateY(360deg) scale3d(1, 1, 1);
            }
            100% {
                -webkit-transform: rotate3d(1, 1, 1, 720deg) rotateY(720deg) scale3d(0.5, 0.5, 0.5);
                transform: rotate3d(1, 1, 1, 720deg) rotateY(720deg) scale3d(0.5, 0.5, 0.5);
            }
        } 
    

用js控制旋转结果:

 //动画配置开始
    let diceWrap = document.querySelector('.diceWrap');
    //不同开奖号码的配置,不同数字代表不同开奖结果
    let keyframes = {
        '1': [{
            transform: 'rotate3d(1, 1, 1, 0deg) rotateX(-270deg) scale3d(1, 1, 1)'
        }, {
            transform: 'rotate3d(1, 1, 1, 360deg) rotateX(90deg) scale3d(1, 1, 1)'
        }],
        '2': [{
            transform: 'rotate3d(1, 1, 1, 0deg) rotateX(-90deg) scale3d(1, 1, 1)'
        }, {
            transform: 'rotate3d(1, 1, 1, 360deg) rotateX(270deg) scale3d(1, 1, 1)'
        }],
        '3': [{
            transform: 'rotate3d(1, 1, 1, 0deg) rotateY(0deg) scale3d(1, 1, 1)'
        }, {
            transform: 'rotate3d(1, 1, 1, 360deg) rotateY(360deg) scale3d(1, 1, 1)'
        }],
        '4': [{
            transform: 'rotate3d(1, 1, 1, 0deg) rotateX(-180deg) scale3d(1, 1, 1)'
        }, {
            transform: 'rotate3d(1, 1, 1, 360deg) rotateX(180deg) scale3d(1, 1, 1)'
        }],
        '5': [{
            transform: 'rotate3d(1, 1, 1, 0deg) rotateZ(-90deg) scale3d(1, 1, 1)'
        }, {
            transform: 'rotate3d(1, 1, 1, 360deg) rotateZ(270deg) scale3d(1, 1, 1)'
        }],
        '6': [{
            transform: 'rotate3d(1, 1, 1, 0deg) rotateZ(-270deg) scale3d(1, 1, 1)'
        }, {
            transform: 'rotate3d(1, 1, 1, 360deg) rotateZ(90deg) scale3d(1, 1, 1)'
        }]
    }
    let animationConfig = {
        duration: 200,
        iterations: 10,
        fill: 'forwards', //当动画完成后,保持最后一个属性值(在最后一个关键帧中定义)
    };
    //动画配置结束
    //旋转函数
    function roll(number) {
        let timeout = null;
        let diceAnimate = diceWrap.animate(
            keyframes[number],
            animationConfig
        );
        let startTime = new Date().getTime();

        function playbackRate() {
            diceAnimate.playbackRate *= 0.93;
            timeout = setTimeout(playbackRate, 200);
            if (diceAnimate.playbackRate <= 0.3) {
                console.log(12222)
                clearTimeout(timeout);
            }
        }
        playbackRate();
    }

项目github地址

https://github.com/leeseean/c…

    原文作者:all2005
    原文地址: https://segmentfault.com/a/1190000010420473
    本文转自网络文章,转载此文章仅为分享知识,如有侵权,请联系博主进行删除。
点赞