Skip to content

Commit 8a23e4b

Browse files
committed
feat: upgrade to version 1.5.0 with new features and improvements
- Added new utility functions for handling font sizes and element offsets in pixels. - Enhanced parsing of offset expressions to support various units (px, rem, em, vh, vw). - Improved handling of anchor links and scroll behavior. - Updated internal logic for managing instances and resize observers. - Refactored code for better readability and maintainability. - Updated package version in package.json to 1.5.0.
1 parent 17af5b3 commit 8a23e4b

5 files changed

Lines changed: 344 additions & 41 deletions

File tree

README.md

Lines changed: 100 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
- Support for **multiple smooth scroll instances**
1414
- A clean global API under `window.rtSmoothScroll`
1515
- **Smart Scroll-To actions** with indexed selectors and dynamic offsets
16+
- **Advanced Offset Expressions** with selectors plus CSS length units
1617
- **Automatic Anchor Link Conversion** (hijack native links for smooth scrolling)
1718
- **Automatic DOM Resize Detection** (uses `ResizeObserver` to update scroll height on dynamic content changes)
1819
- **Scroll-To Completion Hooks** (run actions/functions after a scroll-to completes)
@@ -46,14 +47,13 @@
4647
### 1.1 CDN (jsDelivr)
4748

4849
```html
49-
<script src="[https://cdn.jsdelivr.net/npm/@rethink-js/rt-smooth-scroll@latest/dist/index.min.js](https://cdn.jsdelivr.net/npm/@rethink-js/rt-smooth-scroll@latest/dist/index.min.js)"></script>
50+
<script src="https://cdn.jsdelivr.net/npm/@rethink-js/rt-smooth-scroll@latest/dist/index.min.js"></script>
5051
```
5152

5253
### 1.2 npm
5354

5455
```bash
5556
npm install @rethink-js/rt-smooth-scroll
56-
5757
```
5858

5959
Then bundle or load `dist/index.min.js` as appropriate for your build setup.
@@ -72,7 +72,7 @@ Add the script to your page. With no configuration provided, `rt-smooth-scroll`
7272
Example:
7373

7474
```html
75-
<script src="[https://cdn.jsdelivr.net/npm/@rethink-js/rt-smooth-scroll@latest/dist/index.min.js](https://cdn.jsdelivr.net/npm/@rethink-js/rt-smooth-scroll@latest/dist/index.min.js)"></script>
75+
<script src="https://cdn.jsdelivr.net/npm/@rethink-js/rt-smooth-scroll@latest/dist/index.min.js"></script>
7676
```
7777

7878
> Note: If you do not set any `rt-smooth-scroll-*` config attributes, the root instance uses **Lenis defaults**.
@@ -228,25 +228,102 @@ You can target elements by class order (1-based index) using `.class(N)` syntax.
228228

229229
You can customize the scroll behavior for specific triggers.
230230

231-
| Attribute | Description |
232-
| ---------------------------- | ------------------------------------------------------------------ |
233-
| `rt-smooth-scroll-offset` | Offset in pixels. **Supports selectors!** (See below) |
234-
| `rt-smooth-scroll-duration` | Override scroll duration for this action |
235-
| `rt-smooth-scroll-immediate` | Jump instantly (true/false) |
236-
| `rt-smooth-scroll-lock` | Lock scroll during animation |
237-
| `rt-smooth-scroll-force` | Force scroll even if stopped |
238-
| `rt-smooth-scroll-target-id` | **Explicitly** target a specific scroll instance ID (e.g. "panel") |
231+
| Attribute | Description |
232+
| ---------------------------- | ------------------------------------------------------------------------------------- |
233+
| `rt-smooth-scroll-offset` | Offset value. Supports numbers, CSS length units, selectors, and selector expressions |
234+
| `rt-smooth-scroll-duration` | Override scroll duration for this action |
235+
| `rt-smooth-scroll-immediate` | Jump instantly (true/false) |
236+
| `rt-smooth-scroll-lock` | Lock scroll during animation |
237+
| `rt-smooth-scroll-force` | Force scroll even if stopped |
238+
| `rt-smooth-scroll-target-id` | **Explicitly** target a specific scroll instance ID (e.g. `"panel"`) |
239+
240+
### Offset Value Support
241+
242+
`rt-smooth-scroll-offset` now supports all of the following:
243+
244+
#### 1) Plain numeric values
245+
246+
These are treated as pixel values.
247+
248+
```html
249+
<button rt-smooth-scroll-to="#about" rt-smooth-scroll-offset="32">About</button>
250+
```
251+
252+
#### 2) CSS length units
253+
254+
Supported units:
255+
256+
- `px`
257+
- `rem`
258+
- `em`
259+
- `vh`
260+
- `vw`
261+
262+
Examples:
263+
264+
```html
265+
<button rt-smooth-scroll-to="#about" rt-smooth-scroll-offset="32px">
266+
About
267+
</button>
268+
269+
<button rt-smooth-scroll-to="#about" rt-smooth-scroll-offset="2rem">
270+
About
271+
</button>
272+
273+
<button rt-smooth-scroll-to="#about" rt-smooth-scroll-offset="5vh">
274+
About
275+
</button>
276+
```
277+
278+
#### 3) Dynamic element offsets
239279

240-
### Dynamic Element Offsets
280+
Instead of hardcoding pixels, you can pass a selector to `rt-smooth-scroll-offset`. The library will calculate that element’s `offsetHeight` and apply it as a **negative offset**.
241281

242-
Instead of hardcoding pixels, you can pass a selector to `rt-smooth-scroll-offset`. The library will calculate that element's `offsetHeight` and apply it as a **negative offset** (perfect for sticky headers).
282+
This is useful for sticky headers and fixed navbars.
243283

244284
```html
245285
<button rt-smooth-scroll-to="#about" rt-smooth-scroll-offset="#nav">
246286
About
247287
</button>
248288
```
249289

290+
#### 4) Offset expressions
291+
292+
You can combine selectors and CSS length values with `+` or `-`.
293+
294+
Examples:
295+
296+
```html
297+
<button rt-smooth-scroll-to="#about" rt-smooth-scroll-offset="#nav + 32px">
298+
About
299+
</button>
300+
301+
<button rt-smooth-scroll-to="#about" rt-smooth-scroll-offset="#nav + 2rem">
302+
About
303+
</button>
304+
305+
<button rt-smooth-scroll-to="#about" rt-smooth-scroll-offset="#nav + 5vh">
306+
About
307+
</button>
308+
309+
<button rt-smooth-scroll-to="#about" rt-smooth-scroll-offset="#nav - 16px">
310+
About
311+
</button>
312+
```
313+
314+
How expressions are resolved:
315+
316+
- Length values (`32`, `32px`, `2rem`, `5vh`, etc.) are converted into pixels
317+
- Selectors resolve to that element’s `offsetHeight`
318+
- If one or more selectors are present in the expression, the final expression is applied as a **negative offset**
319+
- If the expression contains only numeric/length values, it is applied as a normal numeric offset
320+
321+
This means:
322+
323+
- `rt-smooth-scroll-offset="#nav"` becomes negative nav height
324+
- `rt-smooth-scroll-offset="#nav + 32px"` becomes negative `(nav height + 32px)`
325+
- `rt-smooth-scroll-offset="32px"` becomes positive `32`
326+
250327
### Scroll-To Completion Hooks
251328

252329
When a scroll-to finishes (including the built-in correction pass for layout shifts), you can run an action/function.
@@ -480,6 +557,16 @@ This library includes a built-in `ResizeObserver` that watches your content for
480557
- Ensure your content is properly wrapped.
481558
- If you have rapid animations causing lag, you can debounce the resize events using `rt-smooth-scroll-resize-debounce-ms="100"`.
482559

560+
### My `rt-smooth-scroll-offset` expression is not working
561+
562+
Check the following:
563+
564+
- Use valid selectors and valid CSS length units only
565+
- Supported units are `px`, `rem`, `em`, `vh`, and `vw`
566+
- Leave spaces around operators for readability, e.g. `#nav + 2rem`
567+
- Do not use unsupported CSS functions like `calc()`
568+
- Expressions are intended for additive/subtractive combinations, e.g. `#nav + 32px` or `#nav - 16px`
569+
483570
### My `rt-smooth-scroll-on-complete` didn’t run
484571

485572
Common causes:

dist/index.js

Lines changed: 110 additions & 12 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)