3

我正在使用反应光滑的滑块。问题在于项目顺序:第一个项目呈现为最后一个。但是,如果无限为假,则缺少第一项,最后一项是空白。

import React from 'react';
import Slider from "react-slick";

class Slideshow extends React.Component {
    constructor(props) {
        super(props);
        this.state = {}
    }

    render() {
        const { config } = this.props;
        const slideItems = config.items;

        const data = slideItems.map((item,index) => {
            let textStyle = config.textStyle;
            let titleSize = config.titleSize;
            let urlType = config.urlType;
            let showContent = config.showContent;
            let slideItemStyle = {
                "max-height": config.sliderHeight.indexOf("px")==-1?(config.sliderHeight+"px"):config.sliderHeight
            }
            let style = {
                "background-color": config.backgroundColor
            }
            let actionTarget = item.actionTarget ? "_blank" : "_parent";
            return <div className="slide-item">
                <img src={item.imageUrl} style={slideItemStyle}/>
                {showContent &&
                    <div className={`slide-content slide-content-${index} ${textStyle}`} style={style}>

                        <div className="title">
                            {titleSize == "h1" && <h1>{item.title}</h1>}
                            {titleSize == "h2" && <h2>{item.title}</h2>}
                            {titleSize == "h3" && <h3>{item.title}</h3>}
                            {titleSize == "h4" && <h4>{item.title}</h4>}
                        </div>
                        <div className="description">{item.description}</div>
                        {item.url && item.url != "" &&
                            <a
                                href={item.url}
                                className={`action-url ${urlType == "button" ? "more" : ""}`}
                                target={actionTarget}
                            >{item.urlLabel}</a>
                        }
                    </div>
                }
            </div>;
        });

        const settings = {
            dots: (config.navigationState != "none"),
            dotsClass: (config.navigationState == "thumbnails" ? "slick-dots slick-thumb" : "slick-dots"),
            infinite: config.infinite,
            speed: config.speed,
            arrows: config.arrows,
            slidesToShow: 1,
            slidesToScroll: 1,
            autoplay: config.autoplay,
            autoplaySpeed: config.autoplaySpeed,
            fade: (config.animationType == "fade"),
            vertical: false
        };

        if (config.navigationState == "thumbnails") {
            settings.customPaging = function (i) {
                let style = {"background-image":`url(${slideItems[i].imageUrl})`}
                return (
                    <a className="slide-thumbnail-item" style={style}>
                        <img />
                    </a>
                );
            }
        }

        return (
            <Slider {...settings} className={`${config.navigationState} ${config.showNavigationOnSlider?"navigation-on-slide":""} thumbnail-ailgn-${config.thumbnailAnimationAlignment} slidenav-position-${config.slideNavPosition} ${config.animationType=="fade" ? 'carousel slick-slider-fade' : 'carousel'}`}>
                {data}
            </Slider>
        );
    }
}

export default Slideshow;

我搜索但找不到这样的问题。没有任何错误。我正在使用缩略图模式,示例代码在这里: https ://react-slick.neostack.com/docs/example/custom-paging/

4

1 回答 1

12

我也有这个问题。一旦无限滚动被激活,最后一个项目首先被渲染。

这仅在我动态创建幻灯片时发生。(我尝试对幻灯片进行硬编码,它按预期工作)

我添加了一个条件以仅在创建幻灯片时呈现滑块:

const {elements} = this.props;

const slides = elements.map((item, i) => (
  <SliderItem  className="sliderItem" key={i} item={item} />
))

var settings = {
  dots: true,
  infinite: true,
  speed: 500,
  slidesToShow: 1,
  slidesToScroll: 1

};
return (
  elements != null && elements.length > 0 && 
  <Slider className="slider" {...settings}>
    {slides}
  </Slider>
);
于 2019-06-05T07:39:56.150 回答