适配滚动加载

This commit is contained in:
tangly
2022-11-13 11:18:49 +08:00
parent c2b5072979
commit 030885010b
4 changed files with 45 additions and 11 deletions

View File

@@ -1,13 +1,14 @@
import BLOG from '@/blog.config' import BLOG from '@/blog.config'
import { useGlobal } from '@/lib/global' import { useGlobal } from '@/lib/global'
import Link from 'next/link' import Link from 'next/link'
import { useState } from 'react' import React from 'react'
import throttle from 'lodash.throttle'
export const BlogListScroll = props => { export const BlogListScroll = props => {
const { posts } = props const { posts } = props
const { locale } = useGlobal() const { locale } = useGlobal()
const [page, updatePage] = useState(1) const [page, updatePage] = React.useState(1)
let hasMore = false let hasMore = false
const postsToShow = posts const postsToShow = posts
@@ -23,7 +24,26 @@ export const BlogListScroll = props => {
updatePage(page + 1) updatePage(page + 1)
} }
return <div className="w-full md:pr-12 mb-12"> const targetRef = React.useRef(null)
// 监听滚动自动分页加载
const scrollTrigger = React.useCallback(throttle(() => {
const scrollS = window.scrollY + window.outerHeight
const clientHeight = targetRef ? (targetRef.current ? (targetRef.current.clientHeight) : 0) : 0
if (scrollS > clientHeight + 100) {
handleGetMore()
}
}, 500))
React.useEffect(() => {
window.addEventListener('scroll', scrollTrigger)
return () => {
window.removeEventListener('scroll', scrollTrigger)
}
})
return <div className="w-full md:pr-12 mb-12" ref={targetRef}>
{postsToShow.map(p => ( {postsToShow.map(p => (
<article key={p.id} className="mb-12" > <article key={p.id} className="mb-12" >
<h2 className="mb-4"> <h2 className="mb-4">

View File

@@ -1,8 +1,10 @@
import BLOG from '@/blog.config'
import BlogListPage from './components/BlogListPage' import BlogListPage from './components/BlogListPage'
import BlogListScroll from './components/BlogListScroll'
import LayoutBase from './LayoutBase' import LayoutBase from './LayoutBase'
export const LayoutIndex = (props) => { export const LayoutIndex = (props) => {
return <LayoutBase {...props}> return <LayoutBase {...props}>
<BlogListPage {...props} /> {BLOG.POST_LIST_STYLE === 'page' ? <BlogListPage {...props} /> : <BlogListScroll {...props}/>}
</LayoutBase> </LayoutBase>
} }

View File

@@ -1,9 +1,9 @@
import BLOG from '@/blog.config' import BLOG from '@/blog.config'
import { useEffect, useState } from 'react' import React from 'react'
import BlogCard from './BlogCard' import BlogCard from './BlogCard'
import BlogPostListEmpty from './BlogListEmpty' import BlogPostListEmpty from './BlogListEmpty'
import { useGlobal } from '@/lib/global' import { useGlobal } from '@/lib/global'
import throttle from 'lodash.throttle'
/** /**
* 文章列表分页表格 * 文章列表分页表格
* @param page 当前页 * @param page 当前页
@@ -14,7 +14,7 @@ import { useGlobal } from '@/lib/global'
*/ */
const BlogListScroll = props => { const BlogListScroll = props => {
const { posts = [] } = props const { posts = [] } = props
const [colCount, changeCol] = useState(1) const [colCount, changeCol] = React.useState(1)
const { locale } = useGlobal() const { locale } = useGlobal()
function updateCol() { function updateCol() {
@@ -26,8 +26,9 @@ const BlogListScroll = props => {
changeCol(1) changeCol(1)
} }
} }
const targetRef = React.useRef(null)
const [page, updatePage] = useState(1) const [page, updatePage] = React.useState(1)
let hasMore = false let hasMore = false
const postsToShow = posts const postsToShow = posts
@@ -43,11 +44,23 @@ const BlogListScroll = props => {
updatePage(page + 1) updatePage(page + 1)
} }
useEffect(() => { // 监听滚动自动分页加载
const scrollTrigger = React.useCallback(throttle(() => {
const scrollS = window.scrollY + window.outerHeight
const clientHeight = targetRef ? (targetRef.current ? (targetRef.current.clientHeight) : 0) : 0
if (scrollS > clientHeight + 100) {
handleGetMore()
}
}, 500))
React.useEffect(() => {
updateCol() updateCol()
window.addEventListener('scroll', scrollTrigger)
window.addEventListener('resize', updateCol) window.addEventListener('resize', updateCol)
return () => { return () => {
window.removeEventListener('resize', updateCol) window.removeEventListener('resize', updateCol)
window.removeEventListener('scroll', scrollTrigger)
} }
}) })
@@ -55,7 +68,7 @@ const BlogListScroll = props => {
return <BlogPostListEmpty /> return <BlogPostListEmpty />
} else { } else {
return ( return (
<div id="container"> <div id="container" ref={targetRef} >
{/* 文章列表 */} {/* 文章列表 */}
<div style={{ columnCount: colCount }}> <div style={{ columnCount: colCount }}>
{postsToShow?.map(post => ( {postsToShow?.map(post => (

View File

@@ -1,4 +1,3 @@
import BLOG from '@/blog.config'
import Link from 'next/link' import Link from 'next/link'
import { useRouter } from 'next/router' import { useRouter } from 'next/router'
import { useGlobal } from '@/lib/global' import { useGlobal } from '@/lib/global'