Slot Machine Animation Css
A slow network isn’t the worst thing in the world - it put me in front of the Twitter spinning loader animation for long enough for me to think: “I could create something like this”. And that’s what this post is about, creating a simple CSS spinning loader animation similar to Twitter’s.
Although Twitter’s is made from an SVG, it can also be created out of pure CSS using a simple CSS keyframe animation, with nothing but a single HTML element. As you’ll see, we’ll make heavy use of CSS variables to make our resulting CSS more flexible and extensible.
Let’s just get to it.
Note that the example in this post is not meant to be 100% identical to Twitter's loading animation.
The Idea
- This is a modern proof of concept casino slot machine game, built using only vanilla HTML, CSS and JavaScript. No Flash or Frameworks required. Allowing for an amazing low bundle size and blazing fast performance. Built using the Web Animations API.
- A client requested a “slot machine” hover effect for their social network buttons. When you hover on the icon, it moves up, sort of like in a slot machine, and changes color. Here’s an example of what I’m talking about: It turns out that this effect can be accomplished with some clever CSS!
My inital thought was to:
I started developing another slot machine game using Phaser JS soon after I found out the performance issue with html5 / css3 / js. The game on html5 canvas performance has been far better than on html5 / css3. I have concluded for now that I would use html5 / css3 for games without necessities for intensive animations. Run a slot machine animation in an input field. Slot items are chosen randomly from a large list stored outside the DOM. Slot animation based on Matthew Lein's jQuery jSlots with a few modifications (shown in revisions). Built for MarketWatch's cost-of-living calculator. To use CSS3 Animation you must also define the actual animation keyframes (which you named spin) Read https.
- Create a circle
Rotate
the circle indefinitely from0
to360deg
, and- Find a way to indicate that the circle is being rotated
In CSS pseudocode (if you will), this roughly means:
- Draw a square with a border radius of at least 50%
- Animate a circle continually from
0
to360 degrees
- Indicate that the circle is indeed rotating
The third point seemed fuzzy. But let's keep going.
First, some initial setup
What’s going on with the initial setup?
On the :root
pseudo-class, we defined some CSS variables
:
--s-loader-wrapper-bg-color
: the background color for the loader’s parent container--s-loader-width
: the loader width--s-loader-height
: the loader height (notice how we can assign a variable to another variable)--s-loader-border-color-trans
: an opaque version of the loader border color--s-loader-border-color-full
: a non-opaque version of the loader border color (this will do the trick of indicating the loader rotation)
On the body
element, we:
- Removed the browser default margin on the
body
element - Applied a background color (
rgb(21, 22, 43)
is a fallback for--s-loader-wrapper-bg-color
) - We set the width and height
- The
display
,align-items
andjustify-content
centers the children of thebody
(the spinning loader - in this case)
You can read on viewport units and centering with flex or grid
The Spinning Loader Element
What’s going on here with the spinning loader?
We set an equal width
and height
on the spinning loader so we can get a perfect square.
Then we:
- Create a
border
for the 4 sides with sufficient opacity. - Reduce the
opacity
on all border sides except for one of them (the left one in this case). That’ll serve as a spinning-indicator. The different color on only one of the border sides is the secret sauce here. - Create a perfect circle by applying a
border-radius
of 50% to the square. - Make the
background
of the div transparent. animation-name
- a reference to the animation name (we’ll define that next).animation-iteration-count
: how many cycles should the animation go?animation-duration
: how long should each cycle take?animation-timing-function
: this defines the speed of an animation over time. Here a timing function oflinear
works for our needs because the speed will be constant.
Next we define the actual animation using the @keyframes at-rule - applying a rotation transformation from 0 to 360 degrees:
Slot Machine Animation Css Creator
We rotate
the loader infinitelyfrom0deg
to360deg
. You can omit the deg in 0deg. Pretty much any unit in CSS can be omitted if its value is 0. Also, using the from and to keywords is another way of writing 0%
and 100%
.
And there goes our loader animation:
Slot Machine Text Animation Css
I’ve tweaked the version that’s displayed on this page a little bit, and distilled it down to the essential parts:
The Full Code at a Glance
First, the HTML markup. As promised, only one div
😉:
And next comes the CSS styling:
👍 And, there you have it! There are many ways of creating different kinds of loaders. This is just one of them. Once you start to break down the different parts that make up a loader, you can create them fairly easily. Of course, the only limit is your imagination 🌈. So there, I hope your spinner keeps on rotating!
hot css picks
Intro to CSS Grid
Variable Fonts
Cropping Images with object-fit
position: sticky
Gradient Borders
Centering Using Flexbox
latest css posts
CSS System Font Stack Reference
Styling Scrollbars with CSS: The Modern Way to Style Scrollbars
Using the CSS line-height Property to Improve Readability
How to Use the CSS :root Pseudo-Class Selector
all css posts
Slot Machine Animation Css
/** |
* Quick and easy CSS3 rolling-number/slot machine? |
*/ |
body { |
font-size: 700%; /* with this setup you get 1:1 em , so 1em is actually number 1 */ |
} |
#counter { |
height: 1em; |
overflow: hidden; |
} |
.digits { |
float:left; |
list-style-type: none; |
font-size: 1em; |
line-height: 1em; |
} |
.digits-first { |
margin-top: -4em; /* number 4! */ |
} |
.digits-second { |
margin-top: 0em; /* number 0! */ |
} |
.digits-third { |
margin-top: -4em; /* number 4! */ |
} |
.digits { |
animation-duration: 2s; |
animation-timing-function: ease; |
animation-delay: 2.2s; |
animation-fill-mode: forwards; |
} |
.luckie { |
animation-name: luckie; |
} |
/* Animations */ |
@keyframes luckie { |
100% { |
margin-top: -7em; |
} |
} |
<!-- content to be placed inside <body>…</body> --> |
<divid='counter' class='animated'> |
<ulclass='digits digits-first luckie'><li> 0 <li> 1 <li> 2 <li> 3 <li> 4 <li> 5 <li> 6 <li> 7 <li> 8 <li> 9 </ul> |
<ulclass='digits digits-second luckie'><li> 0 <li> 1 <li> 2 <li> 3 <li> 4 <li> 5 <li> 6 <li> 7 <li> 8 <li> 9 </ul> |
<ulclass='digits digits-third luckie'><li> 0 <li> 1 <li> 2 <li> 3 <li> 4 <li> 5 <li> 6 <li> 7 <li> 8 <li> 9 </ul> |
</div> |
{'view':'split','fontsize':'100','seethrough':'','prefixfree':'1','page':'all'} |