• React 轮播图


    Index.js:

    1. import React from 'react'
    2. import { connect } from 'react-redux'
    3. import { withRouter } from 'react-router-dom'
    4. import useList from './useList'
    5. import { Swiper, SwiperSlide } from 'swiper/react'
    6. import { Pagination } from 'swiper/modules'
    7. import { SinglePageHeader } from '../../../../components/light'
    8. import { Button, Skeleton } from 'antd'
    9. import LazyLoad from 'react-lazy-load'
    10. import 'swiper/css'
    11. import 'swiper/css/pagination'
    12. import './index.css'
    13. function Index(props) {
    14. const {
    15. config,
    16. models,
    17. userInfo,
    18. routerSearchObj,
    19. imgList,
    20. handleJumpPage,
    21. handleTagClick,
    22. handleModelClick,
    23. handleImgDrawSameStyleClick,
    24. } = useList(props)
    25. return (
    26. <div className="m-welcome-wrap-box">
    27. <div className={`m-welcome-wrap-box-inner`}>
    28. <div className="m-welcome-wrap">
    29. {typeof routerSearchObj.type === 'undefined' ||
    30. routerSearchObj.type === '0' ? (
    31. <div className="m-welcome-header">
    32. <div className="m-welcome-header-title">学习div>
    33. <div className="m-welcome-header-info">div>
    34. <div className="m-welcome-header-right">
    35. {userInfo.avatarCdn ? (
    36. <img
    37. src={userInfo.avatarCdn}
    38. className="m-welcome-avatar"
    39. onClick={() => handleJumpPage('/ai/index/home/chatList')}
    40. alt="头像"
    41. >img>
    42. ) : (
    43. <>
    44. <Button
    45. type="link"
    46. className="m-welcome-btn-text"
    47. onClick={() => handleJumpPage('/ai/login')}
    48. >
    49. 登录
    50. Button>
    51. <Button
    52. type="link"
    53. className="m-welcome-btn-text"
    54. onClick={() => handleJumpPage('/ai/register')}
    55. >
    56. 注册
    57. Button>
    58. )}
    59. div>
    60. div>
    61. ) : (
    62. <SinglePageHeader
    63. // goBackPath="/ai/index/home/chatList"
    64. title={'首页&AI绘画'}
    65. >SinglePageHeader>
    66. )}
    67. <Swiper
    68. slidesPerView={1.1}
    69. spaceBetween={10}
    70. centeredSlides={true}
    71. pagination={{
    72. clickable: true,
    73. }}
    74. modules={[Pagination]}
    75. initialSlide={1}
    76. className="mySwiper"
    77. >
    78. {Array.isArray(config.carousel) && config.carousel.length > 0 ? (
    79. config.carousel.map((item) => (
    80. <SwiperSlide
    81. key={item.id}
    82. className={`m-welcome-carousel-item ${
    83. typeof routerSearchObj.type === 'undefined' ||
    84. routerSearchObj.type === '0'
    85. ? 'withBorder'
    86. : ''
    87. }`}
    88. >
    89. <div className="m-welcome-carousel-item-info">
    90. <div
    91. className="m-welcome-carouse-item-tag"
    92. onClick={() => handleTagClick(item)}
    93. >
    94. {item.tag}
    95. div>
    96. <div className="m-welcome-carouse-item-title">
    97. {item.title}
    98. div>
    99. <div className="m-welcome-carouse-item-sub-title">
    100. {item.subTitle}
    101. div>
    102. div>
    103. <div className="m-welcome-carousel-item-img-wrap">
    104. <div
    105. className="m-welcome-carousel-item-img"
    106. style={{ backgroundImage: `url(${item.imgUrl})` }}
    107. >div>
    108. div>
    109. SwiperSlide>
    110. ))
    111. ) : (
    112. <SwiperSlide>
    113. <Skeleton
    114. avatar
    115. paragraph={{
    116. rows: 7,
    117. }}
    118. active
    119. className="m-h5-lesson-play-skeleton"
    120. />
    121. SwiperSlide>
    122. )}
    123. Swiper>
    124. {/* models */}
    125. <div className="m-welcome-card-wrap">
    126. <div className="m-welcome-card-header-wrap">
    127. <div className="m-welcome-card-title-wrap">
    128. <div className="m-welcome-card-title">绘图大模型div>
    129. <div
    130. className="m-welcome-card-more"
    131. onClick={() => handleJumpPage('/welcome/home/modelList')}
    132. >
    133. 查看全部
    134. div>
    135. div>
    136. <div className="m-welcome-card-sub-title">向左滑动 查看更多div>
    137. div>
    138. <Swiper
    139. slidesPerView={1.1}
    140. spaceBetween={10}
    141. centeredSlides={true}
    142. pagination={false}
    143. modules={[Pagination]}
    144. className="mySwiper"
    145. >
    146. {Array.isArray(models) && models.length > 0 ? (
    147. models.map((item) => (
    148. <SwiperSlide
    149. key={item.first.id}
    150. className={`m-welcome-card-item`}
    151. >
    152. <div className={`m-welcome-card-item-row`}>
    153. <LazyLoad className="m-welcome-card-item-img-wrap">
    154. <img
    155. src={item.first.avatar}
    156. className="m-welcome-card-item-img"
    157. alt="头像"
    158. >img>
    159. LazyLoad>
    160. <div className="m-welcome-card-item-info-wrap withBorder">
    161. <div className="m-welcome-card-item-info">
    162. <div
    163. className="m-welcome-card-item-title m-ellipsis"
    164. title={item.first.name}
    165. >
    166. {item.first.name}
    167. div>
    168. <div className="m-welcome-card-item-sub-title">
    169. {item.first.id}
    170. div>
    171. div>
    172. <div
    173. className="m-welcome-card-item-btn"
    174. onClick={() => handleModelClick(item.first)}
    175. >
    176. 画同款
    177. div>
    178. div>
    179. div>
    180. {item.second ? (
    181. <div className={`m-welcome-card-item-row`}>
    182. <LazyLoad className="m-welcome-card-item-img-wrap">
    183. <img
    184. src={item.second.avatar}
    185. className="m-welcome-card-item-img"
    186. alt="头像"
    187. >img>
    188. LazyLoad>
    189. <div className="m-welcome-card-item-info-wrap withBorder">
    190. <div className="m-welcome-card-item-info">
    191. <div
    192. className="m-welcome-card-item-title m-ellipsis"
    193. title={item.second.name}
    194. >
    195. {item.second.name}
    196. div>
    197. <div className="m-welcome-card-item-sub-title">
    198. {item.second.id}
    199. div>
    200. div>
    201. <div
    202. className="m-welcome-card-item-btn"
    203. onClick={() => handleModelClick(item.second)}
    204. >
    205. 画同款
    206. div>
    207. div>
    208. div>
    209. ) : null}
    210. {item.third ? (
    211. <div className={`m-welcome-card-item-row`}>
    212. <LazyLoad className="m-welcome-card-item-img-wrap">
    213. <img
    214. src={item.third.avatar}
    215. className="m-welcome-card-item-img"
    216. alt="头像"
    217. >img>
    218. LazyLoad>
    219. <div className="m-welcome-card-item-info-wrap">
    220. <div className="m-welcome-card-item-info">
    221. <div
    222. className="m-welcome-card-item-title m-ellipsis"
    223. title={item.third.name}
    224. >
    225. {item.third.name}
    226. div>
    227. <div className="m-welcome-card-item-sub-title">
    228. {item.third.id}
    229. div>
    230. div>
    231. <div
    232. className="m-welcome-card-item-btn"
    233. onClick={() => handleModelClick(item.third)}
    234. >
    235. 画同款
    236. div>
    237. div>
    238. div>
    239. ) : null}
    240. SwiperSlide>
    241. ))
    242. ) : (
    243. <SwiperSlide>
    244. <Skeleton
    245. avatar
    246. paragraph={{
    247. rows: 7,
    248. }}
    249. active
    250. className="m-h5-lesson-play-skeleton"
    251. />
    252. SwiperSlide>
    253. )}
    254. Swiper>
    255. div>
    256. <div className="m-welcome-card-wrap">
    257. <div className="m-welcome-card-header-wrap">
    258. <div className="m-welcome-card-title-wrap">
    259. <div className="m-welcome-card-title">AI绘画作品展示div>
    260. <div
    261. className="m-welcome-card-more"
    262. onClick={() => handleJumpPage('/welcome/home/imgList')}
    263. >
    264. 查看全部
    265. div>
    266. div>
    267. <div className="m-welcome-card-sub-title">向左滑动 查看更多div>
    268. div>
    269. <Swiper
    270. slidesPerView={1.1}
    271. spaceBetween={10}
    272. centeredSlides={true}
    273. pagination={false}
    274. modules={[Pagination]}
    275. className="mySwiper"
    276. >
    277. {Array.isArray(imgList) && imgList.length > 0 ? (
    278. imgList.map((item) => (
    279. <SwiperSlide
    280. key={item.first.imgUid}
    281. className={`m-welcome-card-item`}
    282. >
    283. <div className='m-welcome-img-row-wrap'>
    284. <div className={`m-welcome-img-row`}>
    285. <LazyLoad className="m-welcome-img-lazy-load first">
    286. <div className="m-welcome-img-warp">
    287. <img
    288. src={item.first.imgUrlCdn}
    289. className="m-welcome-img"
    290. alt="图片"
    291. onClick={() =>
    292. handleImgDrawSameStyleClick(item.first)
    293. }
    294. >img>
    295. div>
    296. LazyLoad>
    297. <LazyLoad className="m-welcome-img-lazy-load">
    298. <div className="m-welcome-img-warp">
    299. <img
    300. src={item.second.imgUrlCdn}
    301. className="m-welcome-img"
    302. alt="图片"
    303. onClick={() =>
    304. handleImgDrawSameStyleClick(item.second)
    305. }
    306. >img>
    307. div>
    308. LazyLoad>
    309. div>
    310. {/* <div className={`m-welcome-img-row`}>
    311. <LazyLoad className="m-welcome-img-lazy-load">
    312. <div className="m-welcome-img-warp">
    313. <img
    314. src={item.third.imgUrlCdn}
    315. className="m-welcome-img"
    316. alt="图片"
    317. onClick={() =>
    318. handleImgDrawSameStyleClick(item.third)
    319. }
    320. >img>
    321. div>
    322. LazyLoad>
    323. <LazyLoad className="m-welcome-img-lazy-load">
    324. <div className="m-welcome-img-warp">
    325. <img
    326. src={item.fourth.imgUrlCdn}
    327. className="m-welcome-img"
    328. alt="图片"
    329. onClick={() =>
    330. handleImgDrawSameStyleClick(item.fourth)
    331. }
    332. >img>
    333. div>
    334. LazyLoad>
    335. div> */}
    336. div>
    337. SwiperSlide>
    338. ))
    339. ) : (
    340. <SwiperSlide>
    341. <Skeleton
    342. avatar
    343. paragraph={{
    344. rows: 7,
    345. }}
    346. active
    347. className="m-h5-lesson-play-skeleton"
    348. />
    349. SwiperSlide>
    350. )}
    351. Swiper>
    352. div>
    353. div>
    354. div>
  • )
  • }
  • const mapStateToProps = (state) => {
  • return {
  • collapsed: state.getIn(['light', 'collapsed']),
  • }
  • }
  • const mapDispatchToProps = (dispatch) => {
  • return {
  • onSetState(key, value) {
  • dispatch({ type: 'SET_LIGHT_STATE', key, value })
  • },
  • onDispatch(action) {
  • dispatch(action)
  • },
  • }
  • }
  • export default connect(mapStateToProps, mapDispatchToProps)(withRouter(Index))
  • useList.js:

    1. import { useState, useEffect } from 'react'
    2. import Api from '../../../../api'
    3. import { getRouterSearchObj } from '../../../../utils/tools'
    4. export default function useList(props) {
    5. const [config, setConfig] = useState({})
    6. const [models, setModels] = useState([])
    7. const [userInfo, setUserInfo] = useState({})
    8. const [imgList, setImgList] = useState([])
    9. //获取路由参数
    10. const routerSearchObj = getRouterSearchObj(props)
    11. const handleGetConfig = () => {
    12. Api.h5.configWelcome().then((res) => {
    13. if (res.code === 200) {
    14. setConfig(res.data.config)
    15. let models = res.data.config.models
    16. if (Array.isArray(models)) {
    17. let tempModels = []
    18. for (let i = 0; i < models.length; i = i + 3) {
    19. tempModels = [
    20. ...tempModels,
    21. { first: models[i], second: models[i + 1], third: models[i + 2] },
    22. ]
    23. }
    24. setModels(tempModels)
    25. }
    26. }
    27. })
    28. }
    29. const handleImgSearch = () => {
    30. Api.h5
    31. .sdImgSearch({
    32. pageNum: 1,
    33. pageSize: 40,
    34. })
    35. .then((res) => {
    36. if (res.code === 200) {
    37. let list = res.data.list
    38. if (Array.isArray(list)) {
    39. let tempModels = []
    40. for (let i = 0; i < list.length; i = i + 2) {
    41. tempModels = [
    42. ...tempModels,
    43. {
    44. first: list[i],
    45. second: list[i + 1],
    46. // third: list[i + 2],
    47. // fourth: list[i + 3],
    48. },
    49. ]
    50. }
    51. setImgList(tempModels)
    52. }
    53. }
    54. })
    55. }
    56. const getUserInfo = () => {
    57. let token = localStorage.getItem('token')
    58. if (token) {
    59. Api.h5.userGetInfo({ isLoading: false }).then((res) => {
    60. if (res.code === 200) {
    61. setUserInfo(res.data)
    62. } else {
    63. }
    64. })
    65. }
    66. }
    67. //跳转
    68. const handleJumpPage = (path) => {
    69. // eslint-disable-next-line
    70. props.history.push(path)
    71. }
    72. const handleTagClick = () => {
    73. if (userInfo.avatarCdn) {
    74. props.history.push('/ai/index/home/chatList')
    75. } else {
    76. props.history.push('/ai/login')
    77. }
    78. }
    79. const handleModelClick = (item) => {
    80. props.history.push(
    81. `/single/home/sdSimple?modelId=${item.id}&name=${item.name}&link=${item.link}`
    82. )
    83. }
    84. const handleImgDrawSameStyleClick = (item) => {
    85. console.log(item)
    86. props.history.push(
    87. `/single/home/sdSimple?modelId=${item.id}&name=${item.name}&link=${item.link}&imgUid=${item.imgUid}`
    88. )
    89. }
    90. useEffect(() => {
    91. handleGetConfig()
    92. getUserInfo()
    93. handleImgSearch()
    94. // eslint-disable-next-line
    95. }, [])
    96. return {
    97. config,
    98. models,
    99. userInfo,
    100. routerSearchObj,
    101. imgList,
    102. handleJumpPage,
    103. handleTagClick,
    104. handleModelClick,
    105. handleImgDrawSameStyleClick,
    106. }
    107. }

    index.css:

    1. .m-welcome-wrap-box{display: flex;justify-content: center;background: #ddd;background: #ddd;position: absolute;top: 0;left: 0;right: 0;bottom: 0;overflow: hidden;}
    2. .m-welcome-wrap-box-inner{position: relative; display: flex;flex-direction: column; max-width: 800px;width: 100%;background: #ededed;}
    3. .m-welcome-wrap{position: absolute;top: 0;bottom: 0;left: 0;right: 0; padding: 0 0 100px 0; overflow-y: auto;}
    4. .m-welcome-header{display: flex; padding: 20px calc(4.5%) 0 calc(4.5%);align-items:center;margin: 0 0 5px 0;}
    5. .m-welcome-header-title{font-size: 30px;font-weight: bold;}
    6. .m-welcome-header-info{flex: 1}
    7. .m-welcome-header-right{display: flex;align-items:center;}
    8. .m-welcome-avatar{width: 40px;height: 40px;border-radius: 5px;cursor: pointer;}
    9. .m-welcome-btn-text{margin: 0 5px 0 0;padding: 4px 0;}
    10. .m-welcome-carousel-item{padding: 5px 0 0 0;}
    11. .m-welcome-carousel-item.withBorder{border-top: 1px solid #ddd;}
    12. .m-welcome-carousel-item-info{padding: 5px 0;}
    13. .m-welcome-carouse-item-tag{font-size: 12px;color: #1677ff; height: 16px;line-height: 16px;cursor: pointer;}
    14. .m-welcome-carouse-item-tag:hover{color: #0d6ef7;}
    15. .m-welcome-carouse-item-title{font-size: 24px;color: #000; height: 32px;}
    16. .m-welcome-carouse-item-sub-title{font-size: 22px;color: #999;height: 26px;}
    17. .m-welcome-carousel-item-img-wrap{position: relative; width: 100%;padding: 64% 0 0 0;}
    18. .m-welcome-carousel-item-img{position: absolute;top: 0;bottom: 0;left: 0;right: 0;border-radius: 5px; background-size: 100% 100%;}
    19. .m-welcome-card-header-wrap{border-top: 1px solid #ddd; margin: 20px calc(4.5%) 0 calc(4.5%);}
    20. .m-welcome-card-title-wrap{display: flex;align-items: center;padding: 5px 0 2px;}
    21. .m-welcome-card-title{font-size: 22px;font-weight: bold;flex: 1;}
    22. .m-welcome-card-more{font-size: 14px;color: #1677ff;cursor: pointer;}
    23. .m-welcome-card-more:hover{color: #0d6ef7;}
    24. .m-welcome-card-sub-title{font-size: 16px;color: #999;}
    25. .m-welcome-card-item-row{display: flex;padding: 5px 0;align-items: center;}
    26. .m-welcome-card-item-img-wrap{display: flex; width: 50px;height: 64px;background: #dddddd;}
    27. .m-welcome-card-item-img-wrap::before{content: '';margin: auto;width: 22px;height: 22px;background-image: url(../../../../static/images/loading.png);background-size: 100% 100%; animation: loading 0.5s linear infinite;}
    28. .m-welcome-card-item-img{position: absolute; width: 50px;height: 64px;border-radius: 5px;object-fit: cover;}
    29. .m-welcome-card-item-info-wrap{height: 64px; flex: 1;display: flex;align-items: center; margin: 0 0 0 5px;overflow: hidden;}
    30. .m-welcome-card-item-info-wrap.withBorder{border-bottom: 1px solid #ddd;}
    31. .m-welcome-card-item-info{flex:1;padding: 0 4px 0 0; display:flex;flex-direction: column;justify-content: center;overflow: hidden;}
    32. .m-welcome-card-item-title{font-size: 18px;color: #000;font-weight: bold;margin: 0 0 2px 0;}
    33. .m-welcome-card-item-sub-title{font-size: 14px;color: #999;}
    34. .m-welcome-card-item-btn{padding: 0 20px;background: #ddd;color: #1677ff;font-weight: bold;letter-spacing: 2px;font-size: 16px; border-radius: 15px;text-align: center;height: 30px; line-height: 32px;cursor: pointer;}
    35. .m-welcome-card-item-btn:hover{box-shadow: 0 0 5px rgba(0, 0, 0, 0.3) ;}
    36. .m-welcome-img-row-wrap{padding: 5px 0 0;}
    37. .m-welcome-img-row{display: flex;align-items: center;}
    38. .m-welcome-img-lazy-load{display: flex; position: relative; flex: 1; padding: 75% 0 0;border-radius: 5px; background: #dddddd;}
    39. .m-welcome-img-lazy-load.first{margin: 0 5px 0 0;}
    40. /* .m-welcome-card-item-img-big-wrap::before{content: '';margin: auto;width: 22px;height: 22px;background-image: url(../../../../static/images/loading.png);background-size: 100% 100%; animation: loading 0.5s linear infinite;} */
    41. .m-welcome-img-warp{position: absolute;top: 0;left: 0;right: 0;bottom: 0;}
    42. .m-welcome-img{width: 100%;height: 100%;object-fit: cover;border-radius: 5px;}

    效果图:

    https://swiperjs.com/demos

    参考链接:

    https://chat.xutongbao.top/

  • 相关阅读:
    JavaScript系列从入门到精通系列第十五篇:JavaScript中函数的实参介绍返回值介绍以及函数的立即执行
    【附源码】计算机毕业设计SSM腾讯网游辅助小助手
    httpprompt.ml靶场练习
    【iOS】ViewController的生命周期
    学习经验分享之十二:如何撰写论文
    .NET 发布,部署和运行应用程序
    JDBC知识点
    Azure Data Factory(十一)Data Flow 的使用解析
    Flutter Event 派发
    数据结构高阶--AVL(平衡二叉树)(图解+实现)
  • 原文地址:https://blog.csdn.net/xutongbao/article/details/134039039