First attempt to build and animate 3D shapes - hover to spin the tombola
A Pen by Nick Moreton on CodePen.
First attempt to build and animate 3D shapes - hover to spin the tombola
A Pen by Nick Moreton on CodePen.
| <h1>CSS Tombola</h1> | |
| <p>First attempt to build and animate 3D shapes - hover to spin the tombola</p> | |
| <div class="wrapper"> | |
| <div class="tombola"> | |
| <div class="panel p1">One</div> | |
| <div class="panel p2">Two</div> | |
| <div class="panel p3">Three</div> | |
| <div class="panel p4">Four</div> | |
| <div class="panel p5">Five</div> | |
| <div class="panel p6">Six</div> | |
| <div class="panel p7">Seven</div> | |
| <div class="panel p8">Eight</div> | |
| </div> | |
| </div> | |
| <p>The main sticking point was realising that translateZ() happens <em>after</em> the rotation of the panels. Also getting the tombola to rotate around the 'center' point using the 'Z' attribute of the transform-origin property</p> | |
| <p><b>To-do:</b> Some sort of 'lighting' effect.</p> |
| // Script to randomise the panel the tombola lands on | |
| $('.tombola').mouseenter(function(){ | |
| var rotation = [1440,1485,1530,1575,1620,1665,1710,1755]; | |
| var pick = Math.floor(Math.random()*8); | |
| var spin = rotation[pick]; | |
| $('.tombola').css({'transform':'rotateX('+spin+'deg) translateZ(-480px)'}); | |
| }); | |
| $('.tombola').mouseleave(function(){ | |
| $('.tombola').css({'transform':'rotateX(0deg) translateZ(-480px)'}); | |
| }); |
| @base: #94DBAF; | |
| * { | |
| box-sizing:border-box; | |
| } | |
| body { | |
| text-align:center; | |
| width:800px; | |
| margin:auto; | |
| } | |
| .wrapper { | |
| perspective: 1000px; | |
| perspective-origin: 50% 50%; | |
| } | |
| .tombola { | |
| position:relative; | |
| width: 600px; | |
| height:200px; | |
| margin: 120px auto; | |
| transform-style: preserve-3d; | |
| transform-origin:center center -480px; | |
| transform: rotateX(0deg) translateZ(-480px); | |
| transition:4s ease all; | |
| } | |
| .panel { | |
| top:0px; | |
| padding:60px; | |
| font-size: 40px; | |
| font-weight:bold; | |
| text-transform:uppercase; | |
| position: absolute; | |
| width: 600px; | |
| height: 200px; | |
| color: white; | |
| text-align: center; | |
| line-height: 2em; | |
| background:@base; | |
| border: 1px solid darken(@base,40); | |
| } | |
| .p1 { | |
| transform: translateZ(240px); | |
| background:@base; | |
| border: 1px solid darken(@base,40); | |
| } | |
| .p2 { | |
| transform: rotateX(-45deg) translateZ(240px); | |
| background:spin(@base,45); | |
| border: 1px solid darken(spin(@base,45),40); | |
| } | |
| .p3 { | |
| transform: rotateX(-90deg) translateZ(240px); | |
| background:spin(@base,90); | |
| border: 1px solid darken(spin(@base,90),40); | |
| } | |
| .p4 { | |
| transform: rotateX(-135deg) translateZ(240px); | |
| background:spin(@base,135); | |
| border: 1px solid darken(spin(@base,135),40); | |
| } | |
| .p5 { | |
| transform: rotateX(-180deg) translateZ(240px); | |
| background:spin(@base,180); | |
| border: 1px solid darken(spin(@base,180),40); | |
| } | |
| .p6 { | |
| transform: rotateX(-225deg) translateZ(240px) ; | |
| background:spin(@base,225); | |
| border: 1px solid darken(spin(@base,225),40); | |
| } | |
| .p7 { | |
| transform: rotateX( -270deg) translateZ(240px); | |
| background:spin(@base,270); | |
| border: 1px solid darken(spin(@base,270),40); | |
| } | |
| .p8 { | |
| transform: rotateX(45deg) translateZ(240px); | |
| background:spin(@base,315); | |
| border: 1px solid darken(spin(@base,315),40); | |
| } |