首页 > 解决方案 > 加载时光滑的轮播调用事件

问题描述

目前,代码在事件(更改后)上按预期工作,但我需要运行(更改后)事件内部的代码。

一旦 slick 初始化,我需要运行逻辑。

$('.slick-wrapper').on('init', function(event, slick, currentSlide, nextSlide){
    var $loginLogoutWideContainer = $('.row.hero-content'),
      $loginLogoutWideContainerHeight = $loginLogoutWideContainer.height(),
      $slideSelection = $(slick.$slides.get(currentSlide)),
      $slideSelectionHeight = $slideSelection.height();

    if (($slideSelectionHeight < $loginLogoutWideContainerHeight) && ($(window).width() > 991)) {
      $slideSelection.height(($loginLogoutWideContainerHeight - 60) + 'px');
      $slickOffersWrapper.slick('resize');
    }
});

const slickSettings = {
    dots: true,
    autoplay: false,
    autoplaySpeed: 5000,
    fade: true,
    cssEase: 'linear',
    mobileFirst: true,
    pauseOnDotsHover: true,
    adaptiveHeight: true,
    customPaging: function(slider, i) { //Custom paging is to replace the dots with numbers
      var thumb = $(slider.$slides[i]).data();
      return '<a>' + (i + 1) + '</a>';
    },
    responsive: [{
      breakpoint: 1200,
      settings: {
        pauseOnDotsHover: false
      }
    }]
  };
  
  if ($('.slick-wrapper').children().length > 1) { //If slides are more than 1, construct slick
    $('.slick-wrapper').slick(slickSettings); // Construct Slick
  }
  
  function minHeightSlickDesktopSize() {
    var $loginLogoutWideContainerFixed = $('.row.hero-content');
    if (($(window).width() > 991)) {
      var $loginLogoutWideContainerFixedHeight = $loginLogoutWideContainerFixed.height();
      $('.offers-block-wide .slick-list').css({
        'min-height': ($loginLogoutWideContainerFixedHeight - 60) + 'px'
      });
    } else {
      $('.slick-list').css('min-height', 'auto');
      $('.slick-slide').css('height', 'auto');
    }
  };

  minHeightSlickDesktopSize();
  
  function assignHeightSlick() {

  // Assign the height of the wide container to the slick slides to match the height

  $('.slick-wrapper').on('afterChange', function(event, slick, currentSlide, nextSlide) {

    var $loginLogoutWideContainer = $('.row.hero-content'),
      $loginLogoutWideContainerHeight = $loginLogoutWideContainer.height(),
      $slideSelection = $(slick.$slides.get(currentSlide)),
      $slideSelectionHeight = $slideSelection.height();

    if (($slideSelectionHeight < $loginLogoutWideContainerHeight) && ($(window).width() > 991)) {
      $slideSelection.height(($loginLogoutWideContainerHeight) + 'px');
      $slickOffersWrapper.slick('resize');
    }

  });
}
assignHeightSlick();
.row.hero-content {
height: 300px;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<script type="text/javascript" src="//cdn.jsdelivr.net/npm/slick-carousel@1.8.1/slick/slick.min.js"></script>
<link rel="stylesheet" type="text/css" href="//cdn.jsdelivr.net/npm/slick-carousel@1.8.1/slick/slick.css"/>

<div class="parent">

  <div class="row hero-content">Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book. It has survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged. It was popularised in the 1960s with the release of Letraset sheets containing Lorem Ipsum passages, and more recently with desktop publishing software like Aldus PageMaker including versions of Lorem Ipsum.</div>

  <div class="slick-wrapper">
    <div class="one">Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book.</div>
    <div class="two">It was popularised in the 1960s with the release of Letraset sheets containing Lorem Ipsum passages</div>
    <div class="three">It was popularised in the 1960s with the release of Letraset sheets containing Lorem Ipsum passages, and more recently with desktop publishing software like Aldus PageMaker including versions of Lorem Ipsum.</div>
  </div>

</div>

光滑的旋转木马建成后,我需要调用“if”条件。它应该捕获第一张幻灯片并应用此逻辑。一旦回到第一张幻灯片,我的逻辑就起作用了。

我试图初始化(.on(init))但没有工作。

标签: jqueryslick.js

解决方案


推荐阅读