index.tsx 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  1. import React, { useCallback, useEffect, useRef, useState } from 'react'
  2. import styles from './index.module.scss'
  3. import history, { HomeDataRow, isMobileFu, 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. }
  53. }, [])
  54. // 相关文物的滚动
  55. const { touchstart, touchmove, touchend, MouseLeave, isPcMoveFlag } = useMove()
  56. const moveFu = useCallback((val: number) => {
  57. moveRef.current.go(val)
  58. }, [])
  59. // 点击进入详情页
  60. const toInfoPage = useCallback(() => {
  61. if (isPcMoveFlag) {
  62. const id = myData.homeData[index].id
  63. history.replace(`/info/${id}`)
  64. }
  65. }, [index, isPcMoveFlag])
  66. // 打开详情
  67. const [open, setOpen] = useState({} as HomeDataRow)
  68. return (
  69. <div
  70. className={styles.A1home}
  71. style={{ backgroundImage: `url(${myUrl + myData.homeBg})` }}
  72. >
  73. <div className='homeTitleImg'>
  74. <img src={myUrl + myData.homeTitleImg} alt='' />
  75. </div>
  76. <div className='box11'>
  77. <div className='wrap'>
  78. <div id='showcase' className='noselect'>
  79. {myData.homeData.map((v, i) => (
  80. <img
  81. key={v.id}
  82. className={classNmaes('cloud9-item', i === index ? 'imgAc' : '')}
  83. src={myUrl + v.cover}
  84. alt=''
  85. />
  86. ))}
  87. </div>
  88. </div>
  89. </div>
  90. {/* <div id='nav' className='noselect'>
  91. <button className='left'>←</button>
  92. <button className='right'>→</button>
  93. </div> */}
  94. <div className='A1Text'></div>
  95. {/* 左右滑动 */}
  96. <div
  97. className='A1MoveBox'
  98. onTouchStart={e => touchstart(e.touches[0].pageX)}
  99. onTouchMove={e => touchmove(e.touches[0].pageX)}
  100. onTouchEnd={e => touchend(val => moveFu(val), 'mobile')}
  101. onClick={toInfoPage}
  102. onMouseUp={e => touchstart(e.clientX)}
  103. onMouseMove={e => touchmove(e.clientX)}
  104. onMouseDown={e => touchend(val => moveFu(val), 'pc')}
  105. onMouseLeave={MouseLeave}
  106. ></div>
  107. {/* 打开详情 */}
  108. {open.id ? <A1info info={open} closeFu={() => setOpen({} as HomeDataRow)} /> : null}
  109. </div>
  110. )
  111. }
  112. const MemoA1home = React.memo(A1home)
  113. export default MemoA1home