Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Autoplay #3

Open
sami616 opened this issue Aug 1, 2017 · 1 comment
Open

Autoplay #3

sami616 opened this issue Aug 1, 2017 · 1 comment

Comments

@sami616
Copy link

sami616 commented Aug 1, 2017

Not an issue per se, but is there a simple way to introduce an autoplay feature?

@Stanko
Copy link
Owner

Stanko commented Aug 1, 2017

Hello @sami616,
This slider is small component I wrote year ago, and I'm actively working on it.

But you can make simple slideshow - set a timer and update selected prop that you are passing to the slider component.

Something like this:

// Parent component
export default class ParentSlideshow extends Component {
  constructor() {
    super();

    this.state = {
      selected: 0,
    };
  }

  componentDidMount() {
    const SLIDES_COUNT = 5;
    
    this.intervalID = setInterval(() => {
      const { selected } = this.state;
      
      this.setState({
        // Set the next slide, and make sure we return to zero
        // when selected is larger than SLIDES_COUNT
        selected: selected + 1 % SLIDES_COUNT,
      });
    }, 1000);
  }

  componentWillUnmount() {
    // clear interval
    clearInterval(this.intervalID);
    this.intervalID = null;
  }

  render() {
    const { selected } = this.state;
    
    return (
      <Slider
        loop={ true }
        showNav={ false }
        selected={ selected }>
        <div style={{ background: '#21BB9A' }}>A</div>
        <div style={{ background: '#329ADD' }}>B</div>
        <div style={{ background: '#9A5CB9' }}>C</div>
        <div style={{ background: '#E64C3C' }}>D</div>
        <div style={{ background: '#2D3F52' }}>E</div>
      </Slider>
    );
  }
}

Hope that helps, cheers!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants