You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

113 lines
3.4 KiB

This file contains ambiguous Unicode characters!

This file contains ambiguous Unicode characters that may be confused with others in your current locale. If your use case is intentional and legitimate, you can safely ignore this warning. Use the Escape button to highlight these characters.

/**
* GSAP与Elementor初始化管理器
* 确保GSAP和Elementor的正确初始化顺序
*
* @package Nenghui Energy Theme
* @since 1.0.0
*/
(function() {
'use strict';
// 初始化管理器
const GSAPElementorInitManager = {
initialized: false,
// 初始化
init: function() {
if (this.initialized) return;
// 等待DOM加载完成
if (document.readyState === 'loading') {
document.addEventListener('DOMContentLoaded', () => {
this.setupInitialization();
});
} else {
this.setupInitialization();
}
},
// 设置初始化流程
setupInitialization: function() {
// 1. 首先等待GSAP加载
this.waitForGSAP(() => {
// 2. 然后等待兼容性管理器
this.waitForCompatibilityManager(() => {
// 3. 最后初始化兼容性管理器
if (window.GSAPElementorCompatibility) {
window.GSAPElementorCompatibility.init();
}
// 4. 触发自定义事件通知其他脚本可以安全使用GSAP
this.triggerGSAPReady();
this.initialized = true;
});
});
},
// 等待GSAP加载
waitForGSAP: function(callback) {
let attempts = 0;
const maxAttempts = 50;
const checkGSAP = () => {
if (window.gsap && window.ScrollTrigger) {
callback();
} else if (attempts < maxAttempts) {
attempts++;
setTimeout(checkGSAP, 100);
} else {
console.error('GSAP加载超时');
}
};
checkGSAP();
},
// 等待兼容性管理器加载
waitForCompatibilityManager: function(callback) {
let attempts = 0;
const maxAttempts = 50;
const checkManager = () => {
if (window.GSAPElementorCompatibility) {
callback();
} else if (attempts < maxAttempts) {
attempts++;
setTimeout(checkManager, 100);
} else {
callback(); // 即使失败也继续执行
}
};
checkManager();
},
// 触发GSAP准备就绪事件
triggerGSAPReady: function() {
// 创建自定义事件
const gsapReadyEvent = new CustomEvent('gsapReady', {
detail: {
gsap: window.gsap,
ScrollTrigger: window.ScrollTrigger,
api: window.GSAPElementorAPI,
manager: window.GSAPAnimationManager
}
});
// 触发事件
document.dispatchEvent(gsapReadyEvent);
// 同时设置全局标志
window.GSAPReady = true;
}
};
// 立即初始化
GSAPElementorInitManager.init();
// 暴露到全局作用域
window.GSAPElementorInitManager = GSAPElementorInitManager;
})();