From ed09f5bd531b067ab26c20f65100f8dcbe442616 Mon Sep 17 00:00:00 2001 From: tangly1024 Date: Tue, 11 Jul 2023 20:53:54 +0800 Subject: [PATCH] fix plog aninmation --- themes/plog/components/BlogListPage.js | 26 ++++++++++++++++++++++++-- themes/plog/components/BlogPost.js | 11 ++++++++++- 2 files changed, 34 insertions(+), 3 deletions(-) diff --git a/themes/plog/components/BlogListPage.js b/themes/plog/components/BlogListPage.js index f8455ae7..1ac57222 100644 --- a/themes/plog/components/BlogListPage.js +++ b/themes/plog/components/BlogListPage.js @@ -4,6 +4,7 @@ import { useGlobal } from '@/lib/global' import { useRouter } from 'next/router' import Link from 'next/link' import BlogPost from './BlogPost' +import { useEffect, useRef } from 'react' export const BlogListPage = props => { const { page = 1, posts, postCount } = props @@ -16,12 +17,33 @@ export const BlogListPage = props => { const showNext = page < totalPage const pagePrefix = router.asPath.split('?')[0].replace(/\/page\/[1-9]\d*/, '').replace(/\/$/, '') + const blogPostRefs = useRef([]) + + useEffect(() => { + const observer = new IntersectionObserver(entries => { + entries.forEach(entry => { + if (entry.isIntersecting) { + entry.target.classList.toggle('visible') + } + }) + }, { + threshold: 0.1 // 调整阈值以达到最佳效果 + }) + + blogPostRefs.current.forEach(ref => { + observer.observe(ref) + }) + + return () => { + observer.disconnect() + } + }, []) return (
- {posts?.map(post => ( - + {posts?.map((post, index) => ( + blogPostRefs.current.push(el)}/> ))}
diff --git a/themes/plog/components/BlogPost.js b/themes/plog/components/BlogPost.js index 4f7c9bce..65eacc4c 100644 --- a/themes/plog/components/BlogPost.js +++ b/themes/plog/components/BlogPost.js @@ -1,6 +1,7 @@ import { compressImage } from '@/lib/notion/mapImage' import Link from 'next/link' import { usePlogGlobal } from '..' +import { isMobile } from '@/lib/utils' /** * 博客照片卡牌 @@ -8,16 +9,24 @@ import { usePlogGlobal } from '..' * @returns */ const BlogPost = (props) => { - const { post, siteInfo } = props + const { post, index, siteInfo } = props const pageThumbnail = compressImage(post?.pageCoverThumbnail || siteInfo?.pageCover, 800, 80) const { setModalContent, setShowModal } = usePlogGlobal() const handleClick = () => { setShowModal(true) setModalContent(post) } + + // 实现动画 一个接一个出现 + let delay = index * 100 + if (isMobile()) { + delay = 0 + } + return (