/
anchors.js
88 lines (71 loc) · 2.29 KB
/
anchors.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
import { isScrollSnapActive } from './utils';
module.exports = elementorModules.ViewModule.extend( {
getDefaultSettings() {
return {
scrollDuration: 500,
selectors: {
links: 'a[href*="#"]',
targets: '.elementor-element, .elementor-menu-anchor',
scrollable: isScrollSnapActive() ? 'body' : 'html, body',
},
};
},
getDefaultElements() {
var $ = jQuery,
selectors = this.getSettings( 'selectors' );
return {
$scrollable: $( selectors.scrollable ),
};
},
bindEvents() {
elementorFrontend.elements.$document.on( 'click', this.getSettings( 'selectors.links' ), this.handleAnchorLinks );
},
handleAnchorLinks( event ) {
var clickedLink = event.currentTarget,
isSamePathname = ( location.pathname === clickedLink.pathname ),
isSameHostname = ( location.hostname === clickedLink.hostname ),
$anchor;
if ( ! isSameHostname || ! isSamePathname || clickedLink.hash.length < 2 ) {
return;
}
try {
$anchor = jQuery( clickedLink.hash ).filter( this.getSettings( 'selectors.targets' ) );
} catch ( e ) {
return;
}
if ( ! $anchor.length ) {
return;
}
var scrollTop = $anchor.offset().top,
$wpAdminBar = elementorFrontend.elements.$wpAdminBar,
$activeStickies = jQuery( '.elementor-section.elementor-sticky--active:visible' ),
maxStickyHeight = 0;
if ( $wpAdminBar.length > 0 ) {
scrollTop -= $wpAdminBar.height();
}
// Offset height of tallest sticky
if ( $activeStickies.length > 0 ) {
maxStickyHeight = Math.max.apply( null, $activeStickies.map( function() {
return jQuery( this ).outerHeight();
} ).get() );
scrollTop -= maxStickyHeight;
}
event.preventDefault();
scrollTop = elementorFrontend.hooks.applyFilters( 'frontend/handlers/menu_anchor/scroll_top_distance', scrollTop );
// On scroll animation start: remove scroll-snap.
if ( isScrollSnapActive() ) {
elementorFrontend.elements.$body.css( 'scroll-snap-type', 'none' );
}
this.elements.$scrollable.animate( {
scrollTop,
}, this.getSettings( 'scrollDuration' ), 'linear', () => {
// On scroll animation complete: add scroll-snap back.
if ( isScrollSnapActive() ) {
elementorFrontend.elements.$body.css( 'scroll-snap-type', '' );
}
} );
},
onInit() {
elementorModules.ViewModule.prototype.onInit.apply( this, arguments );
},
} );