Skip to content

Instantly share code, notes, and snippets.

@DZuz14
Last active February 7, 2020 18:42
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save DZuz14/bd3c02027b050d48d74810bf544fd555 to your computer and use it in GitHub Desktop.
Save DZuz14/bd3c02027b050d48d74810bf544fd555 to your computer and use it in GitHub Desktop.
Slide Width Calculation
import React, { Component } from 'react'
import Slide from '../slide'
import LeftArrow from '../left-arrow'
import RightArrow from '../right-arrow'
export default class Slider extends Component {
constructor(props) {
super(props)
this.state = {
images: [
"https://s3.us-east-2.amazonaws.com/dzuz14/thumbnails/aurora.jpg",
"https://s3.us-east-2.amazonaws.com/dzuz14/thumbnails/canyon.jpg",
"https://s3.us-east-2.amazonaws.com/dzuz14/thumbnails/city.jpg",
"https://s3.us-east-2.amazonaws.com/dzuz14/thumbnails/desert.jpg",
"https://s3.us-east-2.amazonaws.com/dzuz14/thumbnails/mountains.jpg",
"https://s3.us-east-2.amazonaws.com/dzuz14/thumbnails/redsky.jpg",
"https://s3.us-east-2.amazonaws.com/dzuz14/thumbnails/sandy-shores.jpg",
"https://s3.us-east-2.amazonaws.com/dzuz14/thumbnails/tree-of-life.jpg"
],
currentIndex: 0,
translateValue: 0
}
}
goToPrevSlide = () => {
}
goToNextSlide = () => {
// Exiting the method early if we are at the end of the images array.
// We also want to reset currentIndex and translateValue, so we return
// to the first image in the array.
if(this.state.currentIndex === this.state.images.length - 1) {
return this.setState({
currentIndex: 0,
translateValue: 0
})
}
// This will not run if we met the if condition above
this.setState(prevState => ({
currentIndex: prevState.currentIndex + 1,
translateValue: prevState.translateValue + -(this.slideWidth())
}));
}
slideWidth = () => {
return document.querySelector('.slide').clientWidth
}
render() {
return (
<div className="slider">
<div className="slider-wrapper"
style={{
transform: `translateX(${this.state.translateValue}px)`,
transition: 'transform ease-out 0.45s'
}}>
{
this.state.images.map((image, i) => (
<Slide key={i} image={image} />
))
}
</div>
<LeftArrow
goToPrevSlide={this.goToPrevSlide}
/>
<RightArrow
goToNextSlide={this.goToNextSlide}
/>
</div>
);
}
}
@Nageswaran-1997
Copy link

is this possible to without images like text or contents slider.?

@DZuz14
Copy link
Author

DZuz14 commented Feb 7, 2020

Yup. Check out the newer version I built with React Hooks.

When you look at the code for the new Slider component and ignore all of the fancy looking JS and JSX, you'll realize the Slider actually creates a very simple set of DOM nodes. Thinking abstractically, it's as simple as this:

<div class="slider">
   <div class="slider-wrapper">
      <!-- Place your content in here -->
   </div>
</div>

The parent div in my example is set to 100vh & 100vw, and it's direct descendant is set to be 100% of it's parent's height and width. In my example, I chose to put in some images, but you could easily replace that with any other content. All that is being done to show the next "slide" is done using css transforms, moving the slider-wrapper back and forth.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment