【问题标题】:slick slider on transition keep structure过渡保持结构上的光滑滑块
【发布时间】: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


【解决方案1】:

这里最明显的问题是无限循环。
也许有一天有人会设法解决这个问题,但现在如果你愿意放弃它,这应该可以解决问题。
它适用于任意数量的幻灯片。

$(document).ready(function () {
  var $loop = $(".loop");
  
  $loop.slick({
      slidesToShow: 5,
      sliderPerRow: 1,
      swipeToSlide: true,
      speed: 500,
      infinite: false
  });

});
.slick-track{
  height:100px;
}


.loop .product {
  background: #ccc;
  outline: 1px solid black;
  transition: transform .5s; /* same duration as in js */
  transform: translateY(0);
}


.loop .product.slick-current + .product {
  transform: translateY(30px);
}

.loop .product.slick-current + .product + .product {
  transform: translateY(50px);
}

.loop .product.slick-current + .product + .product + .product {
  transform: translateY(30px);
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
<link rel="stylesheet" type="text/css" href="//cdn.jsdelivr.net/jquery.slick/1.5.7/slick.css"/>
  <script type="text/javascript" src="//cdn.jsdelivr.net/jquery.slick/1.5.7/slick.min.js"></script>


<div class="loop">
  <div class="product"> Your ContentA </div>
  <div class="product"> Your ContentB </div>
  <div class="product"> Your ContentC </div>
  <div class="product"> Your Content1 </div>
  <div class="product"> Your Content2 </div>
  <div class="product"> Your Content3 </div>
  <div class="product"> Your Content4 </div>
  <div class="product"> Your Content5 </div>
  <div class="product"> Your Content6 </div>
  <div class="product"> Your Content7 </div>
</div>

【讨论】:

    【解决方案2】:

    我不确定,但我认为: 光滑的滑块会在您滑动或滑动时添加和删除元素,因此您使用的第 n 个子选择器将无法正常工作。

    尝试使用类名代替第 n 个选择器,例如:

     .test, .slick-active:frstchild{
        top:10px;
        position:relative;
        }
    
    <div class="loop">
          <div class="product frstchild"> Your Content </div>
          <div class="product"> Your Content </div>
          <div class="product"> Your Content </div>
          <div class="product"> Your Content </div>
          <div class="product frstchild"> Your Content </div>
          <div class="product"> Your Content </div>
          <div class="product"> Your Content </div>
        </div>
    

    或者您可以相应地在第 n 个子选择器中使用定义循环大小,例如:

    .test, .slick-active:frstchild(3n+1){}
    

    【讨论】:

      【解决方案3】:

      试试这个:

      .product:nth-child(5n+1),
      .product:nth-child(5n+5)
      {
          top:10px;
          position:relative;
      }
      .product:nth-child(5n+2),
      .product:nth-child(5n+4)
      {
          top:40px;
          position:relative;
      }
      .product:nth-child(5n+3)
      {
          top:70px;
          position:relative;
      }
      

      Slick 滑块添加元素 (slick-cloned),这就是您在设置样式时遇到问题的原因。现在你不需要所有这些 javascript 魔法;) Here is nice explanationnth-child

      注意使用nth-child,因为它并不总是像您想象的那样工作。例如:

      <style>
          .product:nth-child(1){background:red;}
          .product:nth-child(2){background:blue;}
      </style>
      <div class="loop">
          <div class="promo"> Some promo </div>
          <div class="product"> Your Content1 </div>
          <div class="product"> Your Content2 </div>
      </div>
      

      这些元素的背景是什么?

      【讨论】:

        猜你喜欢
        • 2018-01-06
        • 2019-06-21
        • 2013-08-26
        • 2019-08-08
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多