mirror of
https://github.com/bitsocialnet/5chan.git
synced 2026-08-03 07:41:04 +02:00
randomized ImageBanner
This commit is contained in:
Binary file not shown.
|
After Width: | Height: | Size: 41 KiB |
@@ -7,14 +7,19 @@ const ImageBanner = () => {
|
|||||||
const location = useLocation();
|
const location = useLocation();
|
||||||
|
|
||||||
useEffect(() => {
|
useEffect(() => {
|
||||||
|
const countImages = async () => {
|
||||||
|
const images = await importAll(require.context('../../public/assets/banners', false, /\.(png|jpe?g|svg)$/));
|
||||||
|
setCurrentImage(Math.floor(Math.random() * images.length) + 1);
|
||||||
|
};
|
||||||
|
|
||||||
setIsLoaded(false);
|
setIsLoaded(false);
|
||||||
setCurrentImage(Math.floor(Math.random() * 13) + 1);
|
countImages();
|
||||||
}, [location.key]);
|
}, [location.key]);
|
||||||
|
|
||||||
useEffect(() => {
|
useEffect(() => {
|
||||||
setIsLoaded(false);
|
setIsLoaded(false);
|
||||||
const img = new Image();
|
const img = new Image();
|
||||||
img.src = `/assets/banners/banner-${currentImage}.jpg`;
|
img.src = `${process.env.PUBLIC_URL}/assets/banners/banner-${currentImage}.jpg`;
|
||||||
img.onload = () => {
|
img.onload = () => {
|
||||||
setIsLoaded(true);
|
setIsLoaded(true);
|
||||||
};
|
};
|
||||||
@@ -22,9 +27,14 @@ const ImageBanner = () => {
|
|||||||
|
|
||||||
return (
|
return (
|
||||||
<>
|
<>
|
||||||
{isLoaded && <img id="banner-img" src={`/assets/banners/banner-${currentImage}.jpg`} alt="banner" />}
|
{isLoaded && <img id="banner-img" src={`${process.env.PUBLIC_URL}/assets/banners/banner-${currentImage}.jpg`} alt="banner" />}
|
||||||
</>
|
</>
|
||||||
);
|
);
|
||||||
};
|
};
|
||||||
|
|
||||||
|
// Helper function to import all images from a directory
|
||||||
|
function importAll(r) {
|
||||||
|
return r.keys().map(r);
|
||||||
|
}
|
||||||
|
|
||||||
export default ImageBanner;
|
export default ImageBanner;
|
||||||
|
|||||||
Reference in New Issue
Block a user