Advertisement
Guest User

Untitled

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