๐ค ์์งlog ๐ค
[REACT] Carousel๋ก ๋ฐฐ๋ ์ฌ๋ผ์ด๋ ๊ตฌํํ๊ธฐ ๋ณธ๋ฌธ
๋ผ์ด๋ธ๋ฌ๋ฆฌ๋ฅผ ์ฌ์ฉํ์ง ์๊ณ { useState ์ useEffect } ๋ฅผ ์ฌ์ฉํด์ ์ง์ ๊ตฌํํด๋ณด๊ธฐ๋ก ํ์๋ค ! ๐ฅ
๊ทธ์ ์๋ react-bootstrap์ผ๋ก๋ง ํด๋ดค๋ํฐ๋ผ,, ์กฐ๊ธ ๊ฑฑ์ ํ๋๋ฐ
์ญ์๋ ์ด๋ ค์ ์ง๋ง ์ ๋ณด๋ ๋ง์๊ณ , ์ด๊ฒ ์ ๊ฒ ์๋ก ์๋ํด๋ณด๋ ์ฌ๋ฏธ๋์์๋ค -๐
๐ ๊ตฌํ ์ฝ๋
import { useEffect, useState } from "react";
import { Link } from "react-router-dom";
import styled from "styled-components";
import { SLIDE_DATA } from "datas/slide";
const Banner = () => {
const [currentSlide, setCurrentSlide] = useState(0);
const handleSlide = (id: number) => setCurrentSlide(id);
useEffect(() => {
setTimeout(() => {
if (currentSlide >= SLIDE_DATA.length - 1) {
return setCurrentSlide(0);
}
return setCurrentSlide((prev) => prev + 1);
}, 3000);
}, [currentSlide]);
return (
<Container>
<SliderContainer index={currentSlide}>
{SLIDE_DATA.map(({ id, title, img }) => (
<img src={img} alt="" />
<Title>
<p>{title}</p>
</Title>
</CustomLink>
))}
</SliderContainer>
<ButtonWrap>
{SLIDE_DATA.map(({ id }) => (
<Button
key={id}
onClick={() => handleSlide(id)}
isActive={id === currentSlide}
/>
))}
</ButtonWrap>
</Container>
);
};
const Container = styled.div`
margin: 0 auto;
width: 100%;
overflow: hidden;
`;
const SliderContainer = styled.div`
margin-top: 10px;
display: flex;
transition: all 0.7s ease-in-out;
transform: ${({ index }: { index?: number }) =>
`translateX(calc(-425px * ${index}))`};
`;
const CustomLink = styled(Link)`
border: 1px solid #ebebeb;
border-radius: 10px;
margin: 0px 6.5px;
cursor: pointer;
color: #000;
text-decoration: none;
img {
border-radius: 10px 10px 0 0;
margin: 0 auto;
}
`;
const Title = styled.div`
width: 100%;
height: 50px;
margin: 0;
font-size: 14px;
font-weight: 700;
p {
width: 270px;
overflow: hidden;
text-overflow: ellipsis;
display: -webkit-box;
-webkit-line-clamp: 1;
-webkit-box-orient: vertical;
}
`;
const ButtonWrap = styled.div`
display: flex;
justify-content: center;
margin: 10px auto;
gap: 7px;
`;
const Button = styled.button`
height: 5px;
width: 5px;
border-radius: 50%;
border: none;
padding: 0;
cursor: pointer;
background-color: ${({ isActive }: { isActive: boolean }) =>
isActive ? "#f50057" : "lightgray"};
`;
export default Banner;
728x90
'๐ React' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
[REACT] ๋ฌดํ ์คํฌ๋กค (Infinite Scroll) ๊ตฌํํ๊ธฐ (0) | 2023.02.14 |
---|---|
[REACT] CSS ๋ฐ์ํ ๋ ์ด์์ ๋๋น๋ฅผ ๋๋๋ ๊ธฐ์ค ์ ํ๊ธฐ (0) | 2023.02.10 |
[REACT] Swiper ์ฌ์ฉํ์ฌ ํฐ์น์ฌ๋ผ์ด๋ ๊ตฌํํ๊ธฐ (1) | 2023.02.06 |
[REACT] ์ด๋ฒคํธ ํธ๋ค๋ฌ ( Event handler ) (0) | 2022.10.07 |
[REACT] ๋ฐ๋ณต ( Iteration ) (0) | 2022.10.07 |