index.tsx 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  1. import React, { useCallback, useEffect, useMemo, useRef, useState } from 'react'
  2. import styles from './index.module.scss'
  3. import { baseURL, myData } from '@/utils/http'
  4. import { GoodsKeyType, GoodsType } from '@/types'
  5. import classNames from 'classnames'
  6. import HotInfo from './HotInfo'
  7. const txtBg = {
  8. 全部: 'more/txtBg/1quanbu.png',
  9. 车骑拜谒: 'more/txtBg/2cheqi.png',
  10. 主客宴请: 'more/txtBg/3zhuke.png',
  11. 乐舞百戏: 'more/txtBg/4yuewu.png',
  12. 后厨备宴: 'more/txtBg/5houchu.png',
  13. 田间耕作: 'more/txtBg/6tianjian.png',
  14. 仙居世界: 'more/txtBg/7xianju.png'
  15. }
  16. type Props = {
  17. hidden: boolean
  18. }
  19. const data = myData.more['文物欣赏']
  20. function S3goods({ hidden }: Props) {
  21. const [key, setKey] = useState<GoodsKeyType>('全部')
  22. useEffect(() => {
  23. if (sroolRef.current) {
  24. sroolRef.current.scrollLeft = 0
  25. }
  26. }, [key])
  27. const listAll = useMemo(() => {
  28. let arr: {
  29. name: GoodsKeyType
  30. info: GoodsType[]
  31. }[] = []
  32. let temp: GoodsType[] = []
  33. data.dataArr.forEach(v => {
  34. arr.push(v)
  35. v.info.forEach(c => {
  36. temp.push(c)
  37. })
  38. })
  39. arr.unshift({
  40. name: '全部',
  41. info: temp
  42. })
  43. return arr
  44. }, [])
  45. // 滚轮和拖动
  46. const [isFlag, setIsFlag] = useState(false)
  47. const sroolRef = useRef<HTMLDivElement>(null)
  48. const mousemoveFu = useCallback(
  49. (ev: any, flag?: boolean) => {
  50. if (sroolRef.current) {
  51. if (flag && !isFlag) {
  52. const nowMove = sroolRef.current.scrollLeft
  53. // 滚轮
  54. let num = 50
  55. if (ev.deltaY < 0) num = -num
  56. sroolRef.current.scrollLeft = nowMove + num
  57. } else if (isFlag) {
  58. const nowMove = sroolRef.current.scrollLeft
  59. // 鼠标按住移动
  60. sroolRef.current.scrollLeft = nowMove - ev.movementX
  61. }
  62. }
  63. },
  64. [isFlag]
  65. )
  66. const row2Bac = useCallback((index2: number) => {
  67. const num = index2 % 3
  68. return `${baseURL}more/txtBg/ban${num + 1}.jpg`
  69. }, [])
  70. // 打开详情
  71. const [openInfo, setOpenInfo] = useState({} as GoodsType)
  72. return (
  73. <div
  74. hidden={hidden}
  75. className={styles.S3goods}
  76. style={{ backgroundImage: `url(${baseURL + data.bg})`, zIndex: openInfo.name ? 50 : 1 }}
  77. >
  78. <div className='S3main' hidden={!!openInfo.name}>
  79. {listAll.map((item1, index1) => (
  80. <div className={classNames('S3row', key === item1.name ? 'S3rowShow' : '')} key={index1}>
  81. {/* 一直有的 */}
  82. <div
  83. onClick={() => setKey(item1.name)}
  84. className='S3rowTit'
  85. style={{ backgroundImage: `url(${baseURL + Reflect.get(txtBg, item1.name)})` }}
  86. >
  87. <div>{item1.name}</div>
  88. </div>
  89. {/* 展开的 */}
  90. <div
  91. className='S3rowZhan'
  92. style={{ cursor: isFlag ? 'move' : 'pointer' }}
  93. ref={key === item1.name ? sroolRef : null}
  94. onMouseDown={() => setIsFlag(true)}
  95. onMouseUp={() => setIsFlag(false)}
  96. onMouseLeave={() => setIsFlag(false)}
  97. onMouseMove={e => mousemoveFu(e)}
  98. onWheel={e => mousemoveFu(e, true)}
  99. >
  100. {item1.info.map((item2, index2) => (
  101. <div
  102. onClick={() => setOpenInfo(item2)}
  103. className='S3zRow sizeNo'
  104. key={index2}
  105. style={{ backgroundImage: `url(${row2Bac(index2)})` }}
  106. >
  107. {item2.name}
  108. {/* 上下的线 待完善 */}
  109. {/* <div className='S3zRxian'></div>
  110. <div className='S3zRxian S3zRxian2'></div> */}
  111. {/* 右下角图片 */}
  112. <img className='S3zRimg' src={baseURL + item2.suoSrc} alt='' />
  113. </div>
  114. ))}
  115. </div>
  116. </div>
  117. ))}
  118. </div>
  119. {/* 打开热点详情 */}
  120. {openInfo.name ? (
  121. <HotInfo info={openInfo} closeFu={() => setOpenInfo({} as GoodsType)} />
  122. ) : null}
  123. </div>
  124. )
  125. }
  126. const MemoS3goods = React.memo(S3goods)
  127. export default MemoS3goods