最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Using variable in jQuery Slick slideshow to set initial slide doesn't work - Stack Overflow

programmeradmin6浏览0评论

I'm using a slideshow on my website using jQuery Slick.

My slideshow is hidden when page is loaded, and I have some thumbnails to click on to open my slideshow.
Each thumbnail has a data-number attribute (my variable), and when clicking on a thumbnail, my slideshow opens itself, and the initial slide should be set depending on which thumbnails has been clicked.

Here is my code without the variable. I choose thumb 4 just for the example, and it works fine :

$(".thumb").click(function(){
  $(".slider").show();
  setTimeout(function(){
    $('.slider').slick({
      slidesToShow: 4,
      slidesToScroll: 1,
      centerMode: true,   
      initialSlide : 4,
      infinite: true,
      prevArrow : $('.prev'),
      nextArrow : $('.next')});
    $(".slider").css("opacity", 1);
  }, 2000);
});

You can see it working on this jsFiddle.

Now, when I try to put a variable in my code for the initialSlide, the initial slide is ok, but when I click on "next", my slideshow disappear or is not working properly...
try for example thumb 7...

Here is my code :

$(".thumb").click(function(){
  var initialslide = $(this).attr("data-number");
  $(".slider").show();
  setTimeout(function(){
    $('.slider').slick({
      slidesToShow: 4,
      slidesToScroll: 1,
      centerMode: true,   
      initialSlide : initialslide,
      infinite: true,
      prevArrow : $('.prev'),
      nextArrow : $('.next')});
    $(".slider").css("opacity", 1);
  }, 200);
});

See this jsFiddle to see it in action : if you click on next after the slideshow appears, you'll see the problem.

I don't know what I'm doing wrong, can anybody help me with this ?

Thanks

I'm using a slideshow on my website using jQuery Slick.

My slideshow is hidden when page is loaded, and I have some thumbnails to click on to open my slideshow.
Each thumbnail has a data-number attribute (my variable), and when clicking on a thumbnail, my slideshow opens itself, and the initial slide should be set depending on which thumbnails has been clicked.

Here is my code without the variable. I choose thumb 4 just for the example, and it works fine :

$(".thumb").click(function(){
  $(".slider").show();
  setTimeout(function(){
    $('.slider').slick({
      slidesToShow: 4,
      slidesToScroll: 1,
      centerMode: true,   
      initialSlide : 4,
      infinite: true,
      prevArrow : $('.prev'),
      nextArrow : $('.next')});
    $(".slider").css("opacity", 1);
  }, 2000);
});

You can see it working on this jsFiddle.

Now, when I try to put a variable in my code for the initialSlide, the initial slide is ok, but when I click on "next", my slideshow disappear or is not working properly...
try for example thumb 7...

Here is my code :

$(".thumb").click(function(){
  var initialslide = $(this).attr("data-number");
  $(".slider").show();
  setTimeout(function(){
    $('.slider').slick({
      slidesToShow: 4,
      slidesToScroll: 1,
      centerMode: true,   
      initialSlide : initialslide,
      infinite: true,
      prevArrow : $('.prev'),
      nextArrow : $('.next')});
    $(".slider").css("opacity", 1);
  }, 200);
});

See this jsFiddle to see it in action : if you click on next after the slideshow appears, you'll see the problem.

I don't know what I'm doing wrong, can anybody help me with this ?

Thanks

Share Improve this question edited Apr 21, 2017 at 7:21 Sumi Straessle 1,14612 silver badges23 bronze badges asked Apr 20, 2017 at 21:59 mmdwcmmdwc 1,1376 gold badges28 silver badges57 bronze badges
Add a ment  | 

1 Answer 1

Reset to default 5

tl;dr use parseInt with $(this).attr("data-number") or $(this).data("number")

I just added parseInt(initialSlide) and it seems to have fixed your issue.

$(".thumb").click(function(){
  var initialslide = $(this).attr("data-number");

  $(".slider").show();
  setTimeout(function(){
    $('.slider').slick({
      slidesToShow: 4,
      slidesToScroll: 1,
      centerMode: true,   
      initialSlide : parseInt(initialslide),
      infinite: true,
      prevArrow : $('.prev'),
      nextArrow : $('.next')});
    $(".slider").css("opacity", 1);
  }, 200);
});

Edit: you can use jQuery Data method directly, instead of $(this).attr(...), which seems to cast to int automagically.

$(".thumb").click(function(){
  var initialslide = $(this).data("number");

  $(".slider").show();

  setTimeout(function(){
    $('.slider').slick({
      slidesToShow: 4,
      slidesToScroll: 1,
      centerMode: true,   
      initialSlide : initialslide,
      infinite: true,
      prevArrow : $('.prev'),
      nextArrow : $('.next')});
    $(".slider").css("opacity", 1);
  }, 200);
});
发布评论

评论列表(0)

  1. 暂无评论