我已经为我的网站实现了React Slick Slider,它适用于桌面视图并相应地采用适当的宽度,但如果我切换到响应式视图,容器的宽度不正确,它看起来都是扭曲的。
我试图用CSS修复它,给slick-track
div 100%的宽度,但没有任何效果。
以下是容器:光滑轨道,下面是添加的内联css:
width: 2.14748e+08px;
opacity: 1;
transform: translate3d(-8.05305e+07px, 0px, 0px);
以下是我在组件中初始化滑块的方法:
const settings = {
dots: false,
infinite: true,
speed: 500,
cssEase: 'linear',
slidesToShow: this.props.data && this.props.data.upSell && this.props.data.upSell.length>=5?6:this.props.data&& this.props.data.upSell && this.props.data.upSell.length,
rtl: rtl === "en" ? false : true,
slidesToScroll: 1,
asNavFor: null,
arrows: false,
initialSlide: rtl==="en"?0:2,
responsive: [
{
breakpoint: 768,
settings: {
slidesToShow: 2,
slidesToScroll: 1,
infinite: true,
initialSlide: rtl==="en"?0:3,
dots: false
}
}
]
};
下面是循环中的示例HTML结构:
<Slider className="mx-4" {...settings} ref={c => (this.slider = c)}>
<div class="rtl product-item" tabindex="-1" style="width: 100%; display: inline-block;">
<div class="product-shop-top">
<a href="#"><img src="" alt="" class="product-img"></a>
</div>
<div class="product-item-details">
<h5><a href="#">Product Name</a></h5>
<div class="price-wrapper">
<div class="price-box"><span class="special-price">65</span></div>
</div>
</div>
<div class="buynow-block asas"><button type="button" title="Add to Cart" class="action tocart primary"> </button><a class="towishlist">Add to Wishlist</a></div>
</div>
</Slider>
我希望连续2列用于响应光滑滑块。
最后我能够找出问题,滑块的容器位于页面上其他元素所在的同一行内,这导致了滑块无法计算宽度的问题。之前的结构是这样的:
<div classname="row">
<div classname="elementone"></div>
<div classname="elementtwo"></div>
<div classname="slider"></div>
</div>
所以我把滑块移到了行外面,现在工作正常。