响应:幻灯片在调整大小时消失

时间:2018-05-23 10:15:42

标签: jquery slick

我在幻灯片中使用了响应断点。在小屏幕上,我一次显示1张幻灯片。在大2(大意味着小于990像素)

示例:我将断点设置为在小屏幕上一次显示1张幻灯片,并在大屏幕中禁用slider。我有8张幻灯片。屏幕大于990像素。

如果在小屏幕上我导航到幻灯片并再次使屏幕变大,此块中的每张幻灯片都会消失。

您可以在codepen

中查看

如果删除这部分代码

responsive: [{
                breakpoint: 568,
                settings: {
                    slidesToShow: 1
                }
            }]

一切都会好起来的

请帮助任何人

这是我的配置Slick slider

$(window).on('load', function(){
    if($(window).width() < 990){
     $('.container-slick').slick({
          centerMode: true,
          infinite: true,
          prevArrow: false,
          nextArrow: false,
          speed: 200,
          slidesToShow: 2,
          slidesToScroll:1,
          autoplay:true,
            responsive: [{
                breakpoint: 568,
                settings: {
                    slidesToShow: 1
                }
            }]
        });
    }
  });
$(window).on('resize', function(){
    if($(window).width() < 990){
     $('.container-slick').not('.slick-initialized').slick({
          centerMode: true,
          infinite: true,
          prevArrow: false,
          nextArrow: false,
          speed: 200,
          slidesToShow: 2,
          slidesToScroll:1,
         // if you remove from here
            responsive: [{
                breakpoint: 568,
                settings: {
                    slidesToShow: 1
                }
            }]
            // to there everything will work but i need 1 slide in small screens and 2 in screens more than 568 pixels
        });
    }
    else{
      $(".container-slick.slick-initialized").slick("unslick");
    }
  });

2 个答案:

答案 0 :(得分:0)

请参阅下面的小提琴

Fiddle

$(document).ready(function(){
  if($(window).width() < 1200){
   initializeSlick()
  }


  $(window).on('resize', function(){
    if($(window).width() < 1200){
      $(".container-slick.slick-initialized").slick("unslick");
    } else if(!$('.container-slick').hasClass('slick-initialized')) {
      initializeSlick()
    }
  });


    function initializeSlick() {
      $('.container-slick').slick({
          centerMode: true,
          infinite: true,
          prevArrow: false,
          nextArrow: false,
          speed: 200,
          slidesToShow: 2,
          slidesToScroll:1,
          autoplay:true,
            responsive: [{
                breakpoint: 568,
                settings: {
                    slidesToShow: 1
                }
            }]
        });
      }
  });
.projects {
  position: relative;
  width: 100%;
}
.projects .projects_title {
  font-family: "montserrat";
  position: relative;
  bottom: 10%;
  font-size: 26px;
  font-weight: bold;
  font-style: normal;
  font-stretch: normal;
  line-height: 2.69;
  letter-spacing: 1.3px;
  text-align: left;
  color: #2a323d;
  text-transform: uppercase;
}
.projects .projects_title span {
  color: #ffb338;
}

.no-padding {
  padding: 0 !important;
}

.projects_block {
  margin-bottom: 2%;
  display: flex;
  flex-wrap: wrap;
}
.projects_block .project_block {
  width: 23%;
  display: flex;
  flex-direction: column;
  height: 100px;
  transition: background-image, box-shadow 0.3s ease-in-out;
  background-image: none;
  margin: 0 1%;
  background-size: cover;
  background-position: 58% 30%;
  background-blend-mode: multiply;
}
@media only screen and (max-width: 768px) {
  .projects_block .project_block {
    width: auto;
  }
}
.projects_block .project_block:first-child {
  margin-left: 0 !important;
}
.projects_block .project_block:last-child {
  margin-right: 0 !important;
}
.projects_block .project_block:hover {
  cursor: pointer;
  border-bottom-left-radius: 40px;
  background-blend-mode: multiply;
  border-top-right-radius: 40px;
  border: 2px solid #fff;
  background-repeat: no-repeat;
  box-shadow: 15px 16px 30px 0px rgba(0, 0, 0, 0.4);
  background-image: linear-gradient(to top, #151515, rgba(255, 255, 255, 0)),
    url(https://milan-holidays.com/wp-content/uploads/2017/07/milan_tournimage_16_1920x1015.jpg);
}
.projects_block .project_block:hover .project_title {
  color: #fff;
  padding: 20px 0 0 20px;
}
.projects_block .project_block:hover .project_name {
  color: #fff;
  padding-left: 20px;
}
.projects_block .project_block .project_title {
  font-size: 14px;
  font-weight: normal;
  font-style: normal;
  font-stretch: normal;
  line-height: normal;
  letter-spacing: 0.7px;
  text-align: left;
  color: #666666;
}
.projects_block .project_block .project_name {
  font-size: 14px;
  font-weight: bold;
  font-style: normal;
  font-stretch: normal;
  line-height: normal;
  letter-spacing: 0.7px;
  text-align: left;
  color: #2a323d;
}
.projects_block .project_block a {
  opacity: 0;
  transition: opacity 0.3s ease-in-out;
  font-family: "montserrat";
}
.projects_block .project_block:hover a {
  opacity: 1;
  font-size: 12px;
  font-weight: bold;
  font-style: normal;
  font-stretch: normal;
  line-height: normal;
  letter-spacing: 0.6px;
  text-align: left;
  color: #ffb338;
  padding-left: 24px;
  padding-top: 6px;
  text-decoration: none;
}

.project_block:nth-child(1):hover {
  cursor: pointer;
  border-bottom-left-radius: 40px;
  background-blend-mode: multiply;
  border-top-right-radius: 40px;
  border: 2px solid #fff;
  background-repeat: no-repeat;
  box-shadow: 15px 16px 30px 0px rgba(0, 0, 0, 0.4);
  background-image: linear-gradient(to top, #151515, rgba(255, 255, 255, 0)),
    url(https://milan-holidays.com/wp-content/uploads/2017/07/milan_tournimage_16_1920x1015.jpg);
}
.project_block:nth-child(2):hover {
  cursor: pointer;
  border-bottom-left-radius: 40px;
  background-blend-mode: multiply;
  border-top-right-radius: 40px;
  border: 2px solid #fff;
  background-repeat: no-repeat;
  box-shadow: 15px 16px 30px 0px rgba(0, 0, 0, 0.4);
  background-image: linear-gradient(to top, #151515, rgba(255, 255, 255, 0)),
    url(https://milan-holidays.com/wp-content/uploads/2017/07/milan_tournimage_16_1920x1015.jpg);
  background-position: 78% 0%;
}
.project_block:nth-child(3):hover {
  cursor: pointer;
  border-bottom-left-radius: 40px;
  background-blend-mode: multiply;
  border-top-right-radius: 40px;
  border: 2px solid #fff;
  background-repeat: no-repeat;
  box-shadow: 15px 16px 30px 0px rgba(0, 0, 0, 0.4);
  background-image: linear-gradient(to top, #151515, rgba(255, 255, 255, 0)),
    url(https://milan-holidays.com/wp-content/uploads/2017/07/milan_tournimage_16_1920x1015.jpg);
  background-position: 78% 0%;
}
.project_block:nth-child(4):hover {
  cursor: pointer;
  border-bottom-left-radius: 40px;
  background-blend-mode: multiply;
  border-top-right-radius: 40px;
  border: 2px solid #fff;
  background-repeat: no-repeat;
  box-shadow: 15px 16px 30px 0px rgba(0, 0, 0, 0.4);
  background-image: linear-gradient(to top, #151515, rgba(255, 255, 255, 0)),
    url(https://milan-holidays.com/wp-content/uploads/2017/07/milan_tournimage_16_1920x1015.jpg);
}
.project_block:nth-child(5) {
  margin-left: 0 !important;
}
.project_block:nth-child(5):hover {
  cursor: pointer;
  border-bottom-left-radius: 40px;
  background-blend-mode: multiply;
  border-top-right-radius: 40px;
  border: 2px solid #fff;
  background-repeat: no-repeat;
  box-shadow: 15px 16px 30px 0px rgba(0, 0, 0, 0.4);
  background-image: linear-gradient(to top, #151515, rgba(255, 255, 255, 0)),
    url(https://milan-holidays.com/wp-content/uploads/2017/07/milan_tournimage_16_1920x1015.jpg);
}
.project_block:nth-child(6):hover {
  cursor: pointer;
  border-bottom-left-radius: 40px;
  background-blend-mode: multiply;
  border-top-right-radius: 40px;
  border: 2px solid #fff;
  background-repeat: no-repeat;
  box-shadow: 15px 16px 30px 0px rgba(0, 0, 0, 0.4);
  background-image: linear-gradient(to top, #151515, rgba(255, 255, 255, 0)),
    url(https://milan-holidays.com/wp-content/uploads/2017/07/milan_tournimage_16_1920x1015.jpg);
}
.project_block:nth-child(7):hover {
  cursor: pointer;
  border-bottom-left-radius: 40px;
  background-blend-mode: multiply;
  border-top-right-radius: 40px;
  border: 2px solid #fff;
  background-repeat: no-repeat;
  box-shadow: 15px 16px 30px 0px rgba(0, 0, 0, 0.4);
  background-image: linear-gradient(to top, #151515, rgba(255, 255, 255, 0)),
    url(https://milan-holidays.com/wp-content/uploads/2017/07/milan_tournimage_16_1920x1015.jpg);
  background-position: 50% 100%;
}
.project_block:nth-child(8):hover {
  cursor: pointer;
  border-bottom-left-radius: 40px;
  background-blend-mode: multiply;
  border-top-right-radius: 40px;
  border: 2px solid #fff;
  background-repeat: no-repeat;
  box-shadow: 15px 16px 30px 0px rgba(0, 0, 0, 0.4);
  background-image: linear-gradient(to top, #151515, rgba(255, 255, 255, 0)),
    url(https://milan-holidays.com/wp-content/uploads/2017/07/milan_tournimage_16_1920x1015.jpg);
  background-position: 17% 60%;
}

@media (max-width: 768px) {
  .container-slick {
    max-width: 90% !important;
    margin: auto;
    border: 1px solid red;
  }
  .projects_block .slide {
    max-width: 95% !important;
    height: 100%;
    background-size: 100% 100% !important;
    margin: 0 -20px !important;
    outline: none;
  }
}

@media (max-width: 555px) {
  .container-slick {
    max-width: 95% !important;
    margin: auto;
    border: 1px solid red;
  }
  .projects_block .slide {
    max-width: 95% !important;
    margin: 0 -30px !important;
    outline: none;
  }
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<link href="https://cdnjs.cloudflare.com/ajax/libs/slick-carousel/1.9.0/slick.min.css" rel="stylesheet" />
<script src="https://cdnjs.cloudflare.com/ajax/libs/slick-carousel/1.8.1/slick.js"></script>
<div class="container-fluid">
  <div class="row">
    <div class="projects_block container-slick no-padding">
      <div class="project_block slide no-padding">
        <h6 class="project_title">Festival</h6>
        <div class="project_name">Discover Milan's religious sites</div>
        <a href="javascript:void(0)">View &rarr;</a>
      </div>
      <div class="project_block slide no-padding">
        <h6 class="project_title">Festival</h6>
        <div class="project_name">Saint Petersburg Philharmonic Orchestra</div>
        <a href="javascript:void(0)">View &rarr;</a>
      </div>
      <div class="project_block slide no-padding">
        <h6 class="project_title">Festival</h6>
        <div class="project_name">Grand Hall Music </div>
        <a href="javascript:void(0)">View &rarr;</a>
      </div>
      <div class="project_block slide no-padding">
        <h6 class="project_title">Festival</h6>
        <div class="project_name">Hand-made art & design workshop</div>
        <a href="javascript:void(0)">View &rarr;</a>
      </div>
      <div class="project_block slide no-padding">
        <h6 class="project_title">Festival</h6>
        <div class="project_name">Discover Milan's religious sites</div>
        <a href="javascript:void(0)">View &rarr;</a>
      </div>
      <div class="project_block slide no-padding">
        <h6 class="project_title">Festival</h6>
        <div class="project_name">Saint Petersburg Philharmonic Orchestra</div>
        <a href="javascript:void(0)">View &rarr;</a>
      </div>
      <div class="project_block slide no-padding">
        <h6 class="project_title">Festival</h6>
        <div class="project_name">Grand Hall Music </div>
        <a href="javascript:void(0)">View &rarr;</a>
      </div>
      <div class="project_block slide no-padding">
        <h6 class="project_title">Festival</h6>
        <div class="project_name">Hand-made art & design workshop</div>
        <a href="javascript:void(0)">View &rarr;</a>
      </div>
    </div>
  </div>
</div>

更改以下行

$(window).width() < 990

$(window).width() < 1200

答案 1 :(得分:0)

使用setTimeout

$(window).on('load', function(){
    if($(window).width() < 990){
     $('.container-slick').slick({
          centerMode: true,
          infinite: true,
          prevArrow: false,
          nextArrow: false,
          speed: 200,
          slidesToShow: 2,
          slidesToScroll:1,
          autoplay:true,
            responsive: [{
                breakpoint: 568,
                settings: {
                    slidesToShow: 1
                }
            }]
        });
    }
  });
$(window).on('resize', function(){
    if($(window).width() < 990){
      setTimeout(function(){
        $('.container-slick').not('.slick-initialized').slick({
          centerMode: true,
          infinite: true,
          prevArrow: false,
          nextArrow: false,
          speed: 200,
          slidesToShow: 2,
          slidesToScroll:1,
            responsive: [{
                breakpoint: 568,
                settings: {
                    slidesToShow: 1
                }
            }]
        });
      },100)

    }
    else{
    setTimeout(function(){  $(".container-slick.slick-initialized").slick("unslick"); },100)  
    }
  });

您可以在Codepen

中进行检查