Skip to content

Instantly share code, notes, and snippets.

@SFantasy
Last active August 29, 2015 14:10
Show Gist options
  • Save SFantasy/6745c8e00f7075bc959f to your computer and use it in GitHub Desktop.
Save SFantasy/6745c8e00f7075bc959f to your computer and use it in GitHub Desktop.
CSS3 Random Cube
.cube {
perspective: 600px;
perspective-origin: 50% 50%;
width: 100px;
height: 100px;
margin: 100px auto;
position: relative;
}
.origin {
height: 100px;
width: 100px;
background-color: rgba(0, 0, 0, 0);
position: absolute;
transform-style: preserve-3d;
}
.active {
-webkit-animation: around 1s infinite ease-in-out;
}
@-webkit-keyframes around {
0% {
transform: rotateX(0) rotateY(0);
}
50% {
transform: rotateX(360deg) rotateY(360deg);
}
100% {
transform: rotateX(-360deg) rotateY(-360deg);
}
}
.wall {
width: 100px;
height: 100px;
position: absolute;
box-sizing: border-box;
border: 1px solid rgba(0, 0, 0, .5);
font-size: 30px;
color: white;
text-align: center;
line-height: 100px;
}
.front {
transform: translateZ(50px);
background-color: rgba(255, 0, 0, .7);
}
.back {
transform: translateZ(-50px);
background-color: rgba(0, 255, 255, .7);
}
.left {
transform: rotateY(-90deg) translateZ(50px);
background-color: rgba(0, 255, 0, .7);
}
.right {
transform: rotateY(90deg) translateZ(50px);
background-color: rgba(0, 0, 255, .7);
}
.top {
transform: rotateX(90deg) translateZ(50px);
background-color: rgba(255, 255, 0, .7);
}
.bottom {
transform: rotateX(-90deg) translateZ(50px);
background-color: rgba(255, 0, 255, .7);
}
.one {
transform: rotateX(0) rotateY(0);
}
.two {
transform: rotateX(0) rotateY(90deg);
}
.three {
transform: rotateX(180deg) rotateY(0deg);
}
.four {
transform: rotateX(0) rotateY(-90deg);
}
.five {
transform: rotateX(-90deg) rotateY(0);
}
.six {
transform: rotateX(90deg) rotateY(0);
}
<!DOCTYPE html>
<html>
<head>
<script src="//code.jquery.com/jquery-2.1.1.min.js"></script>
<meta charset="utf-8">
<title>JS Bin</title>
</head>
<body>
<div class="cube">
<div class="origin">
<div class="wall front">1</div>
<div class="wall left">2</div>
<div class="wall back">3</div>
<div class="wall right">4</div>
<div class="wall top">5</div>
<div class="wall bottom">6</div>
</div>
</div>
<button id="go">摇摇摇</button>
<button id="stop">停停停</button>
</body>
</html>
$(document).ready(function () {
$('#go').on('click', function () {
$('.origin').addClass('active');
});
$('#stop').on('click', function () {
var num = Math.floor(Math.random()*6 + 1);
var num_class = '';
switch (num) {
case 1: num_class = 'one'; break;
case 2: num_class = 'two'; break;
case 3: num_class = 'three'; break;
case 4: num_class = 'four'; break;
case 5: num_class = 'five'; break;
case 6: num_class = 'six'; break;
default: num_class = 'one';
}
$('.origin').removeClass().addClass('origin ' +num_class);
});
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment