Skip to content

Commit db53acb

Browse files
committed
fix: lazy loading not working without mouse action
1 parent 2e7d9be commit db53acb

1 file changed

Lines changed: 20 additions & 1 deletion

File tree

classes/Visualizer/Module/Frontend.php

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -734,6 +734,7 @@ public function printFooterScripts() {
734734
if ( $this->lazy_render_script ) {
735735
?>
736736
<script type="text/javascript">
737+
var visualizerScriptsLoaded = false;
737738
var visualizerUserInteractionEvents = [
738739
"scroll",
739740
"mouseover",
@@ -747,13 +748,17 @@ public function printFooterScripts() {
747748
});
748749

749750
function visualizerTriggerScriptLoader() {
750-
visualizerLoadScripts();
751751
visualizerUserInteractionEvents.forEach(function(event) {
752752
window.removeEventListener(event, visualizerTriggerScriptLoader, { passive: true });
753753
});
754+
visualizerLoadScripts();
754755
}
755756

756757
function visualizerLoadScripts() {
758+
if ( visualizerScriptsLoaded ) {
759+
return;
760+
}
761+
visualizerScriptsLoaded = true;
757762
document.querySelectorAll("script[data-visualizer-script]").forEach(function(elem) {
758763
jQuery.getScript( elem.getAttribute("data-visualizer-script") )
759764
.done( function( script, textStatus ) {
@@ -776,6 +781,20 @@ function visualizerRefreshChart() {
776781
} );
777782
}
778783
}
784+
785+
// This script tag is printed before the enqueued script tags (data-visualizer-script),
786+
// so defer the viewport check until after the full DOM is parsed and those elements exist.
787+
setTimeout( function() {
788+
var charts = document.querySelectorAll( '.visualizer-front' );
789+
var viewportHeight = window.innerHeight || document.documentElement.clientHeight;
790+
for ( var i = 0; i < charts.length; i++ ) {
791+
var rect = charts[ i ].getBoundingClientRect();
792+
if ( rect.bottom >= 0 && rect.top <= viewportHeight ) {
793+
visualizerLoadScripts();
794+
return;
795+
}
796+
}
797+
}, 0 );
779798
</script>
780799
<?php
781800
}

0 commit comments

Comments
 (0)