Advertisement
Guest User

Untitled

a guest
Dec 12th, 2018
58
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.29 KB | None | 0 0
  1. import React, { Component } from "react";
  2.  
  3. import "./slider.css";
  4.  
  5. export default class Slider extends Component {
  6. constructor(props) {
  7. super(props);
  8. this.state = {
  9. slides: props.slides,
  10. timer: props.timer,
  11. curent: 0,
  12. curentTime: 0,
  13. sliderWidth:1000+"%",
  14. itemWidth: window.innerWidth, //"100vw"
  15. dots: null
  16. };
  17. this.handleAnimation = this.handleAnimation.bind(this);
  18. this.goLeft = this.goLeft.bind(this);
  19. this.goRight = this.goRight.bind(this);
  20. this.handleDotClick = this.handleDotClick.bind(this);
  21. }
  22. interval = null;
  23. componentWillReceiveProps = nextProps => {
  24. if (nextProps) {
  25. let go = {};
  26. if (nextProps.slides) {
  27. go.slides = nextProps.slides;
  28. go.sliderWidth = go.slides.length * 100;
  29. go.itemWidth = window.innerWidth;
  30. go.slides = go.slides.map((item, index) => (
  31. <div
  32. key={index}
  33. className="slide"
  34. // style={{ width: go.itemWidth + "px" }}
  35. >
  36. {item}
  37. </div>
  38. ));
  39. go.dots = go.slides.map((item, index) => (
  40. <span
  41. key={index}
  42. className={"dot" + ((index === 0 && " active") || "")}
  43. />
  44. ));
  45. }
  46. if (nextProps.timer) go.timer = nextProps.timer;
  47. this.setState(go, () => {
  48. });
  49. }
  50. };
  51. componentDidMount = async () => {
  52. let slide = await this.state.slides.map((item, index) => (
  53. <div
  54. key={index}
  55. className="slide"
  56. //style={{ width: "100vw" }}
  57. style={{ width: this.state.itemWidth + "px" }}
  58.  
  59. >
  60. {item}
  61. </div>
  62. ));
  63.  
  64. let dots = await this.state.slides.map((item, index) => (
  65. <span
  66. key={index}
  67. className={"dot" + ((index === 0 && " active") || "")}
  68. />
  69. ));
  70. this.setState({ slides: slide, dots });
  71. // this.setState({dots});
  72. };
  73. componentDidMount = () => {
  74. this.refs.wrap.style.transform = `translate(${this.state.itemWidth *
  75. this.state.curent}px)`;
  76. this.refs.wrap.style.transition = `transform 1s linear`;
  77. this.handleAnimation();
  78. this.interval = setInterval(this.handleAnimation, 1000);
  79. };
  80.  
  81. handleAnimation = () => {
  82. let state = this.state;
  83. if (state.curentTime === state.timer[state.curent]) {
  84. this.refs.wrap.style.transform = `translate(-${this.state.itemWidth *
  85. this.state.curent}px)`;
  86. this.refs.wrap.style.transition = `transform 1000ms linear`;
  87. let dots = this.state.slides.map((item, index) => (
  88. <span
  89. id={"d" + index}
  90. key={index}
  91. className={"dot" + ((index === state.curent && " active") || "")}
  92. onClick={this.handleDotClick}
  93. />
  94. ));
  95. this.setState({
  96. curentTime: 0,
  97. curent: (this.state.curent + 1) % this.state.slides.length,
  98. dots
  99. });
  100. } else if (state.curentTime < state.timer[state.curent]) {
  101. state.curentTime++;
  102. this.setState(state);
  103. }
  104. };
  105. goRight = e => {
  106. let dots = this.state.slides.map((item, index) => (
  107. <span
  108. id={"d" + index}
  109. key={index}
  110. className={
  111. "dot" +
  112. ((index === (this.state.curent + 1) % this.state.slides.length &&
  113. " active") ||
  114. "")
  115. }
  116. onClick={this.handleDotClick}
  117. />
  118. ));
  119. this.refs.wrap.style.transform = `translate(-${window.innerWidth *
  120. ((this.state.curent + 1) % this.state.slides.length)}px)`;
  121. this.refs.wrap.style.transition = `transform 1000ms linear`;
  122. clearInterval(this.interval);
  123. this.setState(
  124. {
  125. curent: (this.state.curent + 1) % this.state.slides.length,
  126. curentTime: 0,
  127. dots
  128. },
  129. () => {
  130. this.interval = setInterval(this.handleAnimation, 1000);
  131. }
  132. );
  133. };
  134. goLeft = e => {
  135. let dots = this.state.slides.map((item, index) => (
  136. <span
  137. id={"d" + index}
  138. key={index}
  139. className={
  140. "dot" +
  141. ((index ===
  142. (this.state.curent + this.state.slides.length - 1) %
  143. this.state.slides.length &&
  144. " active") ||
  145. "")
  146. }
  147. onClick={this.handleDotClick}
  148. />
  149. ));
  150. this.refs.wrap.style.transform = `translate(-${window.innerWidth *
  151. ((this.state.curent + this.state.slides.length - 1) %
  152. this.state.slides.length)}px)`;
  153. this.refs.wrap.style.transition = `transform 1000ms linear`;
  154. clearInterval(this.interval);
  155. this.setState(
  156. {
  157. curent:
  158. (this.state.curent + this.state.slides.length - 1) %
  159. this.state.slides.length,
  160. curentTime: 0,
  161. dots
  162. },
  163. () => {
  164. this.interval = setInterval(this.handleAnimation, 1000);
  165. }
  166. );
  167. };
  168. handleDotClick = e => {
  169. let index = parseInt(e.target.id.slice(1));
  170.  
  171. let dots = this.state.slides.map((item, dindex) => (
  172. <span
  173. id={"d" + dindex}
  174. key={dindex}
  175. className={"dot" + ((index === dindex && " active") || "")}
  176. onClick={this.handleDotClick}
  177. />
  178. ));
  179. this.refs.wrap.style.transform = `translate(-${window.innerWidth *
  180. index}px)`;
  181. this.refs.wrap.style.transition = `transform 1000ms linear`;
  182. clearInterval(this.interval);
  183. this.setState(
  184. {
  185. curent: index,
  186. curentTime: 0,
  187. dots
  188. },
  189. () => {
  190. this.interval = setInterval(this.handleAnimation, 1000);
  191. }
  192. );
  193. };
  194. render() {
  195. let slides = this.state.slides.map((item, index) => (
  196. <div
  197. key={index}
  198. className="slide"
  199. style={{ width: this.state.sliderWidth + "%", position: "relative"}}//+ "px" }}
  200. >
  201. {item}
  202. </div>
  203. ));
  204. return (
  205. <div className="slider">
  206. <div
  207. className="wrap"
  208. ref="wrap"
  209. style={{ width: this.state.sliderWidth ,position:"relative"}}//+ "%", position: "relative" }}
  210. >
  211. {slides}
  212. </div>
  213. <div className="dots">{this.state.dots}</div>
  214. <span className="left" onClick={this.goLeft} />
  215. <span className="right" onClick={this.goRight} />
  216. </div>
  217. );
  218. }
  219. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement