Skip to content

Commit 07b0b1b

Browse files
committed
feat: make possible to use different tags as menu items
1 parent 42327b7 commit 07b0b1b

File tree

6 files changed

+44
-10
lines changed

6 files changed

+44
-10
lines changed

README.md

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ If you're not running any transpiler like babel, you'll most likely need to inst
4141

4242
## Usage
4343

44-
You should wrap your menu in a `<scrollactive>` tag (which will be your nav) and add a `.scrollactive-item` class in your `<a>` tags as I show in the example below:
44+
The primary way to use the plugin is to wrap your menu in a `<scrollactive>` tag (which will be your nav) and add a `.scrollactive-item` class in your `<a>` tags as I show in the example below:
4545

4646
```html
4747
<scrollactive class="my-nav">
@@ -54,6 +54,19 @@ You should wrap your menu in a `<scrollactive>` tag (which will be your nav) and
5454

5555
You can follow whatever structure you wish, just make sure to set the `.scrollactive-item` class in the items you want to highlight and set its `href` with a valid element ID that you would like to track while scrolling.
5656

57+
The secondary way to use it is almost the same as the primary but instead of relying on `href` to find your sections you'll need to set a data attribute `data-section-selector` on your elements with the section selector you wish to have.
58+
59+
```html
60+
<scrollactive class="my-nav">
61+
<span data-section-selector="#home" class="scrollactive-item">Home</span>
62+
<span data-section-selector=".about-us" class="scrollactive-item">About Us</span>
63+
<span data-section-selector=".portfolio div span" class="scrollactive-item">Portfolio</span>
64+
<span data-section-selector="#contact" class="scrollactive-item">Contact</span>
65+
</scrollactive>
66+
```
67+
68+
As you can see this gives you more freedom to choose different tags and you can use whatever CSS selector you find necessary, but it's important to notice that `data-section-selector` takes precedence over `href`, so if you have a tag `<a href="#section-1" data-section-selector="#another-section">` it will completely ignore the `#section-1` and use `#another-section` instead.
69+
5770
## Events
5871

5972
Scrollactive will emit an `itemchanged(event, currentItem, lastActiveItem)` event when an active menu item is changed to another. You can catch that event doing as the example below:

src/scrollactive.vue

Lines changed: 13 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,14 @@
11
<script>
22
import bezierEasing from 'bezier-easing';
33
import { ScrollContainer } from './ScrollContainer';
4-
import { forEach, map, find, getIdFromHash, pushHashToUrl } from './utils';
4+
import {
5+
forEach,
6+
find,
7+
getIdFromHash,
8+
pushHashToUrl,
9+
getSectionSelector,
10+
getSectionIdFromElement,
11+
} from './utils';
512
613
export default {
714
props: {
@@ -200,7 +207,7 @@ export default {
200207
const items = [];
201208
202209
forEach(elements, (menuElement) => {
203-
const section = document.getElementById(getIdFromHash(menuElement.hash));
210+
const section = document.querySelector(getSectionSelector(menuElement));
204211
if (!section) return;
205212
206213
items.push({ section, menuElement });
@@ -265,12 +272,12 @@ export default {
265272
event.preventDefault();
266273
267274
const menuItem = event.target;
268-
const { hash } = menuItem;
269-
const section = document.getElementById(getIdFromHash(hash));
275+
const sectionSelector = getSectionSelector(menuItem);
276+
const section = document.querySelector(sectionSelector);
270277
271278
if (!section) {
272279
console.warn(
273-
`[vue-scrollactive] Element '${hash}' was not found. Make sure it is set in the DOM.`
280+
`[vue-scrollactive] Element '${sectionSelector}' was not found. Make sure it is set in the DOM.`
274281
);
275282
276283
return;
@@ -302,7 +309,7 @@ export default {
302309
}
303310
304311
if (this.modifyUrl) {
305-
pushHashToUrl(hash);
312+
pushHashToUrl(getSectionIdFromElement(menuItem));
306313
}
307314
},
308315
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
export const getSectionIdFromElement = (element) => {
2+
if (element.dataset.sectionSelector && element.dataset.sectionSelector.substr(0, 1) === '#') {
3+
return element.dataset.sectionSelector;
4+
}
5+
6+
return element.hash;
7+
};

src/utils/getSectionSelector.js

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
import { getIdFromHash } from './getIdFromHash';
2+
3+
export const getSectionSelector = (element) => {
4+
if (element.dataset.sectionSelector) return element.dataset.sectionSelector;
5+
if (element.hash) return `#${getIdFromHash(element.hash)}`;
6+
7+
return '';
8+
};

src/utils/index.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
export { forEach } from './forEach';
2-
export { map } from './map';
32
export { find } from './find';
43
export { getIdFromHash } from './getIdFromHash';
54
export { pushHashToUrl } from './pushHashToUrl';
5+
export { getSectionSelector } from './getSectionSelector';
6+
export { getSectionIdFromElement } from './getSectionIdFromElement';

src/utils/map.js

Lines changed: 0 additions & 2 deletions
This file was deleted.

0 commit comments

Comments
 (0)