Skip to content

Commit adf5498

Browse files
author
cferdinandi
committed
Updated readme
1 parent bca9a8f commit adf5498

File tree

1 file changed

+22
-9
lines changed

1 file changed

+22
-9
lines changed

README.md

Lines changed: 22 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -226,19 +226,32 @@ If the `<body>` element has been assigned a height of `100%`, Smooth Scroll is u
226226

227227

228228

229-
## Adding `[data-scroll]` attributes to the `wp_nav_menu()` in WordPress
229+
## Programatically adding `[data-scroll]` attributes to all anchor links
230230

231-
Add this to your `functions.php` file:
231+
Useful if you have anchor links scattered throughout a page, or if you're using WordPress's `wp_nav_menu()` function. Add this code to your JavaScript:
232232

233233
```js
234-
function YOURPREFIX_custom_nav_attributes ( $atts, $item, $args ) {
235-
$atts['data-scroll'] = 'true';
236-
return $atts;
237-
}
238-
add_filter( 'nav_menu_link_attributes', 'YOURPREFIX_custom_nav_attributes', 10, 3 );
239-
```
234+
;(function (window, document, undefined) {
235+
236+
'use strict';
237+
238+
// Cut the mustard
239+
var supports = 'querySelector' in document && 'addEventListener' in window;
240+
if ( !supports ) return;
241+
242+
// Get all anchors
243+
var anchors = document.querySelectorAll( '[href*="#"]' );
240244

241-
**Source:** http://wordpress.stackexchange.com/questions/121123/how-to-add-a-data-attribute-to-a-wordpress-menu-item
245+
// Add smooth scroll to all anchors
246+
for ( var i = 0, len = anchors.length; i < len; i++ ) {
247+
anchors[i].setAttribute( 'data-scroll', true );
248+
}
249+
250+
// Initial smooth scroll (add your attributes as desired)
251+
smoothScroll.init();
252+
253+
})(window, document);
254+
```
242255

243256

244257

0 commit comments

Comments
 (0)