import { useState } from 'react';
import styled from 'styled-components';
export const Carousel3D = () => {
const [rotation, setRotation] = useState(0);
const cells = Array.from({ length: 9 }, (_, i) => i + 1);
const rotateCarousel = (direction) => {
setRotation(rotation + (direction === 'next' ? -40 : 40));
};
return (
<>
<Scene>
<Carousel style={{ transform: `translateZ(-288px) rotateY(${rotation}deg)` }}>
{cells.map((cell, index) => (
<Cell key={index} style={{ '--i': index }}>
{cell}
</Cell>
))}
</Carousel>
</Scene>
<CarouselOptions>
<button onClick={() => rotateCarousel('prev')}>←</button>
<button onClick={() => rotateCarousel('next')}>→</button>
</CarouselOptions>
</>
);
};
const Scene = styled.div`
border: 1px solid #ccc;
margin: 40px 0;
position: relative;
width: 210px;
height: 140px;
margin: 80px auto;
perspective: 1000px;
`;
const Carousel = styled.div`
width: 100%;
height: 100%;
position: absolute;
transform-style: preserve-3d;
transition: transform 1s;
`;
const Cell = styled.div`
position: absolute;
width: 190px;
height: 120px;
left: 10px;
top: 10px;
border: 2px solid black;
line-height: 116px;
font-size: 80px;
font-weight: bold;
color: white;
text-align: center;
transition: transform 1s, opacity 1s;
background: ${({ style }) => `hsla(${style['--i'] * 40}, 100%, 50%, 0.8)`};
transform: ${({ style }) => `rotateY(${style['--i'] * 40}deg) translateZ(288px)`};
`;
const CarouselOptions = styled.div`
text-align: center;
position: relative;
z-index: 2;
background: hsla(0, 0%, 100%, 0.8);
button {
margin: 10px;
padding: 5px 10px;
font-size: 16px;
}
`;
결과 화면