File size: 2,182 Bytes
fc05821 |
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 |
// 页面加载完成
document.addEventListener('DOMContentLoaded', function() {
// 懒加载图片
const lazyImages = document.querySelectorAll('img[data-src]');
const imageObserver = new IntersectionObserver((entries, observer) => {
entries.forEach(entry => {
if (entry.isIntersecting) {
const img = entry.target;
img.src = img.dataset.src;
img.classList.add('loaded');
observer.unobserve(img);
}
});
});
lazyImages.forEach(img => imageObserver.observe(img));
// 懒加载内容
const lazyElements = document.querySelectorAll('.lazy-load');
const elementObserver = new IntersectionObserver((entries, observer) => {
entries.forEach(entry => {
if (entry.isIntersecting) {
entry.target.classList.add('loaded');
observer.unobserve(entry.target);
}
});
}, { threshold: 0.1 });
lazyElements.forEach(el => elementObserver.observe(el));
});
// 初始化图表
function initChart(ctx, config) {
return new Chart(ctx, config);
}
// 格式化数字
function formatNumber(num, decimals = 0) {
if (num >= 100000000) {
return (num / 100000000).toFixed(decimals) + '亿';
} else if (num >= 10000) {
return (num / 10000).toFixed(decimals) + '万';
}
return num.toLocaleString('zh-CN', { maximumFractionDigits: decimals });
}
// 格式化百分比
function formatPercent(value, decimals = 1) {
return (value * 100).toFixed(decimals) + '%';
}
// 创建趋势指示器
function createTrendIndicator(value) {
const isPositive = value >= 0;
const icon = isPositive ? '↑' : '↓';
const className = isPositive ? 'stat-change positive' : 'stat-change negative';
return `<span class="${className}">${icon} ${Math.abs(value).toFixed(1)}%</span>`;
}
// 响应式图表配置
function getResponsiveConfig(baseConfig) {
return {
...baseConfig,
options: {
...baseConfig.options,
responsive: true,
maintainAspectRatio: false,
plugins: {
...baseConfig.options?.plugins,
legend: {
...baseConfig.options?.plugins?.legend,
display: window.innerWidth > 768
}
}
}
};
} |