【问题标题】:Slick.js - Responsive breakpoint custom functionSlick.js - 响应式断点自定义函数
【发布时间】:2016-12-13 22:48:51
【问题描述】:

我正在尝试在Slick.js 断点被触发时启动一个事件。
即使没有命中断点,也会触发init 事件。

有办法解决吗?

这是我的代码:

var $j = jQuery.noConflict();

$j(".homepage_slider").slick({
    dots: false,
    infinite: true,
    arrows:false,
    autoplay:true,
    autoplaySpeed:3500,
    slidesToShow: 1,
    slidesToScroll: 1,
    responsive: [                           
        {
          breakpoint: 480,
          settings: {
            init: changeImages()
          }
        }
    ]
});

function changeImages(){
    $j('img.slider-image').each(function() {
        $j(this).attr('src', $j(this).attr('data-mobile'));
    });                     
}

我也试过了,但是没用:

$j('.homepage_slider').on('breakpoint', function(){
    console.log("test");
    $j('img.slider-image').each(function() {
        $j(this).attr('src', $j(this).attr('data-mobile'));
    });
});

有什么想法吗?

更新

找到这个帖子:how to call different jquery actions in responsive design

var isBreakPoint = function (bp) {
    var bps = [320, 480, 768, 1024],
        w = $j(window).width(),
        min, max
    for (var i = 0, l = bps.length; i < l; i++) {
        if (bps[i] === bp) {
            min = bps[i-1] || 0
            max = bps[i]
            break
        }
    }
    return w > min && w <= max
}

if (isBreakPoint(480)) {
    $j('img.slider-image').each(function() {
        $j(this).attr('src', $j(this).attr('data-mobile'));
    });
}

此解决方法有效,但如果我找到一个在遇到Slick.js 断点事件时有效的解决方法,这样两种方法之间就没有差异,那就太好了。

【问题讨论】:

    标签: javascript html css slick.js


    【解决方案1】:

    查看Slick's documentation 的“事件”部分:

    在 slick 1.4 中,回调方法已被弃用并替换为事件。
    <...>
    断点
    参数:事件、光滑、断点。
    在遇到断点后触发。

    所以你需要采取两个步骤:

    1. 使用responsive 选项设置您需要的断点。
    2. 抓住breakpoint 事件,随心所欲。

    例如:

    var $myCarousel = $('#myCarousel');
    
    /* Step 1 */
    $myCarousel.slick({
      autoplay: true,
      dots: true,
      responsive: [
        { breakpoint: 500 },
        { breakpoint: 768 },
        { breakpoint: 992 }
      ]
    });
    
    /* Step 2 */
    $myCarousel.on('breakpoint', function(event, slick, breakpoint) {
      console.log('breakpoint ' + breakpoint);
    });
    /* Decorations */
    .my-carousel img {
      width: 100%;
    }
    .my-carousel .slick-next {
      right: 15px;
    }
    .my-carousel .slick-prev {
      left: 15px;
      z-index: 1;
    }
    <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/slick-carousel/1.7.1/slick.min.css">
    <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/slick-carousel/1.7.1/slick-theme.css">
    
    <div id="myCarousel" class="my-carousel">
      <div>
        <img src="https://via.placeholder.com/900x300/c69/f9c/?text=1" alt="">
      </div>
      <div>
        <img src="https://via.placeholder.com/900x300/9c6/cf9/?text=2" alt="">
      </div>
      <div>
        <img src="https://via.placeholder.com/900x300/69c/9cf/?text=3" alt="">
      </div>
    </div>
    
    <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script>
    <script src="https://cdnjs.cloudflare.com/ajax/libs/slick-carousel/1.7.1/slick.min.js"></script>

    【讨论】:

      【解决方案2】:
      const $slick = $(".health-slick .items");
      const $slick_item = $(".health-slick .items .item");
      
      // INIT
        slickInit();
        animateHoverItem();
      
      // FUNCTIONS
        function animateHoverItem() {
          $slick_item.mouseover(function() {
              $(this).addClass('animate');
          });
          $slick_item.mouseout(function() {
              $(this).removeClass('animate');
          });
          console.log(0); // this will call 1 time and 1 time when breakpoint window change
        }
      
        function slickInit() {
          $(document).ready(function () {
            $slick.slick({
              slidesToShow: 5,
              slidesToScroll: 1
              responsive: [
                {
                  breakpoint: 767.98,
                  settings: {
                    slidesToShow: 2,
                  },
                },
                {
                  breakpoint: 575.98,
                  settings: {
                    slidesToShow: 1,
                  },
                }
              ],
            });
      
            $slick.on('breakpoint', function(e){
              animateHoverItem();
            });
            
          });
        }
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2021-05-12
        • 1970-01-01
        • 2019-08-09
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多