feat: add wallpaper slideshow
This commit is contained in:
parent
f0b448fd1f
commit
973ba6d772
@ -2,7 +2,7 @@ import titleImg from "@/assets/images/title-img.svg";
|
|||||||
import PageMetadata from "@/components/containers/PageMetadata";
|
import PageMetadata from "@/components/containers/PageMetadata";
|
||||||
import { cn } from "@/utility/utils";
|
import { cn } from "@/utility/utils";
|
||||||
import dayjs from "dayjs";
|
import dayjs from "dayjs";
|
||||||
import { ComponentProps, useEffect, useMemo, useState } from "react";
|
import { ComponentProps, useEffect, useMemo, useRef, useState } from "react";
|
||||||
import { Link } from "react-router-dom";
|
import { Link } from "react-router-dom";
|
||||||
import { icons } from "./icons";
|
import { icons } from "./icons";
|
||||||
import { useQuery } from "react-query";
|
import { useQuery } from "react-query";
|
||||||
@ -20,6 +20,8 @@ const HomePage = () => {
|
|||||||
};
|
};
|
||||||
|
|
||||||
const BackgroundSlideshow = () => {
|
const BackgroundSlideshow = () => {
|
||||||
|
const slideshowTimer = 10000;
|
||||||
|
|
||||||
const { data: wallpapers } = useQuery({
|
const { data: wallpapers } = useQuery({
|
||||||
queryKey: ["wallpapers"],
|
queryKey: ["wallpapers"],
|
||||||
queryFn: async () => {
|
queryFn: async () => {
|
||||||
@ -36,6 +38,23 @@ const BackgroundSlideshow = () => {
|
|||||||
refetchOnWindowFocus: false,
|
refetchOnWindowFocus: false,
|
||||||
});
|
});
|
||||||
|
|
||||||
|
const [currentIdx, setCurrentIdx] = useState(0);
|
||||||
|
const curImg = wallpapers?.[currentIdx];
|
||||||
|
|
||||||
|
useEffect(() => {
|
||||||
|
if (!wallpapers) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
const intv = setInterval(() => {
|
||||||
|
setCurrentIdx((cur) => (cur + 1) % wallpapers.length);
|
||||||
|
}, slideshowTimer);
|
||||||
|
|
||||||
|
return () => {
|
||||||
|
clearInterval(intv);
|
||||||
|
};
|
||||||
|
}, [wallpapers, slideshowTimer]);
|
||||||
|
|
||||||
return (
|
return (
|
||||||
<>
|
<>
|
||||||
<img
|
<img
|
||||||
@ -44,9 +63,7 @@ const BackgroundSlideshow = () => {
|
|||||||
className="h-4 md:h-8 absolute top-6 left-6 md:top-8 md:left-8 lg:left-[5%] lg:top-[5%] z-[5]"
|
className="h-4 md:h-8 absolute top-6 left-6 md:top-8 md:left-8 lg:left-[5%] lg:top-[5%] z-[5]"
|
||||||
/>
|
/>
|
||||||
|
|
||||||
{wallpapers && wallpapers?.length > 0 ? (
|
{curImg ? <BackgroundImage src={curImg} /> : null}
|
||||||
<BackgroundImage src={wallpapers[0]} />
|
|
||||||
) : null}
|
|
||||||
</>
|
</>
|
||||||
);
|
);
|
||||||
};
|
};
|
||||||
@ -56,22 +73,42 @@ type BackgroundImageProps = {
|
|||||||
};
|
};
|
||||||
|
|
||||||
const BackgroundImage = ({ src }: BackgroundImageProps) => {
|
const BackgroundImage = ({ src }: BackgroundImageProps) => {
|
||||||
|
const lastChangeRef = useRef<Date | null>(null);
|
||||||
const [isLoaded, setLoaded] = useState(false);
|
const [isLoaded, setLoaded] = useState(false);
|
||||||
|
const [imgSrc, setImgSrc] = useState("");
|
||||||
|
|
||||||
|
useEffect(() => {
|
||||||
|
setLoaded(false);
|
||||||
|
lastChangeRef.current = new Date();
|
||||||
|
}, [src]);
|
||||||
|
|
||||||
|
const onLoaded = () => {
|
||||||
|
setLoaded(true);
|
||||||
|
setImgSrc(src);
|
||||||
|
};
|
||||||
|
|
||||||
return (
|
return (
|
||||||
<>
|
<>
|
||||||
<img
|
<img
|
||||||
src={src}
|
src={src}
|
||||||
alt="img"
|
alt="img"
|
||||||
className="hidden"
|
className="hidden"
|
||||||
onLoad={() => setTimeout(() => setLoaded(true), 100)}
|
onLoad={() => {
|
||||||
|
let timer = lastChangeRef.current
|
||||||
|
? new Date().getMilliseconds() -
|
||||||
|
lastChangeRef.current.getMilliseconds()
|
||||||
|
: 100;
|
||||||
|
timer = Math.max(500, Math.min(timer, 1000));
|
||||||
|
setTimeout(onLoaded, timer);
|
||||||
|
}}
|
||||||
/>
|
/>
|
||||||
|
|
||||||
<div
|
<div
|
||||||
className={cn(
|
className={cn(
|
||||||
"absolute w-[100vw] bg-center bg-cover inset-0 opacity-0 transition-opacity duration-500",
|
"absolute w-[100vw] bg-center bg-cover inset-0 transition-opacity duration-500",
|
||||||
isLoaded ? "opacity-100" : ""
|
isLoaded ? "opacity-100" : "opacity-0"
|
||||||
)}
|
)}
|
||||||
style={{ backgroundImage: `url('${src}')` }}
|
style={{ backgroundImage: `url('${imgSrc}')` }}
|
||||||
></div>
|
></div>
|
||||||
</>
|
</>
|
||||||
);
|
);
|
||||||
|
Loading…
x
Reference in New Issue
Block a user