|
@@ -1,4 +1,4 @@
|
1
|
|
-import React, { useEffect, useMemo, useRef, useState } from 'react';
|
|
1
|
+import React, { useCallback, useEffect, useRef } from 'react';
|
2
|
2
|
import { Swiper, SwiperSlide } from 'swiper/react';
|
3
|
3
|
import { Autoplay } from 'swiper';
|
4
|
4
|
import 'swiper/css';
|
|
@@ -7,14 +7,32 @@ import Item from './Item';
|
7
|
7
|
import Styles from './style.less';
|
8
|
8
|
|
9
|
9
|
export default (props) => {
|
10
|
|
- // 每次加载 4 个
|
11
|
|
- const tm = 4;
|
12
|
10
|
const { title, color, children } = props;
|
13
|
|
-
|
|
11
|
+ const swiperRef = useRef();
|
14
|
12
|
const classList = classNames(Styles['list-title'], {
|
15
|
13
|
[Styles['yellow-title']]: color !== 'green',
|
16
|
14
|
[Styles['green-title']]: color === 'green',
|
17
|
15
|
});
|
|
16
|
+ const initAutoPlay = useCallback(
|
|
17
|
+ (swiper) => {
|
|
18
|
+ console.log(swiper);
|
|
19
|
+ if (!children?.length || children?.length <= 3) {
|
|
20
|
+ swiper.autoplay.stop();
|
|
21
|
+ } else {
|
|
22
|
+ swiper.autoplay.start();
|
|
23
|
+ }
|
|
24
|
+ },
|
|
25
|
+ [children?.length],
|
|
26
|
+ );
|
|
27
|
+
|
|
28
|
+ useEffect(() => {
|
|
29
|
+ if (swiperRef.current) {
|
|
30
|
+ initAutoPlay(swiperRef.current);
|
|
31
|
+ }
|
|
32
|
+ }, [children?.length, initAutoPlay]);
|
|
33
|
+
|
|
34
|
+ const enable = children?.length && children?.length > 3;
|
|
35
|
+
|
18
|
36
|
return (
|
19
|
37
|
<div className={Styles['screen-list']}>
|
20
|
38
|
<div className={classList}>
|
|
@@ -22,12 +40,17 @@ export default (props) => {
|
22
|
40
|
</div>
|
23
|
41
|
<Swiper
|
24
|
42
|
height={48}
|
|
43
|
+ onSwiper={(swiper) => {
|
|
44
|
+ swiperRef.current = swiper;
|
|
45
|
+ initAutoPlay(swiper);
|
|
46
|
+ }}
|
25
|
47
|
className={Styles['list-body']}
|
|
48
|
+ init={false}
|
26
|
49
|
autoplay={{
|
27
|
50
|
delay: 2500,
|
28
|
51
|
disableOnInteraction: false,
|
29
|
52
|
}}
|
30
|
|
- loop={children?.length >= 3}
|
|
53
|
+ loop={enable}
|
31
|
54
|
loopAdditionalSlides={2}
|
32
|
55
|
modules={[Autoplay]}
|
33
|
56
|
direction="vertical"
|