简体   繁体   English

Slick Carousel - 设置第一张幻灯片

[英]Slick Carousel - Set first slide

I have a slick carousel on my website showing a basketball team's schedule.我的网站上有一个漂亮的旋转木马,显示篮球队的日程安排。 The slider contains all the games from the current season ordered by date. slider 包含按日期排序的当前赛季的所有比赛。

I want the slider to be centered to the next game.我希望 slider 以下一场比赛为中心。 How do I set a specific slide as the first slide, even though it's not the first one on the html.如何将特定幻灯片设置为第一张幻灯片,即使它不是 html 上的第一张幻灯片。

Code:代码:

$('.result_slider').slick({
    rtl: true,
    centerPadding: '0px',
    slidesToShow: 6,
    responsive: [
        {
            breakpoint: 1680,
            settings: {
            arrows: false,
            centerPadding: '0px',
            slidesToShow: 3
          }
        },
        {
          breakpoint: 481,
          settings: {
            arrows: false,
            centerPadding: '0px',
            slidesToShow: 1
          }
        }
    ]
});

You can use initialSlide for that. 您可以使用initialSlide Note that the first slide has the number 0, so if you would like the slider to start from the second slide you would set initialSlide: 1 . 请注意,第一张幻灯片的编号为0,因此如果您希望滑块从第二张幻灯片开始,您可以设置initialSlide: 1

Here's my minimal example where the slider starts from the third slide. 这是滑块从第三张幻灯片开始的最小示例

If you assign your dates of the games in a PHP or JavaScript variable, then you can compare them to the current date and place that variable in the "initialSlide" call. 如果您在PHP或JavaScript变量中分配游戏的日期,则可以将它们与当前日期进行比较,并将该变量放在“initialSlide”调用中。

Example below is based on day of the week, but the concept is still the same. 以下示例基于星期几,但概念仍然相同。 I have a client who has specials at their bar 6 days a week Monday - Saturday. 我有一个客户在周一至周六的一周6天在他们的酒吧有特价。 So with the script below, it will make the current day of the week "center". 因此,使用下面的脚本,它将使当周的当天成为“中心”。

You could also do this matching the date instead of the day of the week. 您也可以匹配日期而不是星期几。

jQuery(document).ready(function(){

    var d = new Date();
    var day = d.getDay();
    var slide;

    if (day == 1) {
        slide = 0;
    }else if (day == 2) {
        slide = 1;
    }else if (day == 3) {
        slide = 2;
    }else if (day == 4) {
        slide = 3;
    }else if (day == 5) {
        slide = 4;
    }else if (day == 6) {
        slide = 5;
    }else{
        slide = 0;
    }

    $('.specials').slick({
      centerMode: true,
      centerPadding: '40px',
      adaptiveHeight: true,
      slidesToShow: 3,
      initialSlide: slide,
      responsive: [
        {
          breakpoint: 768,
          settings: {
            arrows: false,
            centerMode: true,
            centerPadding: '40px',
            slidesToShow: 3
          }
        },
        {
          breakpoint: 480,
          settings: {
            arrows: false,
            centerMode: true,
            centerPadding: '40px',
            slidesToShow: 1
          }
        }
      ]
    });

});

Here is an example using PHP. 这是一个使用PHP的示例。

<?php
//Get current Date/Time
date_default_timezone_set('America/Los_Angeles');
$date = new DateTime();
// Set day variable global for initial slide
global $day;
//$date->format('l') - this gets the "Day" of the week by name.
// if current day of the week matches either set days of the week, then match that slide as the initialSlide.
if ($date->format('l') == "Monday"){
    $day = "0";//slide 0
}else if ($date->format('l') == "Tuesday"){
    $day = "1";//slide 1
}else if ($date->format('l') == "Wednesday"){
    $day = "2";//slide 2
}else if ($date->format('l') == "Thursday"){
    $day = "3";//slide 3
}else if ($date->format('l') == "Friday"){
    $day = "4";//slide 4
}else if ($date->format('l') == "Saturday"){
    $day = "5";//slide 5
}else{
    $day = "0";//slide 0
}
?>

jQuery(document).ready(function(){

    $('.specials').slick({
      centerMode: true,
      centerPadding: '40px',
      adaptiveHeight: true,
      slidesToShow: 3,
      initialSlide: <?php echo $day; ?>,
      responsive: [
        {
          breakpoint: 768,
          settings: {
            arrows: false,
            centerMode: true,
            centerPadding: '40px',
            slidesToShow: 3
          }
        },
        {
          breakpoint: 480,
          settings: {
            arrows: false,
            centerMode: true,
            centerPadding: '40px',
            slidesToShow: 1
          }
        }
      ]
    });

});

You can use the builtin method.您可以使用内置方法。

this.slider.slickGoTo(0);

-For react Also utilize this slider as property like this. -对于反应也可以使用这个滑块作为这样的属性。

<Slider ref={slider => (this.slider = slider)} { ...sliderSettings }>
   { categories.map(((t) => this.renderCategory(t.path, t.image, t.name))) 
   }
</Slider>

Depending on Slick.js version, setting initialSlide option may cause issues.根据 Slick.js 版本,设置initialSlide选项可能会导致问题。

Try something like:尝试类似的东西:

$slider = $('.slick-slider');

const api = $slider.slick('getSlick');
api.slickGoTo(selectedIndex);

Note to define selectedIndex variable, and set to desired slide's index (not number).注意定义selectedIndex变量,并设置为所需幻灯片的索引(不是数字)。

 $('.slider-info').slick('slickAdd','<div>your text</div>',true);

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

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