A Pen by Omar Benseddik on CodePen.
Created
October 28, 2021 05:32
-
-
Save maddyonline/f64a2660f9436f6033e35ad65969f204 to your computer and use it in GitHub Desktop.
Auto-Play Slideshow React
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<div id="App"></div> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const colors = ["#0088FE", "#00C49F", "#FFBB28"]; | |
const delay = 2500; | |
function Slideshow() { | |
const [index, setIndex] = React.useState(0); | |
const timeoutRef = React.useRef(null); | |
function resetTimeout() { | |
if (timeoutRef.current) { | |
clearTimeout(timeoutRef.current); | |
} | |
} | |
React.useEffect(() => { | |
resetTimeout(); | |
timeoutRef.current = setTimeout( | |
() => | |
setIndex((prevIndex) => | |
prevIndex === colors.length - 1 ? 0 : prevIndex + 1 | |
), | |
delay | |
); | |
return () => { | |
resetTimeout(); | |
}; | |
}, [index]); | |
return ( | |
<div className="slideshow"> | |
<div | |
className="slideshowSlider" | |
style={{ transform: `translate3d(${-index * 100}%, 0, 0)` }} | |
> | |
{colors.map((backgroundColor, index) => ( | |
<div | |
className="slide" | |
key={index} | |
style={{ backgroundColor }} | |
></div> | |
))} | |
</div> | |
<div className="slideshowDots"> | |
{colors.map((_, idx) => ( | |
<div | |
key={idx} | |
className={`slideshowDot${index === idx ? " active" : ""}`} | |
onClick={() => { | |
setIndex(idx); | |
}} | |
></div> | |
))} | |
</div> | |
</div> | |
); | |
} | |
ReactDOM.render(<Slideshow />, document.getElementById("App")); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<script src="https://unpkg.com/react/umd/react.development.js"></script> | |
<script src="https://unpkg.com/react-dom/umd/react-dom.development.js"></script> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* Slideshow */ | |
.slideshow { | |
margin: 0 auto; | |
overflow: hidden; | |
max-width: 500px; | |
} | |
.slideshowSlider { | |
white-space: nowrap; | |
transition: ease 1000ms; | |
} | |
.slide { | |
display: inline-block; | |
height: 400px; | |
width: 100%; | |
border-radius: 40px; | |
} | |
/* Buttons */ | |
.slideshowDots { | |
text-align: center; | |
} | |
.slideshowDot { | |
display: inline-block; | |
height: 20px; | |
width: 20px; | |
border-radius: 50%; | |
cursor: pointer; | |
margin: 15px 7px 0px; | |
background-color: #c4c4c4; | |
} | |
.slideshowDot.active { | |
background-color: #6a0dad; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment