css+div实现一个三维骰子旋转动画效果代码

代码语言:html

所属分类:三维

代码描述:css+div实现一个三维骰子旋转动画效果代码

代码标签: 三维 骰子 旋转 动画 效果

下面为部分代码预览,完整代码请点击下载或在bfwstudio webide中打开


<!DOCTYPE html>
<html lang="en" >

<head>

  <meta charset="UTF-8">

<style>
@charset "UTF-8";
html,
body {
  width: 100%;
  height: 100%;
}

body {
  background-color: #f7f7f7;
}

:root {
  --width: 280px;
  --circle-width: 40px;
}

body {
  text-align: center;
  display: -webkit-box;
  display: flex;
  -webkit-box-pack: center;
          justify-content: center;
  -webkit-box-align: center;
          align-items: center;
  overflow: hidden;
}

.container {
  position: relative;
  -webkit-perspective: 1000px;
          perspective: 1000px;
  -webkit-perspective-origin: 50% 50%;
          perspective-origin: 50% 50%;
}

.cube {
  position: relative;
  top: 0;
  left: 0;
  -webkit-transform-style: preserve-3d;
          transform-style: preserve-3d;
  width: var(--width);
  height: var(--width);
  -webkit-animation: rotate 7s infinite linear;
          animation: rotate 7s infinite linear;
  -webkit-transform: rotateY(180deg) rotateX(180deg);
          transform: rotateY(180deg) rotateX(180deg);
}
@-webkit-keyframes rotate {
  0% {
    -webkit-transform: rotateY(0deg) rotateX(0deg);
            transform: rotateY(0deg) rotateX(0deg);
  }
  100% {
    -webkit-transform: rotateY(360deg) rotateX(360deg);
            transform: rotateY(360deg) rotateX(360deg);
  }
}
@keyframes rotate {
  0% {
    -webkit-transform: rotateY(0deg) rotateX(0deg);
            transform: rotateY(0deg) rotateX(0deg);
  }
  100% {
    -webkit-transform: rotateY(360deg) rotateX(360deg);
            transform: rotateY(360deg) rotateX(360deg);
  }
}
.surface {
  border: 1px solid #666;
  position: absolute;
  width: var(--width);
  height: var(--width);
  top: 0;
  left: 0;
  overflow: hidden;
  background-position: 50% 50%;
  display: grid;
  grid-template: repeat(3, calc(100% / 3))/repeat(3, calc(100% / 3));
  justify-items: center;
  -webkit-box-align: center;
          align-items: center;
  background-color: #fff;
}
.surface .circle {
  background-color: #130707;
  width: var(--circle-width);
  height: var(--circle-width);
  border-radius: 50%;
}

/* front */
.surface:nth-of-type(1) {
  -webkit-transform: translateZ(calc(var(--width) / 2));
          transform: translateZ(calc(var(--width) / 2));
}

/* right */
.surface:nth-of-type(2) {
  -webkit-transform: rotateY(90deg) translateZ(calc(var(--width) / 2));
          transform: rotateY(90deg) translateZ(calc(var(--width) / 2));
}

/* back */
.surface:nth-of-type(3) {
  -webkit-transform: rotateX(-90deg) translateZ(calc(var(--width) / 2));
          transform: rotateX(-90deg) translateZ(calc(var(--width) / 2));
}

/* left */
.surface:nth-of-type(4) {
  -webkit-transform: rotateY(180deg) translateZ(calc(var(--width) / 2));
          transform: rotateY(180deg) translateZ(calc(var(--width) / 2));
}

/* top */
.surface:nth-of-type(5) {
  -webkit-transform: rotateY(-90deg) translateZ(calc(var(--width) / 2));
          transform: rotateY(-90deg) translateZ(calc(var(--width) / 2));
}

.surface:nth-of-type(6) {
  -webkit-transform: rotateX(90deg) translateZ(calc(var(--width) / 2));
          transform: rotateX(90deg) translateZ(calc(var(--width) / 2));
}

.surface:nth-of-type(1) .circle:nth-child(1),
.surface:nth-of-type(4) .circle:nth-child(1) {
  grid-area: 1 / 1;
}
.surface:nth-of-type(1) .circle:nth-child(2),
.surface:nth-of-type(4) .circle:nth-child(2) {
  grid-area: 3 / 3;
}
.surface:nth-of-type(1) .circle:nth-child(3),
.surface:nth-of-type(4) .circle:nth-child(3) {
  grid-area: 1 / 3;
}
.surface:nth-of-type(.........完整代码请登录后点击上方下载按钮下载查看

网友评论0