简体   繁体   English

访问 react-slick 幻灯片的幻灯片元素

[英]Accessing slide elements of react-slick slideshow

I'm adding a slideshow using react-slick and implementing my own lazy-loading routine so that I can pre-load images a slide ahead (eventually 2 ahead).我正在使用 react-slick 添加幻灯片并实现我自己的延迟加载例程,以便我可以预先加载图像(最终提前 2 个)。 In order to do this, I need to access the DOM element containing the list of slides wrapped by Slick.为此,我需要访问包含由 Slick 包装的幻灯片列表的 DOM 元素。 Once I have that element, I should easily be able to use querySelector() to target a slide to load its images.一旦我有了那个元素,我应该可以轻松地使用querySelector()来定位幻灯片以加载其图像。

However, I'm having trouble using the slider ref I've created.但是,我在使用我创建的slider参考时遇到了问题。 It's returning a reference to the Slick object, which is not a DOM element, and using querySelector() on slider.current gives the error:它返回对不是 DOM 元素的 Slick object 的引用,并且在slider.current上使用querySelector()会给出错误:

Uncaught TypeError: slider.current.querySelector is not a function未捕获的类型错误:slider.current.querySelector 不是 function

Any thoughts as to how to reference the Slick DOM element?关于如何引用 Slick DOM 元素的任何想法?

export default function Carousel({ children, ...slickProps }) {
    const slider = useRef();

    const slickDefaults = {
        dots: true,
        infinite: false,
        slidesToScroll: 1,
        slidesToShow: 1,
        speed: 200,
        arrows: true
    };

    const onNext = () => {
        slider.current.slickNext();
    };
    const onPrev = () => {
        slider.current.slickPrev();
    };

    return (
        <Slider
            {...slickDefaults}
            {...slickProps}
            ref={slider}
            className="carousel"
            beforeChange={(_currentSlide, nextSlide) => {
                if (slider.current) {

                    // The error occurs here
                    const slideElement = slider.current.querySelector(`[data-index="${nextSlide}"]`);

                    if (slideElement) {
                        const images = slideElement.getElementsByTagName('IMG');
                        for (let i = 0; i < images.length; i++) {
                            const image = images[i];
                            const lazySrc = image.getAttribute('data-lazy');
                            if (lazySrc) {
                                image.setAttribute('src', lazySrc)
                                image.removeAttribute('data-lazy');
                            }
                        }
                    }
                }
            }}
            prevArrow={
                <CarouselButton
                    direction="left"
                    clickEvent={onPrev}
                />
            }
            nextArrow={
                <CarouselButton
                    direction="right"
                    clickEvent={onNext}
                />
            }
        >
            {children}
        </Slider>
    );
}

It turns out that I just had to dig a little deeper into the object pointed to by the ref.事实证明,我只需要更深入地研究参考文献所指向的 object。 In most cases, ref.current will suffice, but here the slideshow DOM element can be found at: ref.current.innerSlider.list , which references the element div.slick-list .在大多数情况下, ref.current就足够了,但这里的幻灯片 DOM 元素可以在以下位置找到: ref.current.innerSlider.list ,它引用元素div.slick-list

The error can be resolved by replacing the line with:可以通过将行替换为以下内容来解决该错误:

const slideElement = slider.current.innerSlider.list.querySelector(`[data-index="${slideIndex}"]`);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM