diff --git a/lib/utils.js b/lib/utils.js index 59c760e9..a97c114a 100644 --- a/lib/utils.js +++ b/lib/utils.js @@ -133,6 +133,8 @@ export const isMobile = () => { if (!isBrowser()) { return isMobile } + + // 这个判断会引发 TypeError: navigator.userAgentData.mobile is undefined 问题,导致博客无法正常工作 // if (!isMobile && navigator.userAgentData.mobile) { // isMobile = true // } diff --git a/pages/_app.js b/pages/_app.js index 41feba71..e6749281 100644 --- a/pages/_app.js +++ b/pages/_app.js @@ -24,6 +24,7 @@ import smoothscroll from 'smoothscroll-polyfill' import AOS from 'aos' import 'aos/dist/aos.css' // You can also use for styles +import { isMobile } from '@/lib/utils' const Ackee = dynamic(() => import('@/components/Ackee'), { ssr: false }) const Gtag = dynamic(() => import('@/components/Gtag'), { ssr: false }) @@ -57,7 +58,9 @@ const MyApp = ({ Component, pageProps }) => { useEffect(() => { AOS.init() - smoothscroll.polyfill() + if (isMobile()) { + smoothscroll.polyfill() + } }, []) return ( diff --git a/themes/example/components/BlogListScroll.js b/themes/example/components/BlogListScroll.js index d70e4bc5..b3d7f3f0 100644 --- a/themes/example/components/BlogListScroll.js +++ b/themes/example/components/BlogListScroll.js @@ -78,5 +78,5 @@ export const BlogListScroll = props => { - ); + ) } diff --git a/themes/hexo/LayoutBase.js b/themes/hexo/LayoutBase.js index 701daa71..066c4404 100644 --- a/themes/hexo/LayoutBase.js +++ b/themes/hexo/LayoutBase.js @@ -1,6 +1,6 @@ import CommonHead from '@/components/CommonHead' -import { useEffect, useState } from 'react' - +import { useCallback, useEffect, useState } from 'react' +import throttle from 'lodash.throttle' import Footer from './components/Footer' import JumpToTopButton from './components/JumpToTopButton' import SideRight from './components/SideRight' @@ -42,23 +42,20 @@ const LayoutBase = props => { > ) const { onLoading } = useGlobal() + const throttleMs = 200 + const scrollListener = useCallback(throttle(() => { + const targetRef = document.getElementById('wrapper') + const clientHeight = targetRef?.clientHeight + const scrollY = window.pageYOffset + const fullHeight = clientHeight - window.outerHeight + let per = parseFloat(((scrollY / fullHeight) * 100).toFixed(0)) + if (per > 100) per = 100 + const shouldShow = scrollY > 100 && per > 0 - const scrollListener = () => { - requestAnimationFrame(() => { - const targetRef = document.getElementById('wrapper') - const clientHeight = targetRef?.clientHeight - const scrollY = window.pageYOffset - const fullHeight = clientHeight - window.outerHeight - let per = parseFloat(((scrollY / fullHeight) * 100).toFixed(0)) - if (per > 100) per = 100 - const shouldShow = scrollY > 100 && per > 0 - - if (shouldShow !== showFloatButton) { - switchShow(shouldShow) - } - // changePercent(per) - }) - } + if (shouldShow !== showFloatButton) { + switchShow(shouldShow) + } + }, throttleMs)) useEffect(() => { document.addEventListener('scroll', scrollListener) return () => document.removeEventListener('scroll', scrollListener) diff --git a/themes/hexo/components/Announcement.js b/themes/hexo/components/Announcement.js index 391fd3a8..677cb467 100644 --- a/themes/hexo/components/Announcement.js +++ b/themes/hexo/components/Announcement.js @@ -7,7 +7,7 @@ const Announcement = ({ post, className }) => { const { locale } = useGlobal() if (post?.blockMap) { return
- {post.summary} -
- )} - - {/* 搜索结果 */} - {post.results && ( -- {post.results.map(r => ( - {r} - ))} -
- )} - - {showPreview && ( -+ {post.summary} +
+ )} + + {/* 搜索结果 */} + {post.results && ( ++ {post.results.map(r => ( + {r} + ))} +
+ )} + {/* 预览 */} + {showPreview && ( +