프로그래밍/ReactJS

react framer-motion slide구현

타코코딩 2023. 11. 23. 04:40
import { AnimatePresence, motion } from "framer-motion";
import React, { useRef, useState } from "react";
import styled from "styled-components";

const Wrapper = styled.div`
  position: relative;
`;
const Row = styled(motion.div)`
  display: grid;
  grid-template-columns: repeat(6, 1fr);
  gap: 10px;
  position: absolute;
  width: 100%;
`;

const rowVars = {
  //window.innerWidth를 사용 이게 픽셀로 주는것보다 더 좋을듯
  start: {
    x: window.innerWidth,
  },
  end: {
    x: 0,
  },
  exit: {
    x: -window.innerWidth,
  },
};

const Box = styled(motion.div)`
  background-color: tomato;
  height: 200px;
  color: tomato;
  font-size: 50px;
  background-image: url(${(props) => props.bgphoto});
  background-size: cover;
  background-position: center center;
`;

const offset = 6;
const Detail_reco = () => {
  const [index, setIndex] = useState(0);
  const [leaving, setLeaving] = useState(false);
  const toggleLeaving = () => setLeaving((leaving) => !leaving);
  const increaseindex = () => {
    toggleLeaving();
    const totalMovies = 12;
    const maxIndex = Math.ceil(totalMovies / 6) - 1;
    setIndex((prev) => (prev == maxIndex ? 0 : prev + 1));
  };
  return (
    <Wrapper onClick={increaseindex}>
      <AnimatePresence initial={false} onExitComplete={toggleLeaving}>
        <Row
          variants={rowVars}
          initial="start"
          animate="end"
          exit="exit"
          transition={{ type: "tween", duration: 1 }}
          key={index}>
          {[1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12]
            .slice(offset * index, offset * index + offset)
            .map((movie: any, i: any) => (
              <Box key={i}></Box>
            ))}
        </Row>
      </AnimatePresence>
    </Wrapper>
  );
};

export default Detail_reco;

'프로그래밍 > ReactJS' 카테고리의 다른 글

스와이프 리액트에서 반응형 세팅  (0) 2024.01.09
useReducer 사용하기  (0) 2023.12.28
framer-motion react,typescript 환경 오류 해  (0) 2023.11.22
toggle 버튼 만들기 로직  (1) 2023.11.18
svg animation  (0) 2023.11.18