CSS3 Scale Translate3d

Overview

This is an update to my original JavaScript tweening boxes experiment from several years ago. This time, instead of using JavaScript to change values, the example below uses the CSS3 animation properties Scale and Translate 3d. (Because of this it currently only works with Firefox, Google Chrome, Safari and iOS. It may also work on Android but I can’t test this…)

The example uses percentages for all block sizes and transformations, meaning everything scales to the size of the container element. This can be seen in the full-screen example. The only difference in the code between that version and the inline version above is that a width and height has been added to the containing HTML element.

It’s a simple proof-of-concept example, and as such all positioning and animations are hardcoded. It would be possible to control these dynamically with JavaScript.

The HTML is quite straightforward:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
<div id="container">
    <div id="block-home" class="block">
        <h2>Home</h2>
        <p>...</p>
        <ul>
            <li><a href="#about">About</a></li>
            ...
        </ul>
    </div>
    <div id="block-about" class="block">
        <h2>About</h2>
        <p>...</p>
        <ul>
            <li><a href="#home">Home</a></li>
        </ul>
    </div>
    ...
</div>

The only JavaScript used is to detect clicks on the navigation links, and change the class on the containing element:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
var $links = $('a'),
current = null;

$links.bind('click', function(e){
    e.preventDefault();
    this.blur();

    var href = this.href.split('#')[1];

    // condition : treat home links differently
    if (href == "home") {
        $('#container').attr('class', '').addClass(current + '-to-home');
        current = null;
    } else {
        current = href;
        $('#container').attr('class', '').addClass('home-to-' + href);
    }
});

The CSS contains six animations, one for the animation from the central block to each sub-block, and a return animation. These are activated when the class on the container element is changed. Using scale and transform means the transitions are hardware-accelerated on Mac Safari and iOS:

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
/* container default styles */
#container {
    position:absolute;
    left:0;
    top:0;
    overflow:visible;
    -webkit-animation-duration: 3s;
    -webkit-animation-timing-function: ease-in-out;
    -webkit-animation-iteration-count: 1;

    -moz-animation-duration: 3s;
    -moz-animation-timing-function: ease-in-out;
    -moz-animation-iteration-count: 1;

    animation-duration: 3s;
    animation-timing-function: ease-in-out;
    animation-iteration-count: 1;
}

/* block positions */
    #block-home   { left:10%; top:10%; }
    #block-about  { left:-75%; top:-75%; }
    #block-lorem  { left:-75%; top:95%; }
    #block-ipsum  { left:95%; top:95%; }

/* block styles */
    .block {
        position:absolute;
        padding:0;
        width:80%;
        height:80%;
        overflow:hidden;
    }

/* a bit of extra styling to make it look a bit nicer */
    .block {
        background-color: #ff9900;
        background-image: -webkit-gradient(linear, left top, left bottom, from(#ff9900), to(#ff3300)); /* Saf4+, Chrome */
        background-image: -webkit-linear-gradient(top, #ff9900, #ff3300); /* Chrome 10+, Saf5.1+, iOS 5+ */
        background-image:    -moz-linear-gradient(top, #ff9900, #ff3300); /* FF3.6 */
        background-image:     -ms-linear-gradient(top, #ff9900, #ff3300); /* IE10 */
        background-image:      -o-linear-gradient(top, #ff9900, #ff3300); /* Opera 11.10+ */
        background-image:         linear-gradient(top, #ff9900, #ff3300);
        filter: progid:DXImageTransform.Microsoft.gradient(startColorStr='#ff9900', EndColorStr='#ff3300'); /* IE6–IE9 */
        text-shadow: 2px 2px 2px #f90;
        border-radius:25px;
    }

    .block h2,
    .block p,
    .block ul {
        margin:30px;
    }

    .block ul {
        padding-left:30px;
    }

    .block a {
        color:#fff;
        text-decoration:none;
    }


/* animations */
    #container.about-to-home {
        -webkit-animation-name: about-to-home; -webkit-transform: translate3d(0,0,0);
        -moz-animation-name: about-to-home; -moz-transform: translate(0,0);
        animation-name: about-to-home; transform: translate3d(0,0,0);
    }
    #container.lorem-to-home {
        -webkit-animation-name: lorem-to-home; -webkit-transform: translate3d(0,0,0);
        -moz-animation-name: lorem-to-home; -moz-transform: translate(0,0);
        animation-name: lorem-to-home; transform: translate3d(0,0,0);
    }
    #container.ipsum-to-home {
        -webkit-animation-name: ipsum-to-home; -webkit-transform: translate3d(0,0,0);
        -moz-animation-name: ipsum-to-home; -moz-transform: translate(0,0);
        animation-name: ipsum-to-home; transform: translate3d(0,0,0);
    }
    #container.home-to-about {
        -webkit-animation-name: home-to-about; -webkit-transform: translate3d(85%, 85%, 0);
        -moz-animation-name: home-to-about; -moz-transform: translate(85%, 85%);
        animation-name: home-to-about; transform: translate3d(85%, 85%, 0);
    }
    #container.home-to-lorem {
        -webkit-animation-name: home-to-lorem; -webkit-transform: translate3d(85%, -85%, 0);
        -moz-animation-name: home-to-lorem; -moz-transform: translate(85%, -85%);
        animation-name: home-to-lorem; transform: translate3d(85%, -85%, 0);
    }
    #container.home-to-ipsum {
        -webkit-animation-name: home-to-ipsum; -webkit-transform: translate3d(-85%, -85%, 0);
        -moz-animation-name: home-to-ipsum; -moz-transform: translate(-85%, -85%);
        animation-name: home-to-ipsum; transform: translate3d(-85%, -85%, 0);
    }


/* animation keyframes */
  /* webkit */
      @-webkit-keyframes about-to-home {
          0%   { -webkit-transform: scale(1) translate3d(85%, 85%, 0); }
          45%  { -webkit-transform: scale(0.25); }
          55%  { -webkit-transform: scale(0.25) translate3d(0, 0, 0); }
          100% { -webkit-transform: scale(1); }
      }

      @-webkit-keyframes lorem-to-home {
          0%   { -webkit-transform: scale(1) translate3d(85%, -85%, 0); }
          45%  { -webkit-transform: scale(0.25); }
          55%  { -webkit-transform: scale(0.25) translate3d(0, 0, 0); }
          100% { -webkit-transform: scale(1); }
      }

      @-webkit-keyframes ipsum-to-home {
          0%   { -webkit-transform: scale(1) translate3d(-85%, -85%, 0); }
          45%  { -webkit-transform: scale(0.25); }
          55%  { -webkit-transform: scale(0.25) translate3d(0, 0, 0); }
          100% { -webkit-transform: scale(1); }
      }

      @-webkit-keyframes home-to-about {
          0%   { -webkit-transform: scale(1) translate3d(0,0,0); }
          45%  { -webkit-transform: scale(0.25); }
          55%  { -webkit-transform: scale(0.25) translate3d(0,0,0); }
          100% { -webkit-transform: scale(1) translate3d(85%, 85%, 0); }
      }

      @-webkit-keyframes home-to-lorem {
          0%   { -webkit-transform: scale(1) translate3d(0, 0, 0); }
          45%  { -webkit-transform: scale(0.25); }
          55%  { -webkit-transform: scale(0.25) translate3d(0, 0, 0); }
          100% { -webkit-transform: scale(1) translate3d(85%, -85%, 0); }
      }

      @-webkit-keyframes home-to-ipsum {
          0%   { -webkit-transform: scale(1) translate3d(0, 0, 0); }
          45%  { -webkit-transform: scale(0.25); }
          55%  { -webkit-transform: scale(0.25) translate3d(0, 0, 0); }
          100% { -webkit-transform: scale(1) translate3d(-85%, -85%, 0); }
      }

  /* moz */
      @-moz-keyframes about-to-home {
          0%   { -moz-transform: scale(1) translate(85%, 85%); }
          45%  { -moz-transform: scale(0.25); }
          55%  { -moz-transform: scale(0.25) translate(0, 0); }
          100% { -moz-transform: scale(1); }
      }

      @-moz-keyframes lorem-to-home {
          0%   { -moz-transform: scale(1) translate(85%, -85%); }
          45%  { -moz-transform: scale(0.25); }
          55%  { -moz-transform: scale(0.25) translate(0, 0); }
          100% { -moz-transform: scale(1); }
      }

      @-moz-keyframes ipsum-to-home {
          0%   { -moz-transform: scale(1) translate(-85%, -85%); }
          45%  { -moz-transform: scale(0.25); }
          55%  { -moz-transform: scale(0.25) translate3d(0, 0); }
          100% { -moz-transform: scale(1); }
      }

      @-moz-keyframes home-to-about {
          0%   { -moz-transform: scale(1) translate(0,0); }
          45%  { -moz-transform: scale(0.25); }
          55%  { -moz-transform: scale(0.25) translate(0,0); }
          100% { -moz-transform: scale(1) translate(85%, 85%); }
      }

      @-moz-keyframes home-to-lorem {
          0%   { -moz-transform: scale(1) translate(0, 0); }
          45%  { -moz-transform: scale(0.25); }
          55%  { -moz-transform: scale(0.25) translate(0, 0); }
          100% { -moz-transform: scale(1) translate(85%, -85%); }
      }

      @-moz-keyframes home-to-ipsum {
          0%   { -moz-transform: scale(1) translate(0, 0); }
          45%  { -moz-transform: scale(0.25); }
          55%  { -moz-transform: scale(0.25) translate(0, 0); }
          100% { -moz-transform: scale(1) translate(-85%, -85%); }
      }

  /* css proper */
      @keyframes about-to-home {
          0%   { transform: scale(1) translate3d(85%, 85%, 0); }
          45%  { transform: scale(0.25); }
          55%  { transform: scale(0.25) translate3d(0, 0, 0); }
          100% { transform: scale(1); }
      }

      @keyframes lorem-to-home {
          0%   { transform: scale(1) translate3d(85%, -85%, 0); }
          45%  { transform: scale(0.25); }
          55%  { transform: scale(0.25) translate3d(0, 0, 0); }
          100% { transform: scale(1); }
      }

      @keyframes ipsum-to-home {
          0%   { transform: scale(1) translate3d(-85%, -85%, 0); }
          45%  { transform: scale(0.25); }
          55%  { transform: scale(0.25) translate3d(0, 0, 0); }
          100% { transform: scale(1); }
      }

      @keyframes home-to-about {
          0%   { transform: scale(1) translate3d(0,0,0); }
          45%  { transform: scale(0.25); }
          55%  { transform: scale(0.25) translate3d(0,0,0); }
          100% { transform: scale(1) translate3d(85%, 85%, 0); }
      }

      @keyframes home-to-lorem {
          0%   { transform: scale(1) translate3d(0, 0, 0); }
          45%  { transform: scale(0.25); }
          55%  { transform: scale(0.25) translate3d(0, 0, 0); }
          100% { transform: scale(1) translate3d(85%, -85%, 0); }
      }

      @keyframes home-to-ipsum {
          0%   { transform: scale(1) translate3d(0, 0, 0); }
          45%  { transform: scale(0.25); }
          55%  { transform: scale(0.25) translate3d(0, 0, 0); }
          100% { transform: scale(1) translate3d(-85%, -85%, 0); }
      }

Yet another pointless example of something that has been achievable with Flash for many years, that’s finally also possible with HTML/CSS (on a small percentage of browsers).