Delighters.JS
13 |Add CSS animations to delight users as they scroll down.
14 |
15 | A tiny javascript library by Q42
16 | https://github.com/Q42/delighters
17 |
Features
26 |-
27 |
- Declarative 28 |
- Flexible 29 |
- Light footprint 30 |
- Framework-agnostic 31 |
- Supports desktop, mobile & tablets 32 |
- Degrades gracefully to your default styling 33 |
Usage
37 |1. Include the script
38 |39 | <script type="text/javascript" src="delighters.js">40 |
2. Add delighter attributes
41 |42 | <div class="foo" data-delighter>43 |
44 | 3. Style the delighter (or its children) using built-in classes
45 | .started
and .ended
46 |
48 | /* when the library loads, each [data-delighter] 49 | gets the .delighter class */ 50 | 51 | .foo.delighter { 52 | transition: all .3s ease-out; 53 | transform: translateX(-100%); 54 | opacity: 0; 55 | } 56 | 57 | /* the .started class is set when the top 58 | of [data-delighter] is at 0.75 of the viewport 59 | (where 0 is top and 1 is bottom) */ 60 | 61 | .foo.delighter.started { 62 | transform: none; 63 | opacity: 1; 64 | } 65 | 66 | /* an extra .ended state is set when the bottom 67 | of [data-delighter] is at 0.75 of the viewport 68 | (where 0 is top and 1 is bottom) */ 69 | 70 | .foo.delighter.started.ended { 71 | border: solid red 10px; 72 | }73 |
How it works
76 |-
77 |
- Delighters.js toggles classes when you scroll down. You do the rest! 78 |
- Each delighter gets a
.started
class when its top raises above 75% of the browser's viewport height
79 | - When the bottom of the delighter is at 75%, an extra class
.ended
is added
80 | - You can change any of these like so:
data-delighter="start:0.5;end:0.95;
81 | - Remember: 0 = top, 1 = bottom 82 |
- Values outside the viewport are allowed, such as
start:1.2;end:-0.3
83 |
Debugging
87 |-
88 |
- Set the
data-delighter="debug"
flag to enable debugging for that delighter
89 | - This will give the delighter a red outline 90 |
- Also, a dotted red hint line is shown when the delighter is within the viewport 91 |
- The hint line is positioned at the defined start, which by default is
0.75
92 | - The line is removed when the bottom of the delighter is above the end position, by default also
0.75
93 |
Customizing
97 |98 | By default, delighters.js intializes automatically when the DOM is ready, and with the following configuration: 99 |
100 |101 | options = { 102 | attribute: 'data-delighter', 103 | classNames: ['delighter', 'started', 'ended'], 104 | start: 0.75, // default start threshold 105 | end: 0.75, // default end threshold 106 | autoInit: true // initialize when DOMContentLoaded 107 | }108 |
109 | You can customize any or all of the above properties using: 110 |
111 | Delighters.config({ 112 | // set the default start threshold at the bottom 113 | start: 1, 114 | // let's call Delighters.init() manually later 115 | autoInit: false 116 | // ... etc ... 117 | })118 |