Introduction
64 |65 | CascadeJS makes it simple to add cascading animations to individual letters or elements, using only vanilla javascript. 66 |
67 |Installation
70 |71 | Installation is easy, you have three options: 72 |
73 |74 |76 |npm install cascadejs
75 |bower install cascadejs
77 | or just download from GitHub. Then: 78 |
79 | 80 |<link href='stylesheet' type='text/css' href='cascade.min.css' />
81 | <script type='text/javascript' src='cascade.min.js' />
82 | Quick start
83 |84 | CascadeJS comes with default settings, so as long as you've included both the JS and CSS files, it will work right out of the box. Here's an example usage: 85 |
86 | 123 |Usage
126 |127 | CascadeJS works by reading a line of text, splitting that text into spans around each character, and then adding animations to each span (by default). The splitting is modeled after charming.js, a vanilla javascript version of Lettering.js. Cascade requires an element be passed in on initialization: 128 |
129 | 130 |var cascade = new Cascade(element);
131 |
132 | 133 | Cascade has two methods that can be called: 134 |
135 | 136 |var cascade = new Cascade(element);
137 | cascade.fragment();
138 | cascade.flow();
139 |
140 | 141 | Calling fragment() will split the text without adding the animations, and must be called on an element with only a text node for a child. Calling flow() will first fragment the text, and then animate all children elements in a cascade. 142 |
143 |Usage of flow()
146 | 147 |148 | Initialize Cascade with an element: 149 |
150 | 151 |var cascade = new Cascade(element);
152 |
153 | 154 | Then call flow() with any or none of the following options: 155 |
156 | 157 |cascade.flow({
158 | tagName: 'span',
159 | classPrefix: 'cascade char',
160 | effect: 'fadeUp',
161 | totalTime: 0.5,
162 | duration: 1,
163 | shouldFragment: true,
164 | shouldAppendTargetClass: true,
165 | targetClass: 'cascade-container'
166 | });
167 |
168 | 169 | tagName: - The tag that will wrap each character, ie <span>A</span>. Defaults to 'span'. 170 |
171 | 172 |173 | classPrefix: - The class(es) to be added to the tags, with an increasing number appended at the end, ie <span class="cascade char1">A</span>. Defaults to 'cascade char'. 174 |
175 | 176 |177 | effect: - The class name that adds the animation. In this default, 'fadeUp' uses a keyframe animation to fade in text while utilizing translateY to slide it up. Defaults to 'fadeUp'. 178 |
179 | 180 |181 | totalTime: - The amount of time in seconds as a float/integer from the first letter beginning it's animation to the last letter beginning it's animation. Defaults to 0.5. 182 |
183 | 184 |185 | duration: - How long each letter's animation lasts. If you'd rather specify this in your CSS, just leave this option empty. Defaults to null and doesn't add this styling. 186 |
187 | 188 |189 | shouldFragment: - If you've previously called fragment() on this node, set this option to false otherwise the fragmentation will run again and throw an error. Defaults to true. 190 |
191 | 192 |193 | shouldAppendTargetClass: - Defaults to true, will append a class to the target element after fragmenting. 194 |
195 | 196 |197 | targetClass: - The class to be appended to the target element. Defaults to 'cascade-container'. Note: if you've already added the class to the element that you'd like appended, CascadeJS will skip the appending. Example: 198 |
199 | 200 |<!-- This element will have a class appended to it -->
201 | <h1 class='text-center'>Hello!</h1>
202 |
203 | <script>
204 | var element = document.getElementsByTagName('h1')[0];
205 | var cascade = new Cascade(element);
206 | cascade.flow({
207 | targetClass: 'cascade-container'
208 | });
209 | </script>
210 |
211 | <!-- This element will NOT have a class appended -->
212 | <h1 class='text-center cascade-container'>Hello!</h1>
213 |
214 | <script>
215 | var element = document.getElementsByTagName('h1')[0];
216 | var cascade = new Cascade(element);
217 | cascade.flow({
218 | targetClass: 'cascade-container'
219 | });
220 | </script>
221 | Usage of fragment()
225 | 226 |227 | fragment() will split and wrap your text, but not animate it. Just want to style each letter in an interesting way? Want a vanilla javascript replacement for lettering.js? This is it. fragment() accepts all the same options as flow() , but only the following will be applied, while the rest will be stored in the object for when flow() is called: 228 |
229 | 230 |var cascade = new Cascade(element).fragment({
231 | tagName: 'span',
232 | classPrefix: 'cascade char'
233 | });
234 |
235 | 236 | You can then call: 237 |
238 | 239 |cascade.flow({
240 | shouldFragment: false
241 | });
242 |
243 | 244 | when you're ready for some cool animations. 245 |
246 |Examples
250 |<h1>Hello!</h1>
251 |
252 | <script>
253 | var element = document.getElementsByClassName('cascade-container')[0];
254 | cascade = new Cascade(element).flow({
255 | totalTime: 2,
256 | duration: 1.5
257 | })
258 | </script>
259 |
260 | 261 | Produces: 262 |
263 | 264 |<h1 class='cascade-container'>
265 | <span class='cascade char1 fadeUp' style='animation-delay: 0s; animation-duration: 1.5s;'>H</span>
266 | <span class='cascade char2 fadeUp' style='animation-delay: 0.4s; animation-duration: 1.5s;'>e</span>
267 | <span class='cascade char3 fadeUp' style='animation-delay: 0.8s; animation-duration: 1.5s;'>l</span>
268 | <span class='cascade char4 fadeUp' style='animation-delay: 1.2s; animation-duration: 1.5s;'>l</span>
269 | <span class='cascade char5 fadeUp' style='animation-delay: 1.6s; animation-duration: 1.5s;'>o</span>
270 | <span class='cascade char6 fadeUp' style='animation-delay: 2s; animation-duration: 1.5s;'>!</span>
271 | </h1>
272 |
273 | 274 | Use Animate.css? Try this: 275 |
276 | 277 |var element = document.getElementsByClassName('cascade-container')[0];
278 | var cascade = new Cascade(element).flow({
279 | classPrefix: 'animated char',
280 | effect: 'bounceInLeft'
281 | });
282 |