index.tsx 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  1. import React, { useCallback, useEffect, useRef, useState } from 'react'
  2. import styles from './index.module.scss'
  3. import history, { HomeDataRow, myData, myUrl } from '@/utils/history'
  4. import classNmaes from 'classnames'
  5. import A1info from './A1info'
  6. import { imgInitFu } from './data'
  7. import useMove from '@/components/useMove'
  8. declare global {
  9. //设置全局属性
  10. interface Window {
  11. //window对象属性
  12. getIndexFu: (index: number) => void
  13. moveFu: (val: any) => void
  14. }
  15. }
  16. function A1home() {
  17. // 当前索引
  18. const [index, setIndex] = useState(-1)
  19. useEffect(() => {
  20. window.getIndexFu = index => {
  21. setIndex(index)
  22. // console.log('当前索引', index)
  23. }
  24. }, [])
  25. useEffect(() => {
  26. imgInitFu()
  27. }, [])
  28. const moveRef = useRef<any>(null)
  29. useEffect(() => {
  30. const urlAll = window.location.href
  31. if (urlAll.includes('?m=')) {
  32. const idArr = urlAll.split('?m=')
  33. if (idArr && idArr[1]) {
  34. const id = Number(idArr[1])
  35. const index = myData.homeData.findIndex(v => v.id === id)
  36. // console.log('返回的索引', index)
  37. window.moveFu = val => {
  38. moveRef.current = val
  39. if ([1, 2, 3, 4, 5, 6].includes(index)) val.go(-index)
  40. else if ([7, 8, 9, 10, 11].includes(index)) {
  41. const obj = {
  42. 11: 1,
  43. 10: 2,
  44. 9: 3,
  45. 8: 4,
  46. 7: 5
  47. }
  48. val.go(Reflect.get(obj, index))
  49. }
  50. }
  51. }
  52. } else {
  53. window.moveFu = val => {
  54. moveRef.current = val
  55. }
  56. }
  57. }, [])
  58. // 相关文物的滚动
  59. const { touchstart, touchmove, touchend, MouseLeave, isPcMoveFlag } = useMove()
  60. const moveFu = useCallback((val: number) => {
  61. moveRef.current.go(val)
  62. }, [])
  63. // 点击进入详情页
  64. const toInfoPage = useCallback(() => {
  65. if (isPcMoveFlag) {
  66. const id = myData.homeData[index].id
  67. history.replace(`/info/${id}`)
  68. }
  69. }, [index, isPcMoveFlag])
  70. // 打开详情
  71. const [open, setOpen] = useState({} as HomeDataRow)
  72. return (
  73. <div
  74. className={styles.A1home}
  75. style={{ backgroundImage: `url(${myUrl + myData.homeBg})` }}
  76. >
  77. <div className='homeTitleImg'>
  78. <img src={myUrl + myData.homeTitleImg} alt='' />
  79. </div>
  80. <div className='box11'>
  81. <div className='wrap'>
  82. <div id='showcase' className='noselect'>
  83. {myData.homeData.map((v, i) => (
  84. <img
  85. key={v.id}
  86. className={classNmaes('cloud9-item', i === index ? 'imgAc' : '')}
  87. src={myUrl + v.cover}
  88. alt=''
  89. />
  90. ))}
  91. </div>
  92. </div>
  93. </div>
  94. {/* <div id='nav' className='noselect'>
  95. <button className='left'>←</button>
  96. <button className='right'>→</button>
  97. </div> */}
  98. <div className='A1Text'></div>
  99. {/* 左右滑动 */}
  100. <div
  101. className='A1MoveBox'
  102. onTouchStart={e => touchstart(e.touches[0].pageX)}
  103. onTouchMove={e => touchmove(e.touches[0].pageX)}
  104. onTouchEnd={e => touchend(val => moveFu(val), 'mobile')}
  105. onClick={toInfoPage}
  106. onMouseUp={e => touchstart(e.clientX)}
  107. onMouseMove={e => touchmove(e.clientX)}
  108. onMouseDown={e => touchend(val => moveFu(val), 'pc')}
  109. onMouseLeave={MouseLeave}
  110. ></div>
  111. {/* 打开详情 */}
  112. {open.id ? <A1info info={open} closeFu={() => setOpen({} as HomeDataRow)} /> : null}
  113. </div>
  114. )
  115. }
  116. const MemoA1home = React.memo(A1home)
  117. export default MemoA1home