优化首页样式
This commit is contained in:
@@ -0,0 +1,140 @@
|
||||
(() => {
|
||||
const main = document.querySelector('.site-main');
|
||||
const heroSection = document.querySelector('.hero-section');
|
||||
const servicesSection = document.querySelector('.services-section');
|
||||
const canvas = document.querySelector('.hero-waves');
|
||||
if (!main || !canvas) return;
|
||||
|
||||
const ctx = canvas.getContext('2d');
|
||||
if (!ctx) return;
|
||||
|
||||
const prefersReduced = window.matchMedia('(prefers-reduced-motion: reduce)');
|
||||
let width = 0;
|
||||
let height = 0;
|
||||
let dpr = 1;
|
||||
let rafId = null;
|
||||
|
||||
const waveDefs = [
|
||||
{ yRatio: 0.3, amplitude: 22, wavelength: 940, speed: 0.5, alpha: 0.98 },
|
||||
{ yRatio: 0.45, amplitude: 16, wavelength: 1020, speed: 0.58, alpha: 0.92 },
|
||||
{ yRatio: 0.65, amplitude: 14, wavelength: 1160, speed: 0.6, alpha: 0.9 },
|
||||
];
|
||||
|
||||
const palettes = {
|
||||
light: {
|
||||
gradient: ['#e8f3ff', '#cfe6fb', '#558ec1', '#2476af', '#2b6fb3'],
|
||||
waves: ['rgba(170, 215, 245, 0.91)', 'rgba(120, 185, 230, 0.93)', 'rgba(80, 155, 210, 0.95)'],
|
||||
},
|
||||
dark: {
|
||||
gradient: ['#0d1117', '#111d2b', '#14334d', '#17507a', '#1b6aa6'],
|
||||
waves: ['rgba(90, 165, 225, 0.9)', 'rgba(65, 140, 210, 0.88)', 'rgba(50, 120, 190, 0.86)'],
|
||||
},
|
||||
};
|
||||
|
||||
const getTheme = () => (document.documentElement.getAttribute('data-theme') === 'dark' ? 'dark' : 'light');
|
||||
|
||||
const getCoverageHeight = () => {
|
||||
if (servicesSection) {
|
||||
const coverage = servicesSection.offsetTop + servicesSection.offsetHeight * 0.5;
|
||||
return Math.min(coverage, main.scrollHeight);
|
||||
}
|
||||
if (heroSection) {
|
||||
return heroSection.offsetHeight;
|
||||
}
|
||||
return main.scrollHeight;
|
||||
};
|
||||
|
||||
const resize = () => {
|
||||
dpr = window.devicePixelRatio || 1;
|
||||
width = main.clientWidth;
|
||||
height = Math.max(1, Math.round(getCoverageHeight()));
|
||||
canvas.width = Math.floor(width * dpr);
|
||||
canvas.height = Math.floor(height * dpr);
|
||||
canvas.style.width = `${width}px`;
|
||||
canvas.style.height = `${height}px`;
|
||||
ctx.setTransform(dpr, 0, 0, dpr, 0, 0);
|
||||
};
|
||||
|
||||
const drawBackground = (palette) => {
|
||||
const gradient = ctx.createLinearGradient(0, 0, 0, height);
|
||||
gradient.addColorStop(0, palette.gradient[0]);
|
||||
gradient.addColorStop(0.35, palette.gradient[1]);
|
||||
gradient.addColorStop(0.55, palette.gradient[2]);
|
||||
gradient.addColorStop(0.75, palette.gradient[3]);
|
||||
gradient.addColorStop(1, palette.gradient[4]);
|
||||
ctx.fillStyle = gradient;
|
||||
ctx.fillRect(0, 0, width, height);
|
||||
};
|
||||
|
||||
const drawWave = (palette, wave, time, index) => {
|
||||
const yBase = height * wave.yRatio;
|
||||
const phase = time * wave.speed;
|
||||
const prevAlpha = ctx.globalAlpha;
|
||||
ctx.globalAlpha = wave.alpha ?? 1;
|
||||
ctx.beginPath();
|
||||
ctx.moveTo(0, height);
|
||||
for (let x = 0; x <= width; x += 18) {
|
||||
const theta = (x / wave.wavelength) * Math.PI * 2 + phase;
|
||||
const y = yBase + Math.sin(theta) * wave.amplitude;
|
||||
ctx.lineTo(x, y);
|
||||
}
|
||||
ctx.lineTo(width, height);
|
||||
ctx.closePath();
|
||||
ctx.fillStyle = palette.waves[index % palette.waves.length];
|
||||
ctx.fill();
|
||||
ctx.globalAlpha = prevAlpha;
|
||||
};
|
||||
|
||||
const render = (timestamp) => {
|
||||
const palette = palettes[getTheme()];
|
||||
const time = timestamp * 0.001;
|
||||
ctx.clearRect(0, 0, width, height);
|
||||
drawBackground(palette);
|
||||
waveDefs.forEach((wave, index) => drawWave(palette, wave, time, index));
|
||||
rafId = requestAnimationFrame(render);
|
||||
};
|
||||
|
||||
const stop = () => {
|
||||
if (rafId) {
|
||||
cancelAnimationFrame(rafId);
|
||||
rafId = null;
|
||||
}
|
||||
};
|
||||
|
||||
const drawStatic = () => {
|
||||
const palette = palettes[getTheme()];
|
||||
ctx.clearRect(0, 0, width, height);
|
||||
drawBackground(palette);
|
||||
waveDefs.forEach((wave, index) => drawWave(palette, wave, 0, index));
|
||||
};
|
||||
|
||||
const start = () => {
|
||||
stop();
|
||||
resize();
|
||||
if (prefersReduced.matches) {
|
||||
drawStatic();
|
||||
return;
|
||||
}
|
||||
rafId = requestAnimationFrame(render);
|
||||
};
|
||||
|
||||
const onResize = () => {
|
||||
resize();
|
||||
if (prefersReduced.matches) {
|
||||
drawStatic();
|
||||
}
|
||||
};
|
||||
|
||||
const themeObserver = new MutationObserver(() => {
|
||||
if (prefersReduced.matches) {
|
||||
drawStatic();
|
||||
}
|
||||
});
|
||||
|
||||
themeObserver.observe(document.documentElement, { attributes: true, attributeFilter: ['data-theme'] });
|
||||
prefersReduced.addEventListener('change', start);
|
||||
window.addEventListener('resize', onResize);
|
||||
window.addEventListener('load', onResize);
|
||||
|
||||
start();
|
||||
})();
|
||||
@@ -0,0 +1,40 @@
|
||||
(() => {
|
||||
const hero = document.querySelector('.hero-section');
|
||||
if (!hero) return;
|
||||
|
||||
const body = document.body;
|
||||
const desktopQuery = window.matchMedia('(min-width: 900px)');
|
||||
let hasScrolled = false;
|
||||
|
||||
const applyState = () => {
|
||||
if (!desktopQuery.matches) {
|
||||
body.classList.remove('home-hero-initial', 'home-hero-scrolled');
|
||||
return;
|
||||
}
|
||||
|
||||
if (hasScrolled || window.scrollY > 6) {
|
||||
body.classList.remove('home-hero-initial');
|
||||
body.classList.add('home-hero-scrolled');
|
||||
} else {
|
||||
body.classList.add('home-hero-initial');
|
||||
body.classList.remove('home-hero-scrolled');
|
||||
}
|
||||
};
|
||||
|
||||
const onScroll = () => {
|
||||
hasScrolled = window.scrollY > 6;
|
||||
applyState();
|
||||
};
|
||||
|
||||
const onResize = () => {
|
||||
if (!desktopQuery.matches) {
|
||||
hasScrolled = false;
|
||||
}
|
||||
applyState();
|
||||
};
|
||||
|
||||
applyState();
|
||||
window.addEventListener('scroll', onScroll, { passive: true });
|
||||
window.addEventListener('resize', onResize);
|
||||
desktopQuery.addEventListener('change', onResize);
|
||||
})();
|
||||
+1
-1
@@ -1,5 +1,5 @@
|
||||
document.addEventListener('DOMContentLoaded', function() {
|
||||
const container = document.querySelector('.hero-description');
|
||||
const container = document.querySelector('.stream-text') || document.querySelector('.hero-description');
|
||||
if (!container) return;
|
||||
|
||||
const cnText = container.getAttribute('data-cn') || '';
|
||||
|
||||
Reference in New Issue
Block a user