Skip to content

Commit

Permalink
Merge pull request #47 from 2023WISCOM/feature/#11-project
Browse files Browse the repository at this point in the history
이미지 슬라이더 수정 및 화살표 구현
  • Loading branch information
harim061 authored Nov 2, 2023
2 parents 805edb5 + f3ee68d commit 9633cd4
Show file tree
Hide file tree
Showing 6 changed files with 168 additions and 143 deletions.
104 changes: 60 additions & 44 deletions WISCOM/src/components/Project/ImageSlider.jsx
Original file line number Diff line number Diff line change
@@ -1,79 +1,95 @@
import { useState, useEffect } from 'react';
import * as I from './ImageSliderStyle';
import LeftArrow from '../../img/Project/LeftArrow.svg'; // 화살표 이미지 경로
import RightArrow from '../../img/Project/RightArrow.svg'; // 화살표 이미지 경로

import { useParams } from 'react-router-dom';
import * as I from './ImageSliderStyle';
import LeftArrow from '../../img/Project/RightArrow.svg';
import RightArrow from '../../img/Project/RightArrow.svg';
import LeftArrowHover from '../../img/Project/RightArrowHover.svg';
import RightArrowHover from '../../img/Project/RightArrowHover.svg';

const ImageSlider = () => {
const [data, setData] = useState(null);
const { post_id } = useParams();
const [slideImages, setSlideImages] = useState([]);
const [currentIdx, setCurrentIdx] = useState(0);
const [showArrows, setShowArrows] = useState(false);
const [isLeftArrowHovered, setIsLeftArrowHovered] = useState(false);
const [isRightArrowHovered, setIsRightArrowHovered] = useState(false);

const handleLeftArrowMouseEnter = () => {
setIsLeftArrowHovered(true);
};

const handleLeftArrowMouseLeave = () => {
setIsLeftArrowHovered(false);
};

const handleRightArrowMouseEnter = () => {
setIsRightArrowHovered(true);
};

const handleRightArrowMouseLeave = () => {
setIsRightArrowHovered(false);
};

const preSlide = () => {
setCurrentIdx((preIdx) => (preIdx - 1 + slideImages.length) % slideImages.length);
};

const nextSlide = () => {
setCurrentIdx((preIdx) => (preIdx + 1) % slideImages.length);
};

useEffect(() => {
const nextPostId = Number(post_id) + 1;
// 정적 URL을 사용하여 데이터를 가져옵니다.
fetch(`http://13.124.248.135/posts/${nextPostId}/`, {
method: 'GET',
})
.then((response) => response.json())
.then((data) => {
setData(data);
setSlideImages(data.images);
console.log('데이터 가져오기 성공');
})
.catch((error) => console.error('데이터 가져오기 오류:', error));
}, [post_id]);

useEffect(() => {
const interval = setInterval(() => {
nextSlide();
}, 3000);
}, 15000);

return () => clearInterval(interval);
}, [currentIdx, slideImages.length]);

const preSlide = () => {
setCurrentIdx((preIdx) => (preIdx - 1 + slideImages.length) % slideImages.length);
console.log('이전 이미지 가져오기');
};

const nextSlide = () => {
setCurrentIdx((preIdx) => (preIdx + 1) % slideImages.length);
console.log('다음 이미지 가져오기');
};

const toggleArrows = () => {
setShowArrows(!showArrows);
};

return (
<I.BannerWrap onMouseEnter={toggleArrows} onMouseLeave={toggleArrows}>
<I.ImageSliderContainer>
{data && (
<I.SlideBanner>
{slideImages.map((image, index) => (
<I.BannerImage
key={index}
className={`${index === currentIdx ? 'active' : ''}`}
src={`http://13.124.248.135/${image}`}
alt={`slide ${index}`}
/>
))}
{showArrows && (
<>
<I.PreArrow>
<img onClick={preSlide} src={LeftArrow} alt="Previous" />
</I.PreArrow>
<I.NextArrow>
<img onClick={nextSlide} src={RightArrow} alt="Next" />
</I.NextArrow>
</>
)}
</I.SlideBanner>
<>
<I.PreArrow
onClick={preSlide}
onMouseEnter={handleLeftArrowMouseEnter}
onMouseLeave={handleLeftArrowMouseLeave}>
<img src={isLeftArrowHovered ? LeftArrowHover : LeftArrow} alt="Previous" />
</I.PreArrow>
<I.SliderBannerWrap>
<I.SlideBanner>
{slideImages.map((image, index) => (
<I.BannerImage
key={index}
className={`${index === currentIdx ? 'active' : ''}`}
src={`http://13.124.248.135/${image}`}
alt={`slide ${index}`}
/>
))}
</I.SlideBanner>
</I.SliderBannerWrap>
<I.NextArrow
onClick={nextSlide}
onMouseEnter={handleRightArrowMouseEnter}
onMouseLeave={handleRightArrowMouseLeave}>
<img src={isRightArrowHovered ? RightArrowHover : RightArrow} alt="Next" />
</I.NextArrow>
</>
)}
</I.BannerWrap>
</I.ImageSliderContainer>
);
};

Expand Down
115 changes: 44 additions & 71 deletions WISCOM/src/components/Project/ImageSliderStyle.jsx
Original file line number Diff line number Diff line change
@@ -1,88 +1,49 @@
import styled from 'styled-components';

export const BannerWrap = styled.div`
export const ImageSliderContainer = styled.div`
display: flex;
align-items: center;
text-align: center;
max-width: 1440px;
height: 600px;
margin-bottom: 20%;
border-radius: 30px;
box-shadow: 0 2px 5px rgba(0, 0, 0, 0.1);
--arrow-size: 70px; /* 화살표 크기 변수 설정 */
justify-content: space-between;
@media (max-width: 1442px) {
width: 100%;
height: 710px;
}
@media (max-width: 1350px) {
width: 100%;
height: 650px;
}
--arrow-size: 90px; /* 화살표 크기 변수 설정 */
position: relative; /* 부모 클래스에 relative 포지션을 추가 */
@media (max-width: 1250px) {
width: 100%;
height: 600px;
}
@media (max-width: 1165px) {
width: 100%;
height: 500px;
}
@media (max-width: 1000px) {
width: 100%;
height: 460px;
--arrow-size: 60px; /* 미디어 쿼리에서 화살표 크기 조절 */
}
margin-bottom: 200px;
`;

@media (max-width: 900px) {
width: 100%;
height: 420px;
--arrow-size: 50px; /* 미디어 쿼리에서 화살표 크기 조절 */
}
export const SliderBannerWrap = styled.div`
// max-width: 100%;
// width: 1100px;
// height: 700px;
@media (max-width: 850px) {
width: 100%;
height: 380px;
--arrow-size: 40px; /* 미디어 쿼리에서 화살표 크기 조절 */
}
width: 100%;
max-width: 1100px;
position: relative;
aspect-ratio: 11 / 7; /* 가로 세로 비율을 11:7로 고정 */
@media (max-width: 690px) {
width: 100%;
height: 280px;
--arrow-size: 30px; /* 미디어 쿼리에서 화살표 크기 조절 */
}
border-radius: 30px;
box-shadow: 0 2px 5px rgba(0, 0, 0, 0.1);
position: relative;
display: flex;
align-items: center;
@media (max-width: 525px) {
width: 100%;
height: 200px;
--arrow-size: 20px; /* 미디어 쿼리에서 화살표 크기 조절 */
}
justify-content: center;
margin: 0 auto; /* 필수!!!!! */
@media (max-width: 393px) {
max-width: 330px;
height: 150px;
--arrow-size: 15px; /* 미디어 쿼리에서 화살표 크기 조절 */
}
margin-left: 10%;
margin-right: 10%;
`;

export const SlideBanner = styled.div`
position: relative;
width: 100%;
height: 100%;
&:hover .preArrow,
&:hover .nextArrow {
cursor: pointer;
opacity: 1;
transition: 0.7s;
}
`;

export const BannerImage = styled.img`
width: 100%;
height: 100%;
object-fit: cover;
object-fit: contain;
border-radius: 30px;
position: absolute;
left: 0;
Expand All @@ -96,25 +57,37 @@ export const BannerImage = styled.img`

export const Arrow = styled.div`
position: absolute;
top: 50%; /* 세로 가운데 정렬 */
width: var(--arrow-size);
height: calc(var(--arrow-size) * 1.43);
background-color: rgba(255, 255, 255, 0.5);
display: flex;
border-radius: 5px;
opacity: 0; /* 처음에는 화살표를 보이지 않게 설정 */
transition: opacity 0.8s ease-in-out;
@media (max-width: 1000px) {
width: 60px; /* 화면이 850px보다 작을 때 화살표 크기를 60px로 조절 */
height: calc(60px * 1.43);
}
@media (max-width: 768px) {
width: 50px; /* 화면이 850px보다 작을 때 화살표 크기를 60px로 조절 */
height: calc(50px * 1.43);
}
@media (max-width: 550px) {
width: 40px; /* 화면이 850px보다 작을 때 화살표 크기를 60px로 조절 */
height: calc(40px * 1.43);
}
&:hover {
opacity: 1; /* 마우스 호버 시에만 화살표를 보이도록 설정 */
@media (max-width: 426px) {
width: 30px; /* 화면이 850px보다 작을 때 화살표 크기를 60px로 조절 */
height: calc(30px * 1.43);
}
`;

export const PreArrow = styled(Arrow)`
left: 0;
transform: translateY(-50%); /* 세로 가운데 정렬을 위해 추가 */
transform: rotate(180deg); /* 세로 가운데 정렬을 위해 추가 */
`;

export const NextArrow = styled(Arrow)`
right: 0;
transform: translateY(-50%); /* 세로 가운데 정렬을 위해 추가 */
`;
29 changes: 15 additions & 14 deletions WISCOM/src/img/Project/LeftArrow.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
17 changes: 17 additions & 0 deletions WISCOM/src/img/Project/LeftArrowHover.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
29 changes: 15 additions & 14 deletions WISCOM/src/img/Project/RightArrow.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
17 changes: 17 additions & 0 deletions WISCOM/src/img/Project/RightArrowHover.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.

0 comments on commit 9633cd4

Please sign in to comment.