Skip to content

Commit

Permalink
fix: 배너 슬라이더 드래그시 창 이동하지 않도록 수정 #239
Browse files Browse the repository at this point in the history
  • Loading branch information
Miensoap committed Dec 5, 2024
1 parent 701d7ad commit 1ada9be
Showing 1 changed file with 32 additions and 7 deletions.
39 changes: 32 additions & 7 deletions frontend/src/components/Banner/BannerSlider.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import React, { useEffect, useState } from 'react';
import React, { useEffect, useState, useRef } from 'react';
import Slider from 'react-slick';
import 'slick-carousel/slick/slick.css';
import 'slick-carousel/slick/slick-theme.css';
Expand All @@ -14,6 +14,8 @@ const BannerSlider: React.FC<BannerSliderProps> = ({
interval = 3000,
}) => {
const [banners, setBanners] = useState<Banner[]>([]);
const startPosition = useRef<{ x: number; y: number } | null>(null);
const isDragged = useRef(false);

useEffect(() => {
const fetchBanners = async () => {
Expand All @@ -34,18 +36,41 @@ const BannerSlider: React.FC<BannerSliderProps> = ({
autoplaySpeed: interval,
};

const handleMouseDown = (event: React.MouseEvent) => {
startPosition.current = { x: event.clientX, y: event.clientY };
isDragged.current = false;
};

const handleMouseUp = (event: React.MouseEvent) => {
if (startPosition.current) {
const deltaX = Math.abs(event.clientX - startPosition.current.x);
const deltaY = Math.abs(event.clientY - startPosition.current.y);

if (deltaX > 5 || deltaY > 5) {
isDragged.current = true;
}
}
startPosition.current = null;
};

const handleClick = (redirectUrl: string) => {
if (!isDragged.current) {
window.open(redirectUrl, '_blank');
}
};

return (
<div className={className}>
<Slider {...settings} className={'h-full w-full'}>
<Slider {...settings} className="h-full w-full">
{banners.map((banner, index) => (
<div key={index} className={'cursor-pointer'}>
<div key={index} className="cursor-pointer">
<img
className={
'box-sizing:border-box h-full w-full rounded-md border-[1.5px] border-c_border_gray'
}
className="box-sizing:border-box h-full w-full rounded-md border-[1.5px] border-c_border_gray"
src={banner.imageUrl}
alt={`Banner ${index}`}
onClick={() => window.open(banner.redirectUrl, '_blank')}
onMouseDown={handleMouseDown}
onMouseUp={handleMouseUp}
onClick={() => handleClick(banner.redirectUrl)}
/>
</div>
))}
Expand Down

0 comments on commit 1ada9be

Please sign in to comment.