index.jsx 2.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  1. import { Carousel } from "antd";
  2. import React, { useState, useRef, useEffect, useMemo } from "react";
  3. import { getBannerList } from "@/services/rotationChart";
  4. import VideoPlay from "./components/VideoPlay";
  5. const height = "100%";
  6. const contentStyle = {
  7. height,
  8. margin: "0 auto",
  9. width: "100%",
  10. };
  11. export default (props) => {
  12. const [height, setHeight] = useState('100%');
  13. const [current, setCurrent] = useState(0);
  14. const [mouse, setMouse] = useState(false);
  15. const [mediaList, setMediaList] = useState([]);
  16. const ref = useRef();
  17. const boxRef = useRef();
  18. const mouseRef = useRef(false);
  19. const isVideoRef = useRef(false);
  20. const isVideoPlayRef = useRef(false);
  21. const itemStyle = useMemo(() => ({
  22. height,
  23. overflow: 'hidden',
  24. width: '100%',
  25. objectFit: 'contain',
  26. }), [height]);
  27. useEffect(() => {
  28. getBannerList().then((res) => {
  29. setMediaList(
  30. res?.records?.map((x) => {
  31. if (x.type === "2") {
  32. return {
  33. ...x,
  34. type: "video",
  35. url: x.video,
  36. };
  37. } else {
  38. return {
  39. ...x,
  40. type: "image",
  41. url: x.image,
  42. };
  43. }
  44. })
  45. );
  46. });
  47. handNext();
  48. const boxHeight = boxRef.current.offsetHeight;
  49. setHeight(boxHeight ? `${boxHeight}px` : '100%');
  50. }, []);
  51. const handNext = () => {
  52. setTimeout(() => {
  53. if (!mouseRef.current && !isVideoRef.current && !isVideoPlayRef.current) {
  54. ref.current?.next();
  55. }
  56. handNext();
  57. }, 3000);
  58. };
  59. const onMouseEnter = () => {
  60. mouseRef.current = true;
  61. };
  62. const onMouseLeave = () => {
  63. mouseRef.current = false;
  64. };
  65. return (
  66. <div
  67. onMouseEnter={onMouseEnter}
  68. onMouseLeave={onMouseLeave}
  69. style={{ height, overflow: "hidden", background: '#081A48' }}
  70. ref={boxRef}
  71. >
  72. <Carousel
  73. ref={ref}
  74. afterChange={(cur) => {
  75. setCurrent(cur);
  76. }}
  77. >
  78. {mediaList?.map((x, index) => (
  79. <div key={index}>
  80. { x.type === "image" && <img src={x.url} style={itemStyle}></img> }
  81. { x.type === "video" && (
  82. <VideoPlay
  83. url={x.url}
  84. isVideoPlayRef={isVideoPlayRef}
  85. index={index}
  86. current={current}
  87. style={itemStyle}
  88. />
  89. )}
  90. </div>
  91. ))}
  92. </Carousel>
  93. </div>
  94. );
  95. };