PluginProbe
Selection Lite / 1.12
Selection Lite v1.12
trunk 1.0 1.1 1.10 1.11 1.12 1.14 1.15 1.16 1.17 1.2 1.3 1.4 1.5 1.6 1.7 1.8 1.9
selection-lite / js / sticky-effect.js

sticky-effect.js in Selection Lite 1.12, at js/sticky-effect.js

231 lines 10.8 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 /**
2 * Selection Lite
3 * Carefully selected Elementor addons bundle, for building the most awesome websites
4 *
5 * @encoding UTF-8
6 * @version 1.12
7 * @copyright (C) 2018 - 2023 Merkulove ( https://merkulov.design/ ). All rights reserved.
8 * @license GPLv3
9 * @contributors merkulove, vladcherviakov, phoenixmkua, podolianochka, viktorialev01
10 * @support help@merkulov.design
11 **/
12
13 "use strict";
14
15 const mdpStickyEffect = {
16
17 stickySectionEffect: function ( wrapperName, settings ) {
18
19 const stickySection = document.querySelector( `.${wrapperName}` );
20 const scrollDistance = settings.mdp_selection_sticky_effect_scroll_offset.size;
21 const enableOnScrollDown = settings.mdp_selection_sticky_effect_hide_on_scroll_down;
22
23 function setScrollStyles() {
24 stickySection.classList.add( 'mdp-selection-section-sticky' );
25 settings.mdp_selection_sticky_effect_z_index ? stickySection.style.zIndex = settings.mdp_selection_sticky_effect_z_index : stickySection.style.removeProperty( 'z-index' );
26 stickySection.style.top = ( settings.mdp_selection_sticky_section_effect_offset_top.size ? settings.mdp_selection_sticky_section_effect_offset_top.size : 0 ) + 'px';
27 }
28
29 function removeScrollStyles() {
30 stickySection.classList.remove( 'mdp-selection-section-sticky' );
31 stickySection.style.removeProperty( 'z-index' );
32 stickySection.style.removeProperty( 'top' );
33 stickySection.classList.remove( 'mdp-selection-section-sticky-hide' );
34 }
35
36 let scroll_position = 0;
37 let scroll_direction;
38
39 const scrollSectionListener = () => {
40 if ( !document.querySelector( `.${wrapperName}` ) ) { window.removeEventListener( 'scroll', scrollSectionListener ) }
41 stickySection.classList.add( 'mdp-selection-section-sticky' );
42 const scrollOffset = window.scrollY;
43 if ( scrollOffset >= scrollDistance && document.querySelector( `.${wrapperName}` ) ) {
44 setScrollStyles();
45 if ( enableOnScrollDown === 'yes' ) {
46
47 scroll_direction = document.body.getBoundingClientRect().top > scroll_position ? 'up' : 'down';
48 scroll_position = document.body.getBoundingClientRect().top;
49
50 if ( scroll_direction === 'up' ) {
51 stickySection.classList.remove( 'mdp-selection-section-sticky-hide' );
52 } else if ( scroll_direction === 'down' ) {
53 stickySection.classList.add( 'mdp-selection-section-sticky-hide' );
54 }
55
56 }
57
58 } else {
59 stickySection.classList.remove( 'mdp-selection-section-sticky' );
60 removeScrollStyles();
61 }
62 }
63
64 window.addEventListener( 'scroll', scrollSectionListener );
65
66 },
67
68 stickyColumnEffect: function ( wrapperName, settings ) {
69
70 const wrapper = document.querySelector( `.${wrapperName}` );
71 if ( !wrapper ) { return; }
72 const stickyColumn = document.querySelector( `.${wrapperName} .elementor-widget-wrap` );
73 const stickyColumnSection = stickyColumn.parentElement.parentElement;
74 const stickyColumnSectionOffsetTop = settings.mdp_selection_sticky_column_effect_offset_top ? settings.mdp_selection_sticky_column_effect_offset_top : 0;
75 const stickyColumnParentElement = stickyColumn.parentElement;
76 let stickyColumnSectionInitialHeight;
77
78 stickyColumnSectionInitialHeight = stickyColumnSection.getBoundingClientRect().height;
79
80 const scrollColumnListener = () => {
81 if ( !document.querySelector( `.${wrapperName}` ) ) {
82 window.removeEventListener( 'scroll', scrollColumnListener );
83 }
84
85 if ( stickyColumn.closest( '.mdp-selection-section-sticky' ) ) {
86 stickyColumnParentElement.style.removeProperty( 'align-items' );
87 stickyColumn.style.removeProperty( 'top' );
88 stickyColumn.style.removeProperty( 'bottom' );
89 stickyColumn.style.removeProperty( 'height' );
90 stickyColumnSection.style.removeProperty( 'height' );
91 return;
92 }
93
94 const stickyColumnSectionTopOffset = stickyColumnSection.getBoundingClientRect().top;
95 const startPosition = stickyColumnSectionOffsetTop.size;
96 let isNotEqualHeight;
97
98 // set height and width of sticky column
99 stickyColumn.style.height = 'fit-content';
100 stickyColumn.style.width = stickyColumnParentElement.offsetWidth + 'px';
101
102 if ( stickyColumn.getBoundingClientRect().height !== wrapper.getBoundingClientRect().height ) {
103 isNotEqualHeight = true;
104 } else isNotEqualHeight = !!settings.mdp_selection_sticky_column_effect_enable_on.includes( 'mobile' );
105
106 if ( isNotEqualHeight ) {
107 if ( stickyColumnSectionTopOffset <= startPosition && document.querySelector( `.${wrapperName}` ) ) {
108 // set height of column section on mobile
109 if ( window.matchMedia( `(max-width: ${elementorFrontend.config.responsive.activeBreakpoints.mobile.value}px)` ).matches ) {
110 stickyColumnSection.style.height = stickyColumnSectionInitialHeight + 'px';
111 }
112 if ( stickyColumnSection.getBoundingClientRect().bottom >= ( parseInt( window.getComputedStyle( stickyColumn ).getPropertyValue( 'height' ) ) + stickyColumnSectionOffsetTop.size ) ) {
113 stickyColumn.classList.add( 'mdp-selection-column-sticky' );
114 stickyColumn.style.top = `${stickyColumnSectionOffsetTop.size}${stickyColumnSectionOffsetTop.unit}`;
115 } else {
116 stickyColumn.classList.remove( 'mdp-selection-column-sticky' );
117 stickyColumnParentElement.style.alignItems = 'flex-end';
118 stickyColumn.style.removeProperty( 'top' );
119 stickyColumn.style.removeProperty( 'bottom' );
120 stickyColumn.style.removeProperty( 'height' );
121 stickyColumnSection.style.removeProperty( 'height' );
122 stickyColumn.style.removeProperty( 'width' );
123 }
124 } else {
125 stickyColumn.classList.remove( 'mdp-selection-column-sticky' );
126 stickyColumnParentElement.style.removeProperty( 'align-items' );
127 stickyColumn.style.removeProperty( 'top' );
128 stickyColumn.style.removeProperty( 'bottom' );
129 stickyColumn.style.removeProperty( 'height' );
130 stickyColumnSection.style.removeProperty( 'height' );
131 stickyColumn.style.removeProperty( 'width' );
132 }
133
134 }
135
136 }
137
138 window.addEventListener( 'scroll', scrollColumnListener );
139
140 },
141
142 checkScreenSize: function ( enableOn, callback ) {
143
144 if ( enableOn.includes( 'mobile' ) && window.matchMedia( `(max-width: ${elementorFrontend.config.responsive.activeBreakpoints.mobile.value}px)` ).matches ) {
145 callback();
146 }
147
148 if ( enableOn.includes( 'tablet' ) && window.matchMedia( `(min-width: ${elementorFrontend.config.responsive.activeBreakpoints.mobile.value + 1}px)` ).matches
149 && window.matchMedia( `(max-width: ${elementorFrontend.config.responsive.activeBreakpoints.tablet.value}px)` ).matches ) {
150 callback();
151 }
152
153 if ( enableOn.includes( 'desktop' ) && window.matchMedia( `(min-width: ${elementorFrontend.config.responsive.activeBreakpoints.tablet.value + 1}px)` ).matches ) {
154 callback();
155 }
156 },
157
158 setStickyEffect: function ( $column, $section ) {
159
160 // sticky columns
161 const isEditor = elementorFrontend.isEditMode();
162 const elements_editor_data = isEditor ? elementorFrontend.config.elements.data : [];
163 let settingsColumns = {};
164 let settingsSections = {};
165
166 function setSettings( element ) {
167 let settings = {};
168 if ( isEditor ) {
169 if ( element.dataset.modelCid ) {
170 const elementsDataset = elements_editor_data[element.dataset.modelCid].attributes;
171 for ( let key in elementsDataset ) {
172 if ( key.startsWith( 'mdp' ) ) {
173 settings[key] = elementsDataset[key];
174 }
175 }
176 } else if ( element.dataset.settings ) {
177 settings = JSON.parse( element.dataset.settings );
178 }
179 } else if ( !isEditor && element.dataset.settings ) {
180 settings = JSON.parse( element.dataset.settings );
181 }
182
183 return settings;
184 }
185
186 // sticky column
187 if ( $column ) {
188 settingsColumns = setSettings( $column );
189 if ( settingsColumns.mdp_selection_sticky_column_effect_enable === 'yes' ) {
190 $column.classList.add(`mdp-selection-column-sticky-effect-yes-${$column.dataset.id}`);
191 this.checkScreenSize( settingsColumns.mdp_selection_sticky_column_effect_enable_on, this.stickyColumnEffect.bind( null, `mdp-selection-column-sticky-effect-yes-${$column.dataset.id}`, settingsColumns ) );
192 } else {
193 $column.classList.remove( `mdp-selection-column-sticky-effect-yes-${$column.dataset.id}` );
194 }
195 }
196
197 // sticky section
198 if ( $section ) {
199 settingsSections = setSettings( $section );
200 if ( settingsSections.mdp_selection_sticky_effect_enable === 'yes' ) {
201 $section.classList.add(`mdp-selection-section-sticky-effect-yes-${$section.dataset.id}`);
202 this.checkScreenSize( settingsSections.mdp_selection_sticky_effect_enable_on, this.stickySectionEffect.bind( null, `mdp-selection-section-sticky-effect-yes-${$section.dataset.id}`, settingsSections) );
203 } else {
204 $section.classList.remove( `mdp-selection-section-sticky-effect-yes-${$section.dataset.id}` );
205 $section.classList.remove( 'mdp-selection-section-sticky' );
206 }
207 }
208
209 }
210
211 };
212
213 jQuery( window ).on( 'elementor/frontend/init', function () {
214
215 elementorFrontend.hooks.addAction( 'frontend/element_ready/global', function( $scope ) {
216
217 const element = $scope[0].dataset.element_type;
218
219 if ( element === 'column' ) {
220 setTimeout( () => {
221 mdpStickyEffect.setStickyEffect.call( mdpStickyEffect, $scope[0], null );
222 }, 0 );
223 } else if ( element === 'section' ) {
224 setTimeout( () => {
225 mdpStickyEffect.setStickyEffect.call( mdpStickyEffect, null, $scope[0] );
226 }, 0 );
227 }
228 } );
229
230 } );
231