简体   繁体   English

光滑的滑块,在第一张幻灯片中填充

[英]Slick slider, padding in the first slide

My idea is the following one: I would like to put a padding in the first slide from slick.js , and yes, i already tried the :first element in css.. But what I really wanted is: put an css class in the first ever element and then change the class when its not appearing 我的想法如下:我想在slick.js的第一张幻灯片中添加一个填充,是的,我已经尝试了CSS中的:first元素。但是我真正想要的是:在CSS中放置一个CSS类。第一个元素,然后在类不出现时更改类

What I've so far: 我到目前为止:

$(document).ready(function () {
    $('.slider1').slick({
        dots: false,
        infinite: false,
        speed: 300,
        slidesToShow: 4
    });

    $('.slider1').on('click', function (event, slick, currentSlide) {
        if (currentSlide === 0) {
            console.log('First element');
            $(this).eq(currentSlide).addClass('first-slide-is-active111');
        } else {
            $(this).eq(currentSlide).removeClass('first-slide-is-active111');
        }
    });
});

but doesnt work.. any help? 但没有用..任何帮助吗?

JS Fiddle demo JS小提琴演示

edit: to make me clear what i would like to do is like a netflix slider.. with space in the first slide.. 编辑:让我清楚我想做什么就像是netflix滑块..在第一张幻灯片中有空格..

you need to add class to the first slide and give padding. 您需要将类添加到第一张幻灯片并进行填充。

 $(document).ready(function () { $('.slider1').slick({ dots: false, infinite: false, speed: 300, slidesToShow: 4, }); $('.slider1').on('click', function (event, slick, currentSlide) { if (currentSlide === 0) { console.log('First element'); $(this).eq(currentSlide).addClass('first-slide-is-active111'); } else { $(this).eq(currentSlide).removeClass('first-slide-is-active111'); } }); }); 
 .s1 { padding: 50px; } 
 <link href="https://cdnjs.cloudflare.com/ajax/libs/slick-carousel/1.8.1/slick-theme.css" rel="stylesheet"/> <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <div class="slider1"> <div class="s1 slide"><span>Slide1</span></div> <div class="slide"><span>Slide2</span></div> <div class="slide"><span>Slide3</span></div> <div class="slide"><span>Slide4</span></div> <div class="slide"><span>Slide5</span></div> <div class="slide"><span>Slide6</span></div> </div> 

Fixed your code 修正您的代码

$(document).ready(function () {
    $('.slider1').slick({
      dots: false,
      infinite: false,
      speed: 300,
      slidesToShow: 4,
    });

    $('.slide:first').addClass('first-slide-is-active111');

    $('.slider1').on('afterChange', function (event, slick, currentSlide) {
      if (currentSlide === 0) {
        console.log('First element');
        $('.slide:first').addClass('first-slide-is-active111');
      } else {
        $('.slide:first').removeClass('first-slide-is-active111');
      }
    });
});  

js fiddle with fixed code js摆弄固定代码

You had to bind your handler to 'afterChange' not to 'click'. 您必须将处理程序绑定到“ afterChange”,而不是“ click”。 And some logic fixes are aplied too. 并且还提供了一些逻辑修复程序。 If this logic is not what you want just let me know in comments below and I will try to help. 如果您不想要这种逻辑,请在下面的评论中告诉我,我将尽力提供帮助。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM