Files
qygw-template/js/utils.js

50 lines
1.3 KiB
JavaScript
Raw Permalink Normal View History

2025-01-03 18:02:01 +08:00
const toUrl = (url, isNew = false) => {
isNew ?
window.open(url) :
window.location.href = url;
}
window.addEventListener('load', function() {
const images = document.querySelectorAll('.in-scale-image');
const options = {
root: null, // 使用 viewport 作为根
threshold: 0 // 当 10% 的元素可见时触发
};
const callback = (entries) => {
entries.forEach(entry => {
if (entry.isIntersecting) {
entry.target.classList.add('img-scale-in');
}
});
};
const observer = new IntersectionObserver(callback, options);
images.forEach(img => {
observer.observe(img);
});
});
/**
* 依赖于gsapScrollTriggerScrollToPluginScrollSmoother
*/
window.addEventListener('load', function() {
gsap.registerPlugin(ScrollTrigger, ScrollToPlugin, ScrollSmoother);
ScrollSmoother.create({
smooth: 1,
effects: true,
smoothTouch: 0.1,
normalizeScroll: true,
});
const navToValue = new URLSearchParams(window.location.search).get('navTo');
scrollToElement(navToValue);
});
const scrollToElement = (id) => {
gsap.to(window, {
duration: 0.5,
scrollTo:{y: `#${id}`},
ease: "power2",
});
}