"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"; import { useRouter } from "next/navigation"; import baseRequest from "@/utils/baseRequest"; const root = createRoot(document?.getElementById("maskDomBox")); function ImagesMask({}, ref) { const [visible, setVisible] = useState(false); const [images, setImages] = useState([]); const [data, setData] = useState(null); const scrollRef = useRef(null); const defaultIndex = useRef(null); const router = useRouter(); useEffect(() => { root.render(
); // root.render(
xxxx
); }, [visible]); useImperativeHandle(ref, () => ({ show: (arr, index, data) => { defaultIndex.current = index; setImages(arr); setData(data); 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, setVisible, setImages, router, data, defaultIndex = 0 }, ref ) => { const [currentIndex, setCurrentIndex] = useState(0); const [distance, setDistance] = useState(0); const [direction, setDirection] = useState("left"); const [initialX, setInitialX] = useState(0); const [currentX, setCurrentX] = useState(0); const [xOffset, setXOffset] = useState(0); const [active, setActive] = useState(false); const base = baseRequest(); const getDistance = (e) => { e.stopPropagation(); const distance = ref.current.scrollLeft; setDistance(distance); }; useEffect(() => { setCurrentIndex(defaultIndex); setTimeout(() => { // console.log("ref.current", ref.current); if (ref.current) { ref.current.style.transform = `translateX(${ -window.innerWidth * defaultIndex }px)`; } }, 100); }, [defaultIndex]); useEffect(() => { console.log("ref.current", ref.current); if (ref.current) { ref.current.style.transform = `translateX(${ -window.innerWidth * currentIndex }px)`; } }, [currentIndex]); const test1 = (e) => { console.log( e.touches[0].clientX - (!currentIndex ? 0 : xOffset), e.touches[0].clientX, xOffset ); setInitialX(e.touches[0].clientX - (!currentIndex ? 0 : xOffset)); if (e.touches.length === 1) { // 单点触摸 setActive(true); } }; const test2 = (e) => { if (active) { // e.preventDefault(); let X = e.touches[0].clientX - window.innerWidth * (currentIndex ? 1 : currentIndex + 1); console.log("-----", e.touches[0].clientX, initialX, currentIndex); setCurrentX((old) => { setDirection(X > old ? "right" : "left"); return X; }); setXOffset(X); ref.current.style.transform = "translateX(" + X + "px)"; } }; const test3 = (index) => { console.log("index", index); // setInitialX(currentX); setActive(false); let cxOffset = window.innerWidth; console.log(direction, index, images.length - 1); if (direction == "left") { if (index < images.length - 1) { setCurrentIndex(index + 1); console.log(index + 1); } else { ref.current.style.transform = `translateX(${-cxOffset * index}px)`; } } else { if (index > 0) { setCurrentIndex(index - 1); console.log(index - 1); } else { ref.current.style.transform = `translateX(${-cxOffset * index}px)`; } } setInitialX(currentX); // setInitialX(-cxOffset * (index + 1)); setXOffset(-cxOffset * (index || 1)); }; return (
{ setVisible(false), setImages([]), setCurrentIndex(0); // root.unmount(); }} >
{currentIndex + 1}/{images.length}
{ setVisible(false), setImages([]), setCurrentIndex(0); // root.unmount(); }} >
test3(currentIndex)} > {images.map((item, index) => { return (
{item.type == "hid" && (
{ router.push( "/webView/" + encodeURIComponent( "/zone/pay/" + data?.zid + "/h5_zone_moment/" + data?.id + "?base=" + encodeURIComponent(JSON.stringify(base)) ) ); }} > 此内容暂未解锁,立即解锁
)}
); })}
{ setCurrentIndex((old) => (old > 0 ? old - 1 : old)); }} >
{ setCurrentIndex((old) => old < images.length - 1 ? old + 1 : old ); setInitialX((currentIndex || 1) * window.innerWidth * 2); }} >
); } ); export default forwardRef(ImagesMask);