gpt4 book ai didi

javascript - React Slick 有没有办法在 Carousel 中设置 Carousel?

转载 作者:行者123 更新时间:2023-12-03 07:13:10 33 4
gpt4 key购买 nike

React Slick 有没有办法在 Carousel 中包含 Carousel?

import Slider from "react-slick";

<Slider
{...settings}
>
<div/>
<div>
<Slider
{...settings}
>
...
</Slider>
</div>
<div/>
</Slider>

我试过这种代码,但它完全弄乱了两个轮播。

我不需要滑动、点或箭头,使用 slickGoTo 可以完全控制轮播。

最佳答案

我解决了我的问题,我们可以使用此组件有 2 级多个嵌套 slider :

旋转木马

import React, {useEffect, useRef, useState} from 'react';
import PropTypes from 'prop-types';
import Slider from 'react-slick';

import 'slick-carousel/slick/slick.css';
import './carousel.scss';

function isInt(value) {
return !isNaN(value) && (function (x) {
return (x | 0) === x;
})(parseFloat(value))
}

function resize() {
// Trigger resize (IE compatible)
if (navigator.userAgent.indexOf('MSIE') !== -1 || navigator.appVersion.indexOf('Trident/') > 0) {
let event = document.createEvent('UIEvents');
event.initUIEvent('resize', true, false, window, 0);
window.dispatchEvent(event);
} else {
window.dispatchEvent(new Event('resize'));
}
}

function Carousel(props) {
const {children, className, initialSlide, onChange, current, speed, ...rest} = props;

const sliderRef = useRef(null);

useEffect(() => {
initialSlide && setStep(initialSlide)
}, [initialSlide]);

useEffect(() => {
isInt(current) && setStep(current);
}, [current]);

const [step, setStep] = useState(initialSlide || 0);
useEffect(() => {
step < 0 && setStep(0);
step > children.length && setStep(children.length);

sliderRef.current.slickGoTo(step);
onChange && onChange(step);
}, [step]);

const settings = {
accessibility: false,
adaptiveHeight: false,
arrows: false,
className: className,
dots: false,
infinite: false,
initialSlide: initialSlide || 0,
slideIndex: initialSlide || 0,
slidesToScroll: 1,
slidesToShow: 1,
speed: speed || 500,
swipe: false
};

const handleBeforeChange = (_, index) => {
onChange && onChange(index);

// This setTimeout is needed for adaptive height in nested Carousel
setTimeout(() => {
resize();
}, speed || 500);
};

return (
<Slider
beforeChange={handleBeforeChange}
ref={sliderRef}
{...settings}
{...rest}
>
{React.Children.map(children, (child, index) => (
child
? <React.Fragment
key={`slide-${index}`}
>
{React.cloneElement(child, {
step: step,
setStep: setStep
})}
</React.Fragment>
: () => {
}
)
)}
</Slider>
);
}

Carousel.propTypes = {
className: PropTypes.string,
current: PropTypes.number,
initialSlide: PropTypes.number,
speed: PropTypes.number,
onChange: PropTypes.func
};

export default Carousel;

adaptiveHeight 的调整大小在动画开始和结束时处理。

像这样使用它:

<Carousel adaptiveHeight speed={1000} {...props}>
<Component/>
<Carousel adaptiveHeight speed={750} {...props}>
<Component/>
<Component/>
<Component/>
<Component/>
</Carousel/>
<Component/>
</Carousel>

/!\你不能在 <input> 上使用 autoFocus 属性在你的 slider 内(第 1 步除外)

关于javascript - React Slick 有没有办法在 Carousel 中设置 Carousel?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/63079226/

33 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com