"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"; function ImagesMask({}, ref) { const [currentIndex, setCurrentIndex] = useState(0); const [visible, setVisible] = useState(false); const [images, setImages] = useState([]); const [distance, setDistance] = useState(0); const scrollRef = useRef(null); useEffect(() => { if (scrollRef.current) { setTimeout(() => { scrollRef.current.scrollLeft = 414 * currentIndex; }, 200); } }, [currentIndex]); useImperativeHandle(ref, () => ({ show: (arr, index) => { setCurrentIndex(index); setImages(arr); setVisible(true); // Mask.show // const maskDomBox = document.getElementById("maskDomBox"); }, close: () => setVisible(false), })); const getDistance = (e) => { e.stopPropagation(); const distance = scrollRef.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);