"use client"; import React, { useState, useEffect, useRef, forwardRef, useImperativeHandle, } from "react"; import { Mask, Image } from "antd-mobile"; import { FontAwesomeIcon } from "@fortawesome/react-fontawesome"; import { faAngleLeft, faAngleRight } from "@fortawesome/free-solid-svg-icons"; import { createRoot } from "react-dom/client"; const maskDomBox = window ? document?.getElementById("maskDomBox") : null; const root = createRoot(maskDomBox); function ImagesMask({}, ref) { const [currentIndex, setCurrentIndex] = useState(0); const [visible, setVisible] = useState(false); const [images, setImages] = useState([]); const scrollRef = useRef(null); useEffect(() => { root.render(
); // root.render(
xxxx
); }, [visible]); useEffect(() => { if (scrollRef.current) { setTimeout(() => { scrollRef.current.scrollLeft = 414 * currentIndex; }, 200); } }, [currentIndex]); useImperativeHandle(ref, () => ({ show: (arr, index) => { setCurrentIndex(index); setImages(arr); setVisible(old=>{ console.log(arr, index,old) return !old }); // Mask.show // const maskDomBox = document.getElementById("maskDomBox"); }, close: () => setVisible(false), })); return <>; } const ImagesMaskContaint = forwardRef( ({ visible, images, currentIndex,setCurrentIndex,setVisible,setImages }, ref) => { const [distance, setDistance] = useState(0); const getDistance = (e) => { e.stopPropagation(); const distance = ref.current.scrollLeft; setDistance(distance); }; const handleMoveImages = (e, index) => { e.stopPropagation(); // console.log("distance",distance) if ( distance > window.innerWidth * currentIndex && distance < window.innerWidth + window.innerWidth * currentIndex ) { setCurrentIndex(Math.ceil(distance / 414)); } else if (distance < window.innerWidth * currentIndex) { setCurrentIndex(Math.ceil(distance / 414) - 1); } }; return (
{currentIndex + 1}/{images.length}
{images.map((item, index) => { return ( handleMoveImages(e, index)} onTouchMove={getDistance} onTouchEnd={(e) => handleMoveImages(e, index)} onClick={() => { setVisible(false), setImages([]), setCurrentIndex(0); }} /> ); })}
{ setCurrentIndex((old) => (old > 0 ? old - 1 : old)); }} >
{ setCurrentIndex((old) => old < images.length - 1 ? old + 1 : old ); }} >
); } ); export default forwardRef(ImagesMask);