Image Slide Show in React using react-motion

In this example below you will see how to do a Image Slide Show in React using react-motion with some HTML / CSS and Javascript

Thumbnail
This awesome code was written by 0x1000, you can see more from this user in the personal repository.
You can find the original code on Codepen.io
Copyright 0x1000 ©

Technologies

  • HTML
  • CSS
  • JavaScript
<!DOCTYPE html>
<html lang="en" >

<head>
  <meta charset="UTF-8">
  <title>Image Slide Show in React using react-motion</title>
  
  
  
      <link rel="stylesheet" href="css/style.css">

  
</head>

<body>

  <div id="react-mount-point"></div>
  <script src='https://cdnjs.cloudflare.com/ajax/libs/react/15.3.2/react.min.js'></script>
<script src='https://cdnjs.cloudflare.com/ajax/libs/react/15.3.1/react-dom.min.js'></script>
<script src='https://npmcdn.com/react-motion@0.4.2/build/react-motion.js'></script>

  

    <script  src="js/index.js"></script>




</body>

</html>

/*Downloaded from https://www.codeseek.co/0x1000/image-slide-show-in-react-using-react-motion-MjMYge */
/* Nope */


/*Downloaded from https://www.codeseek.co/0x1000/image-slide-show-in-react-using-react-motion-MjMYge */
var _createClass = function () { function defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if ("value" in descriptor) descriptor.writable = true; Object.defineProperty(target, descriptor.key, descriptor); } } return function (Constructor, protoProps, staticProps) { if (protoProps) defineProperties(Constructor.prototype, protoProps); if (staticProps) defineProperties(Constructor, staticProps); return Constructor; }; }();

function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }

function _possibleConstructorReturn(self, call) { if (!self) { throw new ReferenceError("this hasn't been initialised - super() hasn't been called"); } return call && (typeof call === "object" || typeof call === "function") ? call : self; }

function _inherits(subClass, superClass) { if (typeof superClass !== "function" && superClass !== null) { throw new TypeError("Super expression must either be null or a function, not " + typeof superClass); } subClass.prototype = Object.create(superClass && superClass.prototype, { constructor: { value: subClass, enumerable: false, writable: true, configurable: true } }); if (superClass) Object.setPrototypeOf ? Object.setPrototypeOf(subClass, superClass) : subClass.__proto__ = superClass; }

var TransitionMotion = ReactMotion.TransitionMotion;
var spring = ReactMotion.spring;

var BackgroundImage = function (_React$Component) {
  _inherits(BackgroundImage, _React$Component);

  function BackgroundImage() {
    _classCallCheck(this, BackgroundImage);

    return _possibleConstructorReturn(this, (BackgroundImage.__proto__ || Object.getPrototypeOf(BackgroundImage)).apply(this, arguments));
  }

  _createClass(BackgroundImage, [{
    key: 'render',
    value: function render() {
      var style = {
        position: 'fixed',
        top: 0,
        margin: 0,
        left: 0,
        zIndex: -1000,
        backgroundColor: ['red', 'green', 'blue', 'gray', 'black'][this.props.page],
        width: '100%',
        height: '100%',
        transform: this.props.style.transform
      };

      return React.createElement('div', { key: this.props.page, style: style });
    }
  }]);

  return BackgroundImage;
}(React.Component);

var Animation = function (_React$Component2) {
  _inherits(Animation, _React$Component2);

  function Animation(props) {
    _classCallCheck(this, Animation);

    var _this2 = _possibleConstructorReturn(this, (Animation.__proto__ || Object.getPrototypeOf(Animation)).call(this, props));

    _this2.state = {
      page: 0
    };
    return _this2;
  }

  _createClass(Animation, [{
    key: 'handleChange',
    value: function handleChange() {
      console.log('changed: ' + this.state.page);
      this.setState({ page: (this.state.page + 1) % 5 });
      setTimeout(this.handleChange.bind(this), 2000);
    }
  }, {
    key: 'componentDidMount',
    value: function componentDidMount() {
      setTimeout(this.handleChange.bind(this), 2000);
    }
  }, {
    key: 'willLeave',
    value: function willLeave() {
      return { x: spring(-100) };
    }
  }, {
    key: 'willEnter',
    value: function willEnter() {
      return { x: 100 };
    }
  }, {
    key: 'render',
    value: function render() {
      return React.createElement(
        TransitionMotion,
        { willEnter: this.willEnter.bind(this), willLeave: this.willLeave.bind(this),
          defaultStyles: [{
            key: this.state.page,
            style: {
              x: -100
            }
          }],
          styles: [{ key: this.state.page, style: { x: spring(0) } }]
        },
        function (interpolatingStyles) {
          return React.createElement(
            'div',
            null,
            ' ',
            interpolatingStyles.map(function (config) {
              return React.createElement(BackgroundImage, { page: config.key, key: config.key, style: { transform: 'translateX(' + config.style.x + '%)' } });
            })
          );
        }
      );
    }
  }]);

  return Animation;
}(React.Component);

ReactDOM.render(React.createElement(Animation, null), document.getElementById('react-mount-point'));

Comments