Advertisement
Guest User

Untitled

a guest
Mar 25th, 2019
59
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.67 KB | None | 0 0
  1. import React from 'react';
  2. import StyledWrapper from './styled';
  3. import { Spring } from 'react-spring/renderprops';
  4. import Hammer from 'react-hammerjs';
  5.  
  6.  
  7. export default class extends React.Component {
  8.  
  9. static defaultProps = {
  10. items: [
  11. { src: '/assets/corgi.jpg' },
  12. { src: '/assets/bulldog.jpg' },
  13. { src: '/assets/golden.jpg' },
  14. { src: '/assets/terrier.jpg' },
  15. { src: '/assets/puppy.jpg' },
  16. ]
  17. }
  18. state = {
  19. currentIndex: 0,
  20. isPressed: false,
  21. pressedX: 0,
  22. translateX: 0,
  23. currentTranslateX: 0,
  24. }
  25.  
  26. handleTouchStart = (e) => {
  27. this.handleMouseDown(e.touches[0]);
  28. }
  29.  
  30.  
  31.  
  32. handleMouseDown = (e) => {
  33. this.setState({
  34. pressedX: e.clientX,
  35. isPressed: true,
  36. currentTranslateX: this.state.translateX,
  37. });
  38.  
  39. document.addEventListener('mousemove', this.handleMouseMove);
  40. document.addEventListener('touchmove', this.handleTouchMove);
  41. document.addEventListener('mouseup', this.handleMouseUp);
  42. document.addEventListener('touchend', this.handleMouseUp);
  43.  
  44. }
  45.  
  46. handleTouchMove = e => {
  47. this.handleMouseMove(e.touches[0]);
  48. }
  49.  
  50. handleMouseMove = (e) => {
  51. if (this.state.isPressed) {
  52. const moveX = e.clientX - this.state.pressedX;
  53. this.setState({
  54. translateX: this.state.currentTranslateX + moveX,
  55. })
  56. }
  57. }
  58.  
  59. handleMouseUp = (e) => {
  60. const windowWidth = window.innerWidth;
  61. console.log({ windowWidth, translateX: this.state.translateX });
  62.  
  63. this.setState({
  64. isPressed: false,
  65. });
  66.  
  67. const targetIndex = Math.round(-this.state.translateX / windowWidth);
  68. this.goToSlide(targetIndex);
  69.  
  70. document.removeEventListener('mousemove', this.handleMouseMove);
  71. document.removeEventListener('touchmove', this.handleTouchMove);
  72. document.removeEventListener('mouseup', this.handleMouseUp);
  73. document.removeEventListener('touchend', this.handleMouseUp);
  74. }
  75.  
  76. handleSwipe = e => {
  77. console.log('swipe', e.velocityX);
  78. if (e.velocityX < -0.5) this.goNext();
  79. if (e.velocityX > 0.5) this.goPrev();
  80. }
  81.  
  82. goToSlide = targetIndex => {
  83. console.log('goToSlide', targetIndex);
  84. const windowWidth = window.innerWidth;
  85.  
  86. this.setState({
  87. currentIndex: targetIndex,
  88. translateX: -targetIndex * windowWidth,
  89. })
  90. }
  91.  
  92. goNext = e => {
  93. console.log('goNext');
  94. this.goToSlide(this.state.currentIndex + 1);
  95. }
  96.  
  97. goPrev = e => {
  98. console.log('goPrev');
  99. this.goToSlide(this.state.currentIndex - 1);
  100. }
  101.  
  102. render() {
  103. const { items } = this.props;
  104. const { translateX, isPressed } = this.state;
  105. return (
  106. <Hammer
  107. onSwipe={this.handleSwipe}
  108. >
  109. <StyledWrapper>
  110. <div className={`slider-wrapper`}>
  111. <Spring config={{ tension: 260, friction: 26 }} to={{ translateX }}>
  112. {props => {
  113. return (
  114. <div
  115. className={`slider-item-container ${isPressed ? 'dragging' : ''}`}
  116. style={{
  117. ...props,
  118. transform: `translateX(${props.translateX}px)`
  119. }}
  120. onMouseDown={this.handleMouseDown}
  121. onTouchStart={this.handleTouchStart}
  122. // onMouseMove={this.handleMouseMove}
  123. // onMouseUp={this.handleMouseUp}
  124. >
  125. {items.map(item => (
  126. <div className="slider-item" key={item.src}>
  127. <img src={item.src} alt="" draggable={false} />
  128. </div>
  129. ))}
  130. </div>
  131. )
  132. }}
  133. </Spring>
  134. </div>
  135. </StyledWrapper>
  136. </Hammer>
  137. );
  138. }
  139. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement