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

时间:2018-03-02 01:40:56

标签: css reactjs css-position react-slick

我正在使用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>

1 个答案:

答案 0 :(得分:1)

似乎absolute位置元素不受relative容器元素的限制,它应该是。

只需调试,请在css中尝试:

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

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

如果需要!important,那么其他地方的某种风格可能会改变它的价值。

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