๋ฐ์ํ
๐ณ React Slick
๋ฆฌ์กํธ ํ๋ก์ ํธ์์ carousel์ ์ฝ๊ฒ ๊ตฌํํ๋๋ก ๋์์ฃผ๋ ๋ชจ๋
๐๐ป React Slick ํํ์ด์ง
๐๐ป ์ฌ๋ผ์ด๋ ์ข ๋ฅ
1. ๋ค์๊ณผ ๊ฐ์ ๋ช ๋ น์ด๋ฅผ ํฐ๋ฏธ๋์ ์ ๋ ฅํด์ ์ค์นํด์ค๋ค.
$ npm install react-slick --save
$ npm install slick-carousel --save
2. ๊ทธ๋ฆฌ๊ณ Slick์์ ์ ๊ณตํด์ฃผ๋ CSS๋ฅผ ์ฌ์ฉํ๊ธฐ ์ํด ๋ค์์ ์ฝ๋๋ฅผ importํด์ค๋ค.
import "slick-carousel/slick/slick.css";
import "slick-carousel/slick/slick-theme.css";
3. ๊ธฐ๋ณธ ํํ๋ ๋ค์๊ณผ ๊ฐ๋ค.
import React, { Component } from "react";
import Slider from "react-slick";
export default class SimpleSlider extends Component {
render() {
const settings = {
dots: true,
infinite: true,
speed: 500,
slidesToShow: 1,
slidesToScroll: 1
};
return (
<div>
<h2> Single Item</h2>
<Slider {...settings}>
<div>
<h3>1</h3>
</div>
<div>
<h3>2</h3>
</div>
<div>
<h3>3</h3>
</div>
<div>
<h3>4</h3>
</div>
<div>
<h3>5</h3>
</div>
<div>
<h3>6</h3>
</div>
</Slider>
</div>
);
}
}
4. ๋๋ ๋ค์๊ณผ ๊ฐ์ ํํ๋ก ์ฌ์ฉํ์๋ค.
์ผ๋จ ๋๋ React ํจ์ํ๊ณผ styled-component๋ก ์ฌ์ฉํ๋ค.
import React from 'react';
import Slider from 'react-slick';
import styled from 'styled-components';
import 'slick-carousel/slick/slick.css';
import 'slick-carousel/slick/slick-theme.css';
function Slide({ sliders }) {
return (
<Container>
<SlideTitle>์ธ๊ธฐ ์๋น์ค</SlideTitle>
<StyledSlider {...settings}>
{sliders.map(({ name, image }) => {
return (
<CardBox>
<CardImg alt="์ธ๊ธฐ ์๋น์ค" src={image} />
<CardText>{name}</CardText>
</CardBox>
);
})}
</StyledSlider>
</Container>
);
}
export default Slide;
// ์ฌ๋ผ์ด๋ ์ค์
const settings = {
dots: true, // ์ฌ๋ผ์ด๋ ๋ฐ์ ์ ๋ณด์ด๊ฒ
infinite: true, // ๋ฌดํ์ผ๋ก ๋ฐ๋ณต
speed: 500,
autoplay: true,
autoplaySpeed: 2000, // ๋์ด๊ฐ๋ ์๋
slidesToShow: 4, // 4์ฅ์ฉ ๋ณด์ด๊ฒ
slidesToScroll: 1, // 1์ฅ์ฉ ๋ค๋ก ๋์ด๊ฐ๊ฒ
centerMode: true,
centerPadding: '0px', // 0px ํ๋ฉด ์ฌ๋ผ์ด๋ ๋์ชฝ ์ด๋ฏธ์ง๊ฐ ์์๋ฆผ
};
const SlideTitle = styled.h2`
padding: 60px 0px 50px 0px;
text-align: center;
font-size: 30px;
font-weight: bolder;
`;
const Container = styled.div`
margin-right: 25px;
`;
// ์ฌ๋ผ์ด๋ CSS
const StyledSlider = styled(Slider)`
.slick-list {
width: 1600px;
margin: 0 auto;
}
.slick-slide div {
/* cursor: pointer; */
}
.slick-dots {
bottom: -50px;
margin-top: 200px;
}
.slick-track {
/* overflow-x: hidden; */
}
`;
const CardBox = styled.div`
cursor: pointer;
`;
const CardImg = styled.img`
width: 380px;
height: 190px;
`;
const CardText = styled.p`
padding: 20px;
font-size: 20px;
font-weight: bolder;
text-align: center;
`;
๐ณ ์ฅ๋จ์
- ์ฅ์
๋ฅ์๋ ์ฝ๊ฒ ์ฌ๋ผ์ด๋๋ฅผ ๊ตฌํํ ์ ์์. (๋ณธ์ธ ๋ฅ์) - ๋จ์
๋ผ์ด๋ธ๋ฌ๋ฆฌ์ ๊ธฐ๋ณธ CSS๊ฐ ์ ์ฉ๋์ด ์์ด์ ๋ด๊ฐ ์ํ๋ ํํ๋ก ์ฌ์ฉํ๋ ค๋ฉด
const StyledSlider = styled(Slider)`
.slick-list {
width: 1600px;
margin: 0 auto;
}
.slick-slide div {
/* cursor: pointer; */
}
.slick-dots {
bottom: -50px;
margin-top: 200px;
}
.slick-track {
/* overflow-x: hidden; */
}
`;
์ด๋ ๊ฒ CSS๋ฅผ ํ๋ํ๋ ๋ฏ์ด๋ณด๋ฉด์ ๊ณ ์ณ์ผํจ. =_= ์๋นํ ๊ท์ฐฎ์.
๋ฐ์ํ
'JavaScript > React' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
HTML, CSS ์ฝ๋๋ฅผ React๋ก ๋ณํํ ๋ ์ฃผ์ํ ์ (0) | 2021.11.17 |
---|---|
์ฃผ์ ์ฌ์ฉ๋ฒ (0) | 2021.11.16 |
Custom-Hook (0) | 2021.11.16 |
[์ํ์ฝ๋ฉ-React] State ์ฌ์ฉ (0) | 2021.05.29 |
[์ํ์ฝ๋ฉ-React] Component ํ์ผ๋ก ๋ถ๋ฆฌํ๊ธฐ (0) | 2021.05.29 |
๋๊ธ