public
Created

Cube rotates when a trigger has been activated. Using CSS transforms, CSS 3D and CSS animation.

  • Download Gist
index.html
HTML
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
<h1 class="title">Rotating <span>Cube</span></h1>
<section>
<div class="cube-container">
<div class="cube">
<figure class="front"></figure>
<figure class="back"></figure>
<figure class="right"></figure>
<figure class="left"></figure>
<figure class="top"></figure>
<figure class="bottom"></figure>
</div>
</div>
 
<div href="" class="cube-switch">
<span class="switch">
<span class="switch-state off">Off</span>
<span class="switch-state on">On</span>
</span>
</div>
</section>
script.js
JavaScript
1 2 3 4 5 6 7 8 9 10 11
if(Modernizr.csstransforms3d) {
$('.cube-switch .switch').click(function(){
if($('.cube').hasClass('spin') && $('.cube-switch').hasClass('active')){
$('.cube-switch').removeClass('active');
$('.cube').removeClass('spin');
} else {
$('.cube-switch').addClass('active');
$('.cube').addClass('spin');
}
});
}
style.scss
SCSS
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274
@import "compass";
// = GLobals
//-------------------------------------------------------------------------------//
 
body {
background: red;
}
 
.title {
position: relative;
top: 3em;
 
color: white;
 
text-align: center;
text-transform: uppercase;
 
span {
display: block;
 
font-size: 1.85em;
line-height: .75em;
text-outline: black;
}
}
 
 
// = Keyframes
//-------------------------------------------------------------------------------//
 
@-webkit-keyframes spin {
from { @include transform(rotateX(0) rotateY(0)) }
to { @include transform(rotateX(360deg) rotateY(360deg)); }
}
 
@-moz-keyframes spin {
from { @include transform(rotateX(0) rotateY(0)); }
to { @include transform(rotateX(360deg) rotateY(360deg)); }
}
 
@-ms-keyframes spin {
from { @include transform(rotateX(0) rotateY(0)); }
to { @include transform(rotateX(360deg) rotateY(360deg)); }
}
 
@-o-keyframes spin {
from { @include transform(rotateX(0) rotateY(0)); }
to { @include transform(rotateX(360deg) rotateY(360deg)); }
}
 
@keyframes spin {
from { @include transform(rotateX(0) rotateY(0)); }
to { @include transform(rotateX(360deg) rotateY(360deg)); }
}
 
.spin {
-webkit-animation: spin 5s infinite linear;
-moz-animation: spin 5s infinite linear;
-ms-animation: spin 5s infinite linear;
-o-animation: spin 5s infinite linear;
animation: spin 5s infinite linear;
}
 
 
// = Reflection
//-------------------------------------------------------------------------------//
 
.reflections {
-webkit-box-reflect: below 0px -webkit-gradient(linear, left top, left bottom, from(transparent), color-stop(50%, transparent), to(rgba(255,255,255,0.6)));
}
 
 
// = Cube Module
//-------------------------------------------------------------------------------//
// IE9 and below doens't support
// cssransforms3d but does support csstransforms
 
.csstransforms3d {
 
// =======BEGIN CUBE CONTAINER ======= //
 
.cube-container {
height: 200px;
 
margin: 0 auto;
position: relative;
top: 10em;
width: 200px;
// distance between the scene and the viewer
-moz-perspective:600px;
@include perspective(600px);
// vanishing point of perspective default is in middle of the scene
-moz-perspective-origin: 50% 50%;
@include perspective-origin(50% 50%);
}
 
// =======BEGIN CUBE ======= //
 
.cube {
height: 100%;
position: absolute;
@include transition($transition-1: .5s all ease-in-out);
 
// preserve 3fd effect of child elements
// default value is 'flat'
// preserve effects of child elements
-moz-transform-style: preserve-3d;
@include transform-style(preserve-3d);
 
/* panels global styles*/
@include transform(rotateX(-20deg) rotateY(28deg));
 
// tells browser not to render the back of the element
// Accepts visible | hidden
backface-visibility: visible;
 
width: 100%;
&:hover {
cursor: pointer;
// panels global hover styles
@include transform(rotateX(-50deg) rotateY(0deg));
}
 
figure {
@include box-sizing(border-box);
display: block;
height: 200px;
position: absolute;
width: 200px;
 
font-size: 2em;
line-height: 6em;
text-align: center;
}
}
 
// =======BEGIN CUBE PANELS======= //
 
.cube .front {
background: rgba(230,0,0,.5);
@extend .reflections;
@include transform(translateZ(100px));
}
.cube .back {
background: rgba(200,0,0,.6);
@extend .reflections;
@include transform(rotateY(180deg) rotateX(0deg) translateZ(100px));
}
.cube .left {
background: rgba(200,0,0,.5);
@extend .reflections;
@include transform(rotateY(-90deg) translateZ(100px));
}
.cube .right {
background: rgba(200,0,0,.4);
@extend .reflections;
@include transform(rotateY(90deg) translateZ(100px));
}
.cube .top {
background: rgba(200,0,0,.3);
@include transform(rotateX(90deg) translateZ(100px));
}
.cube .bottom {
background: rgba(50,50,50,.5);
@include transform(rotateX(-90deg) rotateZ(-180deg) translateZ(100px));
opacity: .55;
}
}
 
 
// = Cube Switch
//-------------------------------------------------------------------------------//
 
.cube-switch {
border-radius:10px;
border:1px solid rgba(0,0,0,0.4);
box-shadow: 0 0 8px rgba(0,0,0,0.6), inset 0 100px 50px rgba(255,255,255,0.1);
/* Prevents clics on the back */
cursor:default;
display: block;
height: 100px;
position: relative;
left: -260px;
margin: 0 auto;
overflow:hidden;
/* Prevents clics on the back */
pointer-events:none;
width: 100px;
white-space: nowrap;
 
background:#333;
}
 
/* The switch */
.cube-switch .switch {
border:1px solid rgba(0,0,0,0.6);
border-radius:0.7em;
box-shadow:
inset 0 1px 0 rgba(255,255,255,0.3),
inset 0 -7px 0 rgba(0,0,0,0.2),
inset 0 50px 10px rgba(0,0,0,0.2),
0 1px 0 rgba(255,255,255,0.2);
display:block;
width: 60px;
height: 60px;
margin-left:-30px;
margin-top:-30px;
position:absolute;
top: 50%;
left: 50%;
width: 60px;
background:#666;
transition: all 0.2s ease-out;
 
/* Allows click */
cursor:pointer;
pointer-events:auto;
}
 
/* SWITCH Active State */
.cube-switch.active {
background:#222;
box-shadow:
0 0 5px rgba(0,0,0,0.5),
inset 0 50px 50px rgba(55,55,55,0.1);
}
 
.cube-switch.active .switch {
background:#333;
box-shadow:
inset 0 6px 0 rgba(255,255,255,0.1),
inset 0 7px 0 rgba(0,0,0,0.2),
inset 0 -50px 10px rgba(0,0,0,0.1),
0 1px 0 rgba(205,205,205,0.1);
}
 
.cube-switch.active:after,
.cube-switch.active:before {
background:#333;
box-shadow:
0 1px 0 rgba(255,255,255,0.1),
inset 1px 2px 1px rgba(0,0,0,0.5),
inset 3px 6px 2px rgba(200,200,200,0.1),
inset -1px -2px 1px rgba(0,0,0,0.3);
}
 
.cube-switch.active .switch:after,
.cube-switch.active .switch:before {
background:#222;
border:none;
margin-top:0;
height:1px;
}
 
.cube-switch .switch-state {
display: block;
position: absolute;
left: 40%;
color: #FFF;
 
font-size: .5em;
text-align: center;
}
 
/* SWITCH On State */
.cube-switch .switch-state.on {
bottom: 15%;
}
 
/* SWITCH Off State */
.cube-switch .switch-state.off {
top: 15%;
}

Please sign in to comment on this gist.

Something went wrong with that request. Please try again.