|
2 | 2 |
|
3 | 3 | Getting DOM Elements' relative position with CSS variable when page scroll. With mapping support for values, it is easy to create scroll-to-position animation like video playback.
|
4 | 4 |
|
5 |
| -Read this article in other languages: [English](README.md), [繁體中文](README.zh-Hant.md), [简体中文](README.zh-Hans.md). |
| 5 | +Read this document in other languages: [English](README.md), [繁體中文](README.zh-Hant.md), [简体中文](README.zh-Hans.md). |
6 | 6 |
|
7 | 7 | ## Introduction
|
8 | 8 |
|
@@ -42,16 +42,18 @@ In the above example, CSS variable `--scrolled` will be available to `#greeting`
|
42 | 42 |
|
43 | 43 | ## The `tg-` Attributes
|
44 | 44 |
|
45 |
| -| Attribute | Type | Default | Description | |
46 |
| -| ----------- | -------- | ------- | --------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- | |
47 |
| -| `tg-name` | Required | - | The CSS variable name to store the value, with or without `--` prefix. | |
48 |
| -| `tg-from` | Optional | `0` | The start value | |
49 |
| -| `tg-to` | Optional | `1` | The end value | |
50 |
| -| `tg-steps` | Optional | `100` | How many steps between `tg-from` and `tg-to` | |
51 |
| -| `tg-step` | Optional | `0` | Step per increment, if this value is other than `0`, will override `tg-steps`. | |
52 |
| -| `tg-map` | Optional | (Empty) | map the value to another value. Format: `value: newValue; value2: newValue2`. Multiple values map to one value is also supported: `value,value2,value3: newValue` | |
53 |
| -| `tg-filter` | Optional | (Empty) | Only trigger if the scroll value is on the list. Format: `1,3,5,7,9` | |
54 |
| -| `tg-edge` | Optional | cover | When should the calculation starts. `cover` means off-screen to off-screen, the calculation begins right on the element appear; `inset` means the calculation begins after the whole element appears on screen, ends right before off-screen. | |
| 45 | +| Attribute | Type | Default | Description | |
| 46 | +| ----------- | -------- | ------- | --------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- | |
| 47 | +| `tg-name` | Required | - | The CSS variable name to store the value, with or without `--` prefix. | |
| 48 | +| `tg-from` | Optional | `0` | The start value | |
| 49 | +| `tg-to` | Optional | `1` | The end value | |
| 50 | +| `tg-steps` | Optional | `100` | How many steps between `tg-from` and `tg-to` | |
| 51 | +| `tg-step` | Optional | `0` | Step per increment, if this value is other than `0`, will override `tg-steps`. | |
| 52 | +| `tg-map` | Optional | (Empty) | map the value to another value. Format: `value: newValue; value2: newValue2`. Multiple values map to one value is also supported: `value,value2,value3: newValue` | |
| 53 | +| `tg-filter` | Optional | (Empty) | Only trigger if the scroll value is on the list. Format: `1,3,5,7,9`. By default, the filter mode is `retain`, if we want to switch the mode to `exact`, add an `!` at the end. Read more about this in a dedicated section below. | |
| 54 | +| `tg-edge` | Optional | cover | When should the calculation starts. `cover` means off-screen to off-screen, the calculation begins right on the element appear; `inset` means the calculation begins after the whole element appears on screen, ends right before off-screen. | |
| 55 | +| `tg-follow` | Optional | (Empty) | Use the calculation result from another element instead. The value of `tg-follow` is the value of the target element's `tg-ref`. **Important**: When `tg-follow` is set, `tg-from`, `tg-to`, `tg-steps`, `tg-step` and `tg-edge` will be ignored in the same element. | |
| 56 | +| `tg-ref` | Optional | (Empty) | Define the name that can be referenced (followed) by another element using `tg-follow`. | |
55 | 57 |
|
56 | 58 | ## Value Mapping
|
57 | 59 |
|
@@ -128,6 +130,73 @@ Sometimes we only interested in certain values. For example, we only want to kno
|
128 | 130 | </style>
|
129 | 131 | ```
|
130 | 132 |
|
| 133 | +## The mode of `tg-filter` |
| 134 | + |
| 135 | +There are two modes for `tg-filter`, `retain` by default, the other one is `exact`. Let's use an example to clarify this: |
| 136 | + |
| 137 | +```html |
| 138 | +<h1 |
| 139 | + id="heading" |
| 140 | + tg-name="color" |
| 141 | + tg-from="0" |
| 142 | + tg-to="10" |
| 143 | + tg-step="1" |
| 144 | + tg-filter="5" |
| 145 | + tg-map="5: blue" |
| 146 | +> |
| 147 | + Trigger.js |
| 148 | +</h1> |
| 149 | + |
| 150 | +<style> |
| 151 | + body { |
| 152 | + padding: 100vh 0; /* In order to make the page have enough rooms for scrolling */ |
| 153 | + } |
| 154 | +
|
| 155 | + #heading { |
| 156 | + --color: black; |
| 157 | + color: var(--color); |
| 158 | + } |
| 159 | +</style> |
| 160 | +``` |
| 161 | + |
| 162 | +In the above example, the text has an initial color of black, and it will turn blue when it arrives at the middle of the page and never turn black again because there is nothing to trigger the black color. |
| 163 | + |
| 164 | +So let's say we want the text color becomes blue only when the calculation value is `5`, and becomes black for other values, We can change it to: |
| 165 | + |
| 166 | +```html |
| 167 | +<h1 |
| 168 | + id="heading" |
| 169 | + tg-name="color" |
| 170 | + tg-from="0" |
| 171 | + tg-to="10" |
| 172 | + tg-step="1" |
| 173 | + tg-filter="4,5,6" |
| 174 | + tg-map="4: black; 5: blue; 6: black" |
| 175 | +> |
| 176 | + Trigger.js |
| 177 | +</h1> |
| 178 | +``` |
| 179 | + |
| 180 | +This works, but the code becomes redundant quickly. To solve this, we can switch the filter mode to `exact` by adding an `!` at the end of `tg-filter`: |
| 181 | + |
| 182 | +```html |
| 183 | +<h1 |
| 184 | + id="heading" |
| 185 | + tg-name="color" |
| 186 | + tg-from="0" |
| 187 | + tg-to="10" |
| 188 | + tg-step="1" |
| 189 | + tg-filter="5!" |
| 190 | + tg-map="5: blue" |
| 191 | +> |
| 192 | + Trigger.js |
| 193 | +</h1> |
| 194 | +``` |
| 195 | + |
| 196 | +In `exact` mode, `--color` becomes `blue` when the value is `5`, and becomes the default when the value is other than `5`. |
| 197 | + |
| 198 | +The design by adding an `!` directly to `tg-filter` is that this requirement should only happen when `tg-filter` is used. Separating to yet another attribute should not be necessary and might cause misunderstanding. |
| 199 | + |
131 | 200 | ## Value Inheritance
|
132 | 201 |
|
133 | 202 | Just like some CSS properties, the values of `tg-` attributes are inherit from parents if not being set in the current element. If we do not want to inherit from parent and set it as default value, just add the `tg-` attribute without value. For example:
|
|
0 commit comments