Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/beyonk-adventures/svelte-scrollspy
Scroll Spy component for Svelte
https://github.com/beyonk-adventures/svelte-scrollspy
beyonk intersection-observer scroll scrollspy svelte svelte-v3
Last synced: about 1 month ago
JSON representation
Scroll Spy component for Svelte
- Host: GitHub
- URL: https://github.com/beyonk-adventures/svelte-scrollspy
- Owner: beyonk-group
- Created: 2018-11-08T11:21:53.000Z (about 6 years ago)
- Default Branch: master
- Last Pushed: 2023-01-04T17:30:38.000Z (almost 2 years ago)
- Last Synced: 2024-04-14T13:44:04.863Z (9 months ago)
- Topics: beyonk, intersection-observer, scroll, scrollspy, svelte, svelte-v3
- Language: JavaScript
- Size: 363 KB
- Stars: 38
- Watchers: 3
- Forks: 4
- Open Issues: 11
-
Metadata Files:
- Readme: README.md
Awesome Lists containing this project
README
## Svelte Scroll Spy
[![js-standard-style](https://img.shields.io/badge/code%20style-standard-brightgreen.svg)](http://standardjs.com) [![CircleCI](https://circleci.com/gh/beyonk-adventures/svelte-scrollspy.svg?style=shield)](https://circleci.com/gh/beyonk-adventures/svelte-scrollspy) [![svelte-v2](https://img.shields.io/badge/svelte-v2-orange.svg)](https://v2.svelte.dev) [![svelte-v3](https://img.shields.io/badge/svelte-v3-blueviolet.svg)](https://svelte.dev)
Svelte Scroll Spy component
This component uses IntersectionObservers (and a polyfill for non-supporting browsers like Safari) so that performance is quick and doesn't impact the user's experience.
## Usage
This library is pure javascript, so can be used with any framework you like.
### Demo
```
npm run dev # http://localhost:12001
```### To use within a Svelte application:
```bash
npm i -D @beyonk/svelte-scrollspy
```## Usage
```jsx
-
{section.title}
{#each sections as section (section.id)}
{/each}
{#each sections as section (section.id)}
{section.title}
{section.content}
{/each}
import { SectionHeader, ScrollableSection, ScrollSpy } from '@beyonk/svelte-scrollspy'
const sections = [
{ id: 'abc', title: 'Some Title', content: 'Lorem ipsum dolor...' },
{ id: 'def', title: 'Some Other Title', content: 'Lorem ipsum dolor...' },
{ id: 'ghi', title: 'A Different Title', content: 'Lorem ipsum dolor...' }
]
```
You need to set id on each `ScrollableHeader` and its corresponding `ScrollableSection`. Besides that, everything else is pretty freeform.
Upon scrolling to the appropriate section of the page, the heading of the section which is topmost visible on the page will get the class 'active'.
Therefore, you could indicate the current section in the example above by highlighting it in red as follows:
```html
:global(.beyonk-svelte-scrollspy .active) {
color: red;
}
```