index.tsx 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  1. import React, { useEffect, useRef, useState } from 'react'
  2. import styles from './index.module.scss'
  3. import { HomeDataRow, myData, myUrl } from '@/utils/history'
  4. import classNmaes from 'classnames'
  5. import A1info from './A1info'
  6. function A1home() {
  7. const domRef = useRef<HTMLDivElement>(null)
  8. const [moveId, setMoveId] = useState(0)
  9. const timeRef = useRef(-1)
  10. const [time, setTime] = useState(false)
  11. const timeValRef = useRef(false)
  12. useEffect(() => {
  13. timeValRef.current = time
  14. }, [time])
  15. useEffect(() => {
  16. if (moveId) {
  17. timeRef.current = window.setInterval(() => {
  18. setTime(!timeValRef.current)
  19. }, 500)
  20. } else clearInterval(timeRef.current)
  21. }, [moveId])
  22. useEffect(() => {
  23. const urlAll = window.location.href
  24. if (urlAll.includes('?m=')) {
  25. const idArr = urlAll.split('?m=')
  26. if (idArr && idArr[1]) {
  27. const id = Number(idArr[1])
  28. const index = myData.homeData.findIndex(v => v.id === id)
  29. if (index > -1 && domRef.current) {
  30. setMoveId(id)
  31. setTimeout(() => {
  32. setMoveId(0)
  33. }, 5000)
  34. domRef.current.scrollTo({
  35. top: (index - 1) * 190,
  36. behavior: 'smooth'
  37. })
  38. }
  39. }
  40. }
  41. }, [])
  42. // 打开详情
  43. const [open, setOpen] = useState({} as HomeDataRow)
  44. return (
  45. <div
  46. className={styles.A1home}
  47. style={{ backgroundImage: `url(${myUrl + myData.homeBg})` }}
  48. >
  49. <div className='homeTitleImg'>
  50. <img src={myUrl + myData.homeTitleImg} alt='' />
  51. </div>
  52. <div className='A1main' ref={domRef}>
  53. {myData.homeData.map(v => (
  54. <div
  55. onClick={() => {
  56. setOpen(v)
  57. setMoveId(0)
  58. }}
  59. className={classNmaes('A1row', moveId === v.id ? 'A1rowAcMove' : '')}
  60. key={v.id}
  61. >
  62. <div className='A1Rimg'>
  63. <img src={myUrl + v.cover} alt='' />
  64. </div>
  65. <div className='A1Rtxt'>
  66. <div
  67. className={classNmaes('A1Rtxt1')}
  68. style={{ backgroundImage: `url(${myUrl + myData.nameImg})` }}
  69. >
  70. {v.name}
  71. </div>
  72. <div className='A1Rtxt2'>
  73. <div>{v.txt}</div>
  74. </div>
  75. </div>
  76. </div>
  77. ))}
  78. </div>
  79. {/* 打开详情 */}
  80. {open.id ? <A1info info={open} closeFu={() => setOpen({} as HomeDataRow)} /> : null}
  81. </div>
  82. )
  83. }
  84. const MemoA1home = React.memo(A1home)
  85. export default MemoA1home