优化首页样式
This commit is contained in:
@@ -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);
|
||||
})();
|
||||
Reference in New Issue
Block a user