【发布时间】:2015-08-17 09:08:48
【问题描述】:
我正在使用光滑的滑块并尝试为实际滑块创建某种设计。我基本上需要的是一个类似这样的结构:
div div
div div
div
我已经能够让这个设计在滑动/过渡时自动悬停到这个设计
div div div div div
回到原始设计的地方。我想知道是否有可能在过渡时保持顶级设计。下面是我目前的 CSS、HTML 和 jQuery。
HTML:
<div class="loop">
<div class="product"> Your Content </div>
<div class="product"> Your Content </div>
<div class="product"> Your Content </div>
<div class="product"> Your Content </div>
<div class="product"> Your Content </div>
<div class="product"> Your Content </div>
<div class="product"> Your Content </div>
</div>
CSS:
.active:first-child {
margin-top: 10px; }
.test, .slick-active:nth-child(1), .slick-active:nth-child(5) {
margin-top: 10px; }
.test-2, .slick-active:nth-child(2), .slick-active:nth-child(4) {
margin-top: 40px; }
.center-test, .slick-active:nth-child(3) {
margin-top: 70px; }
JavaScript:
$(document).ready(function () {
var loop = $(".loop");
loop.slick({
slidesToShow: 5,
sliderPerRow: 1,
swipeToSlide: true
});
loop.on('afterChange', function (event, slick, currentSlide, nextSlide) {
loop.find(".slick-active").first().addClass("test");
loop.find(".slick-active").last().addClass("test");
loop.find(".slick-active").eq(1).addClass("test-2");
loop.find(".slick-active").eq(3).addClass("test-2");
loop.find(".slick-active").eq(2).addClass("center-test");
});
loop.on('beforeChange', function (event, slick, currentSlide, nextSlide) {
loop.find(".slick-active ").removeClass("test");
loop.find(".slick-active ").removeClass("test-2");
loop.find(".slick-active ").removeClass("center-test");
});
loop.find(".slick-active").removeClass("test");
loop.find(".slick-active").first().addClass("test");
loop.find(".slick-active").last().addClass("test");
loop.find(".slick-active").eq(1).addClass("test-2");
loop.find(".slick-active").eq(3).addClass("test-2");
loop.find(".slick-active").eq(2).addClass("center-test");
});
我猜应该有某种偏移量,而不是margin,一旦代码滑动,就会计算每个div。这是光滑滑块的文档: http://kenwheeler.github.io/slick/
编辑
我也在这个 css 中添加了而不是在 div 中添加边距。
.test, .slick-active:nth-child(1), .slick-active:nth-child(5){
top:10px;
position:relative;
}
.test-2, .slick-active:nth-child(2), .slick-active:nth-child(4){
top:40px;
position:relative;
}
.center-test, .slick-active:nth-child(3){
top:70px;
position:relative;
}
.slick-track{
height:100px;
}
到目前为止,我观察到的是它在幻灯片完成过渡后添加了结构更改的类,因此在 JS 中添加了“afterChange”。但是,当过渡也到位时,是否有可能应用该结构。
【问题讨论】:
-
如果边距不起作用,请尝试使用位置。这也可能与整个滑块的高度有关。也许您必须为幻灯片设置一些最小高度?我很确定您想要实现的目标是可能的:将每张幻灯片视为产品的容器,并将产品放置在其中您想要的位置。
-
@miss.emenems 抱歉,它不起作用,当您滑动或导航到下一张幻灯片时,它似乎不考虑 css 的结构。然后,一旦你降落在适当的幻灯片上,它就会添加 .slick-active 和哪一个测试类。所以它显示了旋转木马的短暂时间延迟。
-
不是真正的解决方案,但也许它会为您指明正确的方向output.jsbin.com/hiyepa
-
@ionutvmi,哇,几乎就在那里,有没有办法将它从 Your Content1 调整到 Your Content2,这几乎就是我需要轮播做的事情。回到上一张幻灯片时,这只是一个小问题。
标签: javascript jquery html css slick.js