旋转木马在图像更改时保持锚定到页面顶部

carousel keeps anchoring to the top of page on image change

我正在使用 react-slick,但我遇到的问题是,每当轮播发生变化时,它都会自动锚定到页面顶部,而与您在页面上的位置无关。

这些是我通过 css 所做的所有更改。

.slick-slider {
   position: relative;
}
.slick-dots {
    position: absolute;
    bottom: 10px;
    z-index: 1;
}

.slick-dots li.slick-active button:before {
    color: white;
    opacity: 1;
}

.slick-dots li button:before {
    color: white;
}

这些是我传递给组件的设置:

class Carousel extends React.Component {
    render() {
        const settings = {
            dots: true,
            speed: 500,
            slidesToShow: 0,
            slidesToScroll: 1,
            fade: true,
            autoplay: true,
            infinite: true,
            arrows: false
    };

    return (
        <Slider {...settings}>
            {testImages.map((i, index) => 
                <div key={index}>
                     <CarouselImage imageSrc={`${i}`} />
                </div>)}
        </Slider>
    );
  }
}

任何人都可以阐明我做错了什么以及为什么会这样吗?

已渲染 html:

<div class="slick-initialized slick-slider">
   <div class="slick-list">
      <div class="slick-track" style="opacity: 1; width: 5901px;">
         <div data-index="0" class="slick-slide slick-active slick-current" tabindex="-1" style="outline: none; position: relative; left: 0px; opacity: 1; visibility: visible; transition: opacity 500ms ease, visibility 500ms ease; width: 843px;">
            <div class="overflow-hidden carousel_image__container"><img class="carousel_image" src="https://via.placeholder.com/1900x1000/ff0000/000000" alt="Artist Carousel Image"></div>
         </div>
         <div data-index="1" class="slick-slide" tabindex="-1" style="outline: none; position: relative; left: -843px; opacity: 0; visibility: hidden; transition: opacity 500ms ease, visibility 500ms ease; width: 843px;">
            <div class="overflow-hidden carousel_image__container"><img class="carousel_image" src="https://via.placeholder.com/1900x1000/00ff00/000000" alt="Artist Carousel Image"></div>
         </div>
         <div data-index="2" class="slick-slide" tabindex="-1" style="outline: none; position: relative; left: -1686px; opacity: 0; visibility: hidden; transition: opacity 500ms ease, visibility 500ms ease; width: 843px;">
            <div class="overflow-hidden carousel_image__container"><img class="carousel_image" src="https://via.placeholder.com/1900x1000/0000ff/000000" alt="Artist Carousel Image"></div>
         </div>
      </div>
   </div>
   <ul class="slick-dots" style="display: block;">
      <li class="slick-active"><button>1</button></li>
      <li class=""><button>2</button></li>
      <li class=""><button>3</button></li>
   </ul>
</div>

似乎 absolute 位置元素没有受到 relative 容器元素的限制,它应该受到限制。

只是为了调试,在 css 中试试这个:

div.slick-initialized.slick-slider {
   position: relative!important;
}

如果有帮助,请尝试不使用 !important

如果 !important 是必需的,那么其他地方的某些样式可能会改变它的值。

如果 none 有效,则右键单击 slick-list div 元素,检查它并查看它的 css 代码。如有必要,您可以将其复制到您的问题中以供进一步审查。对 slick-track div 元素执行相同操作。