以下是 HTML5多立方体3D翻转效果代码 的示例演示效果:
部分效果截图:
HTML代码(index.html):
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>HTML5多立方体3D翻转效果</title>
<style>
* {
box-sizing: border-box;
}
html, body {
height: 100%;
}
body {
overflow: hidden;
background: black;
}
.wrap {
height: 100%;
transform-style: preserve-3d;
perspective: 400px;
width: 1000px;
margin: 0 auto;
}
.box {
transform-style: preserve-3d;
perspective: 5000px;
position: absolute;
width: 50px;
height: 200px;
top: 50%;
margin-top: -100px;
animation: spin 3s infinite alternate;
}
.side, .top, .bottom {
position: absolute;
}
.top, .bottom {
width: 50px;
height: 50px;
}
.top {
transform: translateZ(100px);
}
.bottom {
transform: rotateY(180deg) translateZ(100px);
}
.side:nth-child(3) {
transform: rotateX(90deg) translateZ(50px);
width: 50px;
height: 200px;
}
.side:nth-child(4) {
width: 200px;
height: 50px;
transform: rotateY(90deg) translateZ(-100px);
}
.side:nth-child(5) {
width: 200px;
height: 50px;
transform: rotateY(-90deg) translateZ(50px);
}
.side:nth-child(6) {
width: 50px;
height: 200px;
transform: rotateX(-90deg) translateZ(-100px);
}
.box:nth-child(1) {
left: 0px;
animation-delay: -0.075s;
}
.box:nth-child(1) div {
background: rgba(255, 64, 0, 0.3);
border: 1px solid #ff4000;
}
.box:nth-child(2) {
left: 50px;
animation-delay: -0.15s;
}
.box:nth-child(2) div {
background: rgba(255, 128, 0, 0.3);
border: 1px solid #ff8000;
}
.box:nth-child(3) {
left: 100px;
animation-delay: -0.225s;
}
.box:nth-child(3) div {
background: rgba(255, 191, 0, 0.3);
border: 1px solid #ffbf00;
}
.box:nth-child(4) {
left: 150px;
animation-delay: -0.3s;
}
.box:nth-child(4) div {
background: rgba(255, 255, 0, 0.3);
border: 1px solid yellow;
}
.box:nth-child(5) {
left: 200px;
animation-delay: -0.375s;
}
.box:nth-child(5) div {
background: rgba(191, 255, 0, 0.3);
border: 1px solid #bfff00;
}
.box:nth-child(6) {
left: 250px;
animation-delay: -0.45s;
}
.box:nth-child(6) div {
background: rgba(128, 255, 0, 0.3);
border: 1px solid #80ff00;
}
.box:nth-child(7) {
left: 300px;
animation-delay: -0.525s;
}
.box:nth-child(7) div {
background: rgba(64, 255, 0, 0.3);
border: 1px solid #40ff00;
}
.box:nth-child(8) {
left: 350px;
animation-delay: -0.6s;
}
.box:nth-child(8) div {
background: rgba(0, 255, 0, 0.3);
border: 1px solid lime;
}
.box:nth-child(9) {
left: 400px;
animation-delay: -0.675s;
}
.box:nth-child(9) div {
background: rgba(0, 255, 64, 0.3);
border: 1px solid #00ff40;
}
.box:nth-child(10) {
left: 450px;
animation-delay: -0.75s;
}
.box:nth-child(10) div {
background: rgba(0, 255, 128, 0.3);
border: 1px solid #00ff80;
}
.box:nth-child(11) {
left: 500px;
animation-delay: -0.825s;
}
.box:nth-child(11) div {
background: rgba(0, 255, 191, 0.3);
border: 1px solid #00ffbf;
}
.box:nth-child(12) {
left: 550px;
animation-delay: -0.9s;
}
.box:nth-child(12) div {
background: rgba(0, 255, 255, 0.3);
border: 1px solid cyan;
}
.box:nth-child(13) {
left: 600px;
animation-delay: -0.975s;
}
.box:nth-child(13) div {
background: rgba(0, 191, 255, 0.3);
border: 1px solid deepskyblue;
}
.box:nth-child(14) {
left: 650px;
animation-delay: -1.05s;
}
.box:nth-child(14) div {
background: rgba(0, 127, 255, 0.3);
border: 1px solid #007fff;
}
.box:nth-child(15) {
left: 700px;
animation-delay: -1.125s;
}
.box:nth-child(15) div {
background: rgba(0, 64, 255, 0.3);
border: 1px solid #0040ff;
}
.box:nth-child(16) {
left: 750px;
animation-delay: -1.2s;
}
.box:nth-child(16) div {
background: rgba(0, 0, 255, 0.3);
border: 1px solid blue;
}
.box:nth-child(17) {
left: 800px;
animation-delay: -1.275s;
}
.box:nth-child(17) div {
background: rgba(64, 0, 255, 0.3);
border: 1px solid #4000ff;
}
.box:nth-child(18) {
left: 850px;
animation-delay: -1.35s;
}
.box:nth-child(18) div {
background: rgba(127, 0, 255, 0.3);
border: 1px solid #7f00ff;
}
.box:nth-child(19) {
left: 900px;
animation-delay: -1.425s;
}
.box:nth-child(19) div {
background: rgba(191, 0, 255, 0.3);
border: 1px solid #bf00ff;
}
.box:nth-child(20) {
left: 950px;
animation-delay: -1.5s;
}
.box:nth-child(20) div {
background: rgba(255, 0, 255, 0.3);
border: 1px solid magenta;
}
@keyframes spin {
0% {
transform: translateZ(-300px) rotateX(90deg);
}
25% {
transform: translateZ(-300px) rotateX(90deg);
}
75% {
transform: rotateX(-90deg);
}
100% {
transform: rotateX(-90deg);
}
}
</style>
<script src="js/prefixfree.min.js"></script>
</head>
<body>
<div class='wrap'>
<div class='box'>
<div class='top'></div>
<div class='bottom'></div>
<div class='side'></div>
<div class='side'></div>
<div class='side'></div>
<div class='side'></div>
</div>
<div class='box'>
<div class='top'></div>
<div class='bottom'></div>
<div class='side'></div>
<div class='side'></div>
<div class='side'></div>
<div class='side'></div>
</div>
<div class='box'>
<div class='top'></div>
<div class='bottom'></div>
<div class='side'></div>
<div class='side'></div>
<div class='side'></div>
<div class='side'></div>
</div>
<div class='box'>
<div class='top'></div>
<div class='bottom'></div>
<div class='side'></div>
<div class='side'></div>
<div class='side'></div>
<div class='side'></div>
</div>
<div class='box'>
<div class='top'></div>
<div class='bottom'></div>
<div class='side'></div>
<div class='side'></div>
<div class='side'></div>
<div class='side'></div>
</div>
<div class='box'>
<div class='top'></div>
<div class='bottom'></div>
<div class='side'></div>
<div class='side'></div>
<div class='side'></div>
<div class='side'></div>
</div>
<div class='box'>
<div class='top'></div>
<div class='bottom'></div>
<div class='side'></div>
<div class='side'></div>
<div class='side'></div>
<div class='side'></div>
</div>
<div class='box'>
<div class='top'></div>
<div class='bottom'></div>
<div class='side'></div>
<div class='side'></div>
<div class='side'></div>
<div class='side'></div>
</div>
<div class='box'>
<div class='top'></div>
<div class='bottom'></div>
<div class='side'></div>
<div class='side'></div>
<div class='side'></div>
<div class='side'></div>
</div>
<div class='box'>
<div class='top'></div>
<div class='bottom'></div>
<div class='side'></div>
<div class='side'></div>
<div class='side'></div>
<div class='side'></div>
</div>
<div class='box'>
<div class='top'></div>
<div class='bottom'></div>
<div class='side'></div>
<div class='side'></div>
<div class='side'></div>
<div class='side'></div>
</div>
<div class='box'>
<div class='top'></div>
<div class='bottom'></div>
<div class='side'></div>
<div class='side'></div>
<div class='side'></div>
<div class='side'></div>
</div>
<div class='box'>
<div class='top'></div>
<div class='bottom'></div>
<div class='side'></div>
<div class='side'></div>
<div class='side'></div>
<div class='side'></div>
</div>
<div class='box'>
<div class='top'></div>
<div class='bottom'></div>
<div class='side'></div>
<div class='side'></div>
<div class='side'></div>
<div class='side'></div>
</div>
<div class='box'>
<div class='top'></div>
<div class='bottom'></div>
<div class='side'></div>
<div class='side'></div>
<div class='side'></div>
<div class='side'></div>
</div>
<div class='box'>
<div class='top'></div>
<div class='bottom'></div>
<div class='side'></div>
<div class='side'></div>
<div class='side'></div>
<div class='side'></div>
</div>
<div class='box'>
<div class='top'></div>
<div class='bottom'></div>
<div class='side'></div>
<div class='side'></div>
<div class='side'></div>
<div class='side'></div>
</div>
<div class='box'>
<div class='top'></div>
<div class='bottom'></div>
<div class='side'></div>
<div class='side'></div>
<div class='side'></div>
<div class='side'></div>
</div>
<div class='box'>
<div class='top'></div>
<div class='bottom'></div>
<div class='side'></div>
<div class='side'></div>
<div class='side'></div>
<div class='side'></div>
</div>
<div class='box'>
<div class='top'></div>
<div class='bottom'></div>
<div class='side'></div>
<div class='side'></div>
<div class='side'></div>
<div class='side'></div>
</div>
</div>
</body>
</html>
JS代码(prefixfree.min.js):
!function(){
function e(e,r){
return[].slice.call((r||document).querySelectorAll(e))}
if(window.addEventListener){
var r=window.StyleFix={
link:function(e){
try{
if("stylesheet"!==e.rel||e.hasAttribute("data-noprefix"))return}
catch(t){
return}
var n,i=e.href||e.getAttribute("data-href"),a=i.replace(/[^\/]+$/,""),o=(/^[a-z]{
3,10}
:/.exec(a)||[""])[0],s=(/^[a-z]{
3,10}
:\/\/[^\/]+/.exec(a)||[""])[0],l=/^([^?]*)\??/.exec(i)[1],u=e.parentNode,p=new XMLHttpRequest;
p.onreadystatechange=function(){
4===p.readyState&&n()}
,n=function(){
var t=p.responseText;
if(t&&e.parentNode&&(!p.status||p.status<400||p.status>600)){
if(t=r.fix(t,!0,e),a){
t=t.replace(/url\(\s*?((?:"|')?)(.+?)\1\s*?\)/gi,function(e,r,t){
return/^([a-z]{
3,10}
:|#)/i.test(t)?e:/^\/\//.test(t)?'url("'+o+t+'")':/^\//.test(t)?'url("'+s+t+'")':/^\?/.test(t)?'url("'+l+t+'")':'url("'+a+t+'")'}
);
var n=a.replace(/([\\\^\$*+[\]?{
}
.=!:(|)])/g,"\\$1");
t=t.replace(RegExp("\\b(behavior:\\s*?url\\('?\"?)"+n,"gi"),"$1")}
var i=document.createElement("style");
i.textContent=t,i.media=e.media,i.disabled=e.disabled,i.setAttribute("data-href",e.getAttribute("href")),u.insertBefore(i,e),u.removeChild(e),i.media=e.media}
}
;
try{
p.open("GET",i),p.send(null)}
catch(t){
"undefined"!=typeof XDomainRequest&&(p=new XDomainRequest,p.onerror=p.onprogress=function(){
}
,p.onload=n,p.open("GET",i),p.send(null))}
e.setAttribute("data-inprogress","")}
,styleElement:function(e){
if(!e.hasAttribute("data-noprefix")){
var t=e.disabled;
e.textContent=r.fix(e.textContent,!0,e),e.disabled=t}
}
,styleAttribute:function(e){
var t=e.getAttribute("style");
t=r.fix(t,!1,e),e.setAttribute("style",t)}
,process:function(){
e("style").forEach(StyleFix.styleElement),e("[style]").forEach(StyleFix.styleAttribute)}
,register:function(e,t){
(r.fixers=r.fixers||[]).splice(void 0===t?r.fixers.length:t,0,e)}
,fix:function(e,t,n){
for(var i=0;
i<r.fixers.length;
i++)e=r.fixers[i](e,t,n)||e;
return e}
,camelCase:function(e){
return e.replace(/-([a-z])/g,function(e,r){
return r.toUpperCase()}
).replace("-","")}
,deCamelCase:function(e){
return e.replace(/[A-Z]/g,function(e){
return"-"+e.toLowerCase()}
)}
}
;
!function(){
setTimeout(function(){
}
,10),document.addEventListener("DOMContentLoaded",StyleFix.process,!1)}
()}
}
(),function(e){
function r(e,r,n,i,a){
if(e=t[e],e.length){
var o=RegExp(r+"("+e.join("|")+")"+n,"gi");
a=a.replace(o,i)}
return a}
if(window.StyleFix&&window.getComputedStyle){
var t=window.PrefixFree={
prefixCSS:function(e,n){
var i=t.prefix;
if(t.functions.indexOf("linear-gradient")>-1&&(e=e.replace(/(\s|:|,)(repeating-)?linear-gradient\(\s*(-?\d*\.?\d*)deg/gi,function(e,r,t,n){
return r+(t||"")+"linear-gradient("+(90-n)+"deg"}
)),e=r("functions","(\\s|:|,)","\\s*\\(","$1"+i+"$2(",e),e=r("keywords","(\\s|:)","(\\s|;
|\\}
|$)","$1"+i+"$2$3",e),e=r("properties","(^|\\{
|\\s|;
)","\\s*:","$1"+i+"$2:",e),t.properties.length){
var a=RegExp("\\b("+t.properties.join("|")+")(?!:)","gi");
e=r("valueProperties","\\b",":(.+?);
",function(e){
return e.replace(a,i+"$1")}
,e)}
return n&&(e=r("selectors","","\\b",t.prefixSelector,e),e=r("atrules","@","\\b","@"+i+"$1",e)),e=e.replace(RegExp("-"+i,"g"),"-"),e=e.replace(/-\*-(?=[a-z]+)/gi,t.prefix)}
,property:function(e){
return(t.properties.indexOf(e)?t.prefix:"")+e}
,value:function(e){
return e=r("functions","(^|\\s|,)","\\s*\\(","$1"+t.prefix+"$2(",e),e=r("keywords","(^|\\s)","(\\s|$)","$1"+t.prefix+"$2$3",e)}
,prefixSelector:function(e){
return e.replace(/^:{
1,2}
/,function(e){
return e+t.prefix}
)}
,prefixProperty:function(e,r){
var n=t.prefix+e;
return r?StyleFix.camelCase(n):n}
}
;
!function(){
var e={
}
,r=[],n=getComputedStyle(document.documentElement,null),i=document.createElement("div").style,a=function(t){
if("-"===t.charAt(0)){
r.push(t);
var n=t.split("-"),i=n[1];
for(e[i]=++e[i]||1;
n.length>3;
){
n.pop();
var a=n.join("-");
o(a)&&-1===r.indexOf(a)&&r.push(a)}
}
}
,o=function(e){
return StyleFix.camelCase(e)in i}
;
if(n.length>0)for(var s=0;
s<n.length;
s++)a(n[s]);
else for(var l in n)a(StyleFix.deCamelCase(l));
var u={
uses:0}
;
for(var p in e){
var f=e[p];
u.uses<f&&(u={
prefix:p,uses:f}
)}
t.prefix="-"+u.prefix+"-",t.Prefix=StyleFix.camelCase(t.prefix),t.properties=[];
for(var s=0;
s<r.length;
s++){
var l=r[s];
if(0===l.indexOf(t.prefix)){
var c=l.slice(t.prefix.length);
o(c)||t.properties.push(c)}
}
"Ms"!=t.Prefix||"transform"in i||"MsTransform"in i||!("msTransform"in i)||t.properties.push("transform","transform-origin"),t.properties.sort()}
(),function(){
function e(e,r){
return i[r]="",i[r]=e,!!i[r]}
var r={
"linear-gradient":{
property:"backgroundImage",params:"red,teal"}
,calc:{
property:"width",params:"1px + 5%"}
,element:{
property:"backgroundImage",params:"#foo"}
,"cross-fade":{
property:"backgroundImage",params:"url(a.png),url(b.png),50%"}
}
;
r["repeating-linear-gradient"]=r["repeating-radial-gradient"]=r["radial-gradient"]=r["linear-gradient"];
var n={
initial:"color","zoom-in":"cursor","zoom-out":"cursor",box:"display",flexbox:"display","inline-flexbox":"display",flex:"display","inline-flex":"display",grid:"display","inline-grid":"display","min-content":"width"}
;
t.functions=[],t.keywords=[];
var i=document.createElement("div").style;
for(var a in r){
var o=r[a],s=o.property,l=a+"("+o.params+")";
!e(l,s)&&e(t.prefix+l,s)&&t.functions.push(a)}
for(var u in n){
var s=n[u];
!e(u,s)&&e(t.prefix+u,s)&&t.keywords.push(u)}
}
(),function(){
function r(e){
return a.textContent=e+"{
}
",!!a.sheet.cssRules.length}
var n={
":read-only":null,":read-write":null,":any-link":null,"::selection":null}
,i={
keyframes:"name",viewport:null,document:'regexp(".")'}
;
t.selectors=[],t.atrules=[];
var a=e.appendChild(document.createElement("style"));
for(var o in n){
var s=o+(n[o]?"("+n[o]+")":"");
!r(s)&&r(t.prefixSelector(s))&&t.selectors.push(o)}
for(var l in i){
var s=l+" "+(i[l]||"");
!r("@"+s)&&r("@"+t.prefix+s)&&t.atrules.push(l)}
e.removeChild(a)}
(),t.valueProperties=["transition","transition-property"],e.className+=" "+t.prefix,StyleFix.register(t.prefixCSS)}
}
(document.documentElement);
CSS代码(style.css):
*{box-sizing:border-box;}
html,body{height:100%;}
body{overflow:hidden;background:black;}
.wrap{height:100%;transform-style:preserve-3d;perspective:400px;width:1000px;margin:0 auto;}
.box{transform-style:preserve-3d;perspective:5000px;position:absolute;width:50px;height:200px;top:50%;margin-top:-100px;animation:spin 3s infinite alternate;}
.side,.top,.bottom{position:absolute;}
.top,.bottom{width:50px;height:50px;}
.top{transform:translateZ(100px);}
.bottom{transform:rotateY(180deg) translateZ(100px);}
.side:nth-child(3){transform:rotateX(90deg) translateZ(50px);width:50px;height:200px;}
.side:nth-child(4){width:200px;height:50px;transform:rotateY(90deg) translateZ(-100px);}
.side:nth-child(5){width:200px;height:50px;transform:rotateY(-90deg) translateZ(50px);}
.side:nth-child(6){width:50px;height:200px;transform:rotateX(-90deg) translateZ(-100px);}
.box:nth-child(1){left:0px;animation-delay:-0.075s;}
.box:nth-child(1) div{background:rgba(255,64,0,0.3);border:1px solid #ff4000;}
.box:nth-child(2){left:50px;animation-delay:-0.15s;}
.box:nth-child(2) div{background:rgba(255,128,0,0.3);border:1px solid #ff8000;}
.box:nth-child(3){left:100px;animation-delay:-0.225s;}
.box:nth-child(3) div{background:rgba(255,191,0,0.3);border:1px solid #ffbf00;}
.box:nth-child(4){left:150px;animation-delay:-0.3s;}
.box:nth-child(4) div{background:rgba(255,255,0,0.3);border:1px solid yellow;}
.box:nth-child(5){left:200px;animation-delay:-0.375s;}
.box:nth-child(5) div{background:rgba(191,255,0,0.3);border:1px solid #bfff00;}
.box:nth-child(6){left:250px;animation-delay:-0.45s;}
.box:nth-child(6) div{background:rgba(128,255,0,0.3);border:1px solid #80ff00;}
.box:nth-child(7){left:300px;animation-delay:-0.525s;}
.box:nth-child(7) div{background:rgba(64,255,0,0.3);border:1px solid #40ff00;}
.box:nth-child(8){left:350px;animation-delay:-0.6s;}
.box:nth-child(8) div{background:rgba(0,255,0,0.3);border:1px solid lime;}
.box:nth-child(9){left:400px;animation-delay:-0.675s;}
.box:nth-child(9) div{background:rgba(0,255,64,0.3);border:1px solid #00ff40;}
.box:nth-child(10){left:450px;animation-delay:-0.75s;}
.box:nth-child(10) div{background:rgba(0,255,128,0.3);border:1px solid #00ff80;}
.box:nth-child(11){left:500px;animation-delay:-0.825s;}
.box:nth-child(11) div{background:rgba(0,255,191,0.3);border:1px solid #00ffbf;}
.box:nth-child(12){left:550px;animation-delay:-0.9s;}
.box:nth-child(12) div{background:rgba(0,255,255,0.3);border:1px solid cyan;}
.box:nth-child(13){left:600px;animation-delay:-0.975s;}
.box:nth-child(13) div{background:rgba(0,191,255,0.3);border:1px solid deepskyblue;}
.box:nth-child(14){left:650px;animation-delay:-1.05s;}
.box:nth-child(14) div{background:rgba(0,127,255,0.3);border:1px solid #007fff;}
.box:nth-child(15){left:700px;animation-delay:-1.125s;}
.box:nth-child(15) div{background:rgba(0,64,255,0.3);border:1px solid #0040ff;}
.box:nth-child(16){left:750px;animation-delay:-1.2s;}
.box:nth-child(16) div{background:rgba(0,0,255,0.3);border:1px solid blue;}
.box:nth-child(17){left:800px;animation-delay:-1.275s;}
.box:nth-child(17) div{background:rgba(64,0,255,0.3);border:1px solid #4000ff;}
.box:nth-child(18){left:850px;animation-delay:-1.35s;}
.box:nth-child(18) div{background:rgba(127,0,255,0.3);border:1px solid #7f00ff;}
.box:nth-child(19){left:900px;animation-delay:-1.425s;}
.box:nth-child(19) div{background:rgba(191,0,255,0.3);border:1px solid #bf00ff;}
.box:nth-child(20){left:950px;animation-delay:-1.5s;}
.box:nth-child(20) div{background:rgba(255,0,255,0.3);border:1px solid magenta;}
@keyframes spin{0%{transform:translateZ(-300px) rotateX(90deg);}
25%{transform:translateZ(-300px) rotateX(90deg);}
75%{transform:rotateX(-90deg);}
100%{transform:rotateX(-90deg);}
}