PhotoSlider.tsx 10.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366
  1. import React from 'react';
  2. import classNames from 'classnames';
  3. import PhotoView from './PhotoView';
  4. import SlideWrap from './components/SlideWrap';
  5. import CloseSVG from './components/CloseSVG';
  6. import isMobile from './utils/isMobile';
  7. import './PhotoSlider.less';
  8. import { dataType, IPhotoProviderBase } from './types';
  9. import { defaultOpacity, horizontalOffset, maxMoveOffset } from './variables';
  10. export interface IPhotoSliderProps extends IPhotoProviderBase {
  11. // 图片列表
  12. images: dataType[];
  13. // 图片当前索引
  14. index?: number;
  15. // 可见
  16. visible: boolean;
  17. // 关闭事件
  18. onClose: (evt?: React.MouseEvent | React.TouchEvent) => void;
  19. // 索引改变回调
  20. onIndexChange?: Function;
  21. }
  22. type PhotoSliderState = {
  23. // 偏移量
  24. translateX: number;
  25. // 图片当前的 index
  26. photoIndex: number;
  27. // 图片处于触摸的状态
  28. touched: boolean;
  29. // Reach 开始时 x 坐标
  30. lastClientX: number | undefined;
  31. // Reach 开始时 y 坐标
  32. lastClientY: number | undefined;
  33. // 背景透明度
  34. backdropOpacity: number;
  35. // 缩放度
  36. photoScale: number;
  37. // 覆盖物可见度
  38. overlayVisible: boolean;
  39. };
  40. export default class PhotoSlider extends React.Component<
  41. IPhotoSliderProps,
  42. PhotoSliderState
  43. > {
  44. static displayName = 'PhotoSlider';
  45. static defaultProps = {
  46. maskClosable: true,
  47. photoClosable: false,
  48. bannerVisible: true,
  49. introVisible: true,
  50. };
  51. static getDerivedStateFromProps(nextProps, prevState) {
  52. if (
  53. nextProps.index !== undefined &&
  54. nextProps.index !== prevState.photoIndex
  55. ) {
  56. return {
  57. photoIndex: nextProps.index,
  58. translateX: -(window.innerWidth + horizontalOffset) * nextProps.index,
  59. };
  60. }
  61. return null;
  62. }
  63. constructor(props) {
  64. super(props);
  65. this.state = {
  66. translateX: 0,
  67. photoIndex: 0,
  68. touched: false,
  69. lastClientX: undefined,
  70. lastClientY: undefined,
  71. backdropOpacity: defaultOpacity,
  72. photoScale: 1,
  73. overlayVisible: true,
  74. };
  75. }
  76. componentDidMount() {
  77. const { index = 0 } = this.props;
  78. this.setState({
  79. translateX: index * -(window.innerWidth + horizontalOffset),
  80. photoIndex: index,
  81. });
  82. window.addEventListener('resize', this.handleResize);
  83. }
  84. componentWillUnmount() {
  85. window.removeEventListener('resize', this.handleResize);
  86. }
  87. handleClose = () => {
  88. this.props.onClose();
  89. this.setState({
  90. overlayVisible: true,
  91. });
  92. };
  93. handlePhotoTap = () => {
  94. if (this.props.photoClosable) {
  95. this.handleClose();
  96. } else {
  97. this.setState(prevState => ({
  98. overlayVisible: !prevState.overlayVisible,
  99. }));
  100. }
  101. };
  102. handlePhotoMaskTap = () => {
  103. if (this.props.maskClosable) {
  104. this.handleClose();
  105. }
  106. };
  107. handleResize = () => {
  108. const { innerWidth } = window;
  109. this.setState(({ photoIndex }) => {
  110. return {
  111. translateX: -(innerWidth + horizontalOffset) * photoIndex,
  112. lastClientX: undefined,
  113. lastClientY: undefined,
  114. };
  115. });
  116. };
  117. handleReachVerticalMove = (_, clientY) => {
  118. this.setState(({ lastClientY, backdropOpacity }) => {
  119. if (lastClientY === undefined) {
  120. return {
  121. touched: true,
  122. lastClientY: clientY,
  123. backdropOpacity,
  124. photoScale: 1,
  125. };
  126. }
  127. const offsetClientY = Math.abs(clientY - lastClientY);
  128. return {
  129. touched: true,
  130. lastClientY,
  131. backdropOpacity: Math.max(
  132. Math.min(defaultOpacity, defaultOpacity - offsetClientY / 100 / 2),
  133. 0,
  134. ),
  135. photoScale: Math.max(Math.min(1, 1 - offsetClientY / 100 / 10), 0.6),
  136. };
  137. });
  138. };
  139. handleReachHorizontalMove = clientX => {
  140. const { innerWidth } = window;
  141. this.setState(({ lastClientX, translateX, photoIndex }) => {
  142. if (lastClientX === undefined) {
  143. return {
  144. touched: true,
  145. lastClientX: clientX,
  146. translateX,
  147. };
  148. }
  149. const offsetClientX = clientX - lastClientX;
  150. return {
  151. touched: true,
  152. lastClientX: lastClientX,
  153. translateX:
  154. -(innerWidth + horizontalOffset) * photoIndex + offsetClientX,
  155. };
  156. });
  157. };
  158. handleIndexChange = (photoIndex: number) => {
  159. const singlePageWidth = window.innerWidth + horizontalOffset;
  160. const translateX = -singlePageWidth * photoIndex;
  161. this.setState({
  162. translateX,
  163. photoIndex,
  164. });
  165. const { onIndexChange } = this.props;
  166. if (onIndexChange) {
  167. onIndexChange(photoIndex);
  168. }
  169. };
  170. handleReachUp = (clientX: number, clientY: number) => {
  171. const { innerWidth, innerHeight } = window;
  172. const { images, onIndexChange, onClose } = this.props;
  173. const {
  174. lastClientX = clientX,
  175. lastClientY = clientY,
  176. photoIndex,
  177. overlayVisible,
  178. } = this.state;
  179. const offsetClientX = clientX - lastClientX;
  180. const offsetClientY = clientY - lastClientY;
  181. const singlePageWidth = innerWidth + horizontalOffset;
  182. // 当前偏移
  183. let currentTranslateX = -singlePageWidth * photoIndex;
  184. let currentPhotoIndex = photoIndex;
  185. let isChangeVisible = false;
  186. if (Math.abs(offsetClientY) > innerHeight * 0.14) {
  187. isChangeVisible = true;
  188. onClose();
  189. // 下一张
  190. } else if (
  191. offsetClientX < -maxMoveOffset &&
  192. photoIndex < images.length - 1
  193. ) {
  194. currentPhotoIndex = photoIndex + 1;
  195. currentTranslateX = -singlePageWidth * currentPhotoIndex;
  196. if (onIndexChange) {
  197. onIndexChange(currentPhotoIndex);
  198. }
  199. // 上一张
  200. } else if (offsetClientX > maxMoveOffset && photoIndex > 0) {
  201. currentPhotoIndex = photoIndex - 1;
  202. currentTranslateX = -singlePageWidth * currentPhotoIndex;
  203. if (onIndexChange) {
  204. onIndexChange(currentPhotoIndex);
  205. }
  206. }
  207. this.setState({
  208. touched: false,
  209. translateX: currentTranslateX,
  210. photoIndex: currentPhotoIndex,
  211. lastClientX: undefined,
  212. lastClientY: undefined,
  213. backdropOpacity: defaultOpacity,
  214. photoScale: 1,
  215. overlayVisible: isChangeVisible ? true : overlayVisible,
  216. });
  217. };
  218. render() {
  219. const {
  220. images,
  221. visible,
  222. className,
  223. maskClassName,
  224. viewClassName,
  225. imageClassName,
  226. onClose,
  227. bannerVisible,
  228. introVisible,
  229. overlayRender,
  230. loadingElement,
  231. brokenElement,
  232. } = this.props;
  233. const {
  234. translateX,
  235. touched,
  236. photoIndex,
  237. backdropOpacity,
  238. photoScale,
  239. overlayVisible,
  240. } = this.state;
  241. const imageLength = images.length;
  242. const transform = `translate3d(${translateX}px, 0px, 0)`;
  243. // Overlay
  244. const overlayIntro = imageLength ? images[photoIndex].intro : undefined;
  245. const overlayStyle = { opacity: +overlayVisible };
  246. if (visible) {
  247. const { innerWidth } = window;
  248. return (
  249. <SlideWrap className={className}>
  250. <div
  251. className={classNames(
  252. 'PhotoView-PhotoSlider__Backdrop',
  253. maskClassName,
  254. )}
  255. style={{ background: `rgba(0, 0, 0, ${backdropOpacity})` }}
  256. />
  257. {bannerVisible && (
  258. <div
  259. className="PhotoView-PhotoSlider__BannerWrap"
  260. style={overlayStyle}
  261. >
  262. <div className="PhotoView-PhotoSlider__Counter">
  263. {photoIndex + 1} / {imageLength}
  264. </div>
  265. <div className="PhotoView-PhotoSlider__BannerRight">
  266. <CloseSVG
  267. className="PhotoView-PhotoSlider__Close"
  268. onTouchEnd={isMobile ? onClose : undefined}
  269. onClick={isMobile ? undefined : onClose}
  270. />
  271. </div>
  272. </div>
  273. )}
  274. {images
  275. .slice(
  276. // 加载相邻三张
  277. Math.max(photoIndex - 1, 0),
  278. Math.min(photoIndex + 2, imageLength + 1),
  279. )
  280. .map((item: dataType, index) => {
  281. // 截取之前的索引位置
  282. const realIndex =
  283. photoIndex === 0 ? photoIndex + index : photoIndex - 1 + index;
  284. return (
  285. <PhotoView
  286. key={item.key || realIndex}
  287. src={item.src}
  288. onReachTopMove={this.handleReachVerticalMove}
  289. onReachBottomMove={this.handleReachVerticalMove}
  290. onReachRightMove={
  291. realIndex < imageLength - 1
  292. ? this.handleReachHorizontalMove
  293. : undefined
  294. }
  295. onReachLeftMove={
  296. realIndex > 0 ? this.handleReachHorizontalMove : undefined
  297. }
  298. onReachUp={this.handleReachUp}
  299. onPhotoTap={this.handlePhotoTap}
  300. onMaskTap={this.handlePhotoMaskTap}
  301. photoScale={photoIndex === realIndex ? photoScale : 1}
  302. wrapClassName={viewClassName}
  303. className={imageClassName}
  304. style={{
  305. left: `${(innerWidth + horizontalOffset) * realIndex}px`,
  306. WebkitTransform: transform,
  307. transform,
  308. transition: touched
  309. ? undefined
  310. : 'transform 0.6s cubic-bezier(0.25, 0.8, 0.25, 1)',
  311. }}
  312. loadingElement={loadingElement}
  313. brokenElement={brokenElement}
  314. />
  315. );
  316. })}
  317. {introVisible && overlayIntro ? (
  318. <div
  319. className="PhotoView-PhotoSlider__FooterWrap"
  320. style={overlayStyle}
  321. >
  322. {overlayIntro}
  323. </div>
  324. ) : (
  325. undefined
  326. )}
  327. {overlayRender &&
  328. overlayRender({
  329. images,
  330. index: photoIndex,
  331. visible,
  332. onClose,
  333. onIndexChange: this.handleIndexChange,
  334. overlayVisible,
  335. })}
  336. </SlideWrap>
  337. );
  338. }
  339. return null;
  340. }
  341. }