如何使Slick Slider高度响应?

时间:2015-10-15 20:07:01

标签: javascript jquery css slick.js

我有一个工作光滑滑块(http://kenwheeler.github.io/slick/),但无法获得响应的高度。无论我缩小浏览器的大小,图像始终保持相同的高度。我认为“mobileFirst”设置可以解决这个问题,但似乎并没有。有谁知道如何使滑块和图像内的响应?这是我的代码:

<!DOCTYPE html>
<html>
    <head>
        <meta charset="utf-8">
        <meta http-equiv="X-UA-Compatible" content="IE=edge">
        <meta name="viewport" content="width=device-width, initial-scale=1">
        <!-- The above 3 meta tags *must* come first in the head; any other head content must come *after* these tags -->
        <title>Bailey Miller</title>
        <link rel="stylesheet" href="css/normalize.min.css">
        <link rel="stylesheet" href="css/main.css">
        
        <link rel="stylesheet" type="text/css" href="slick/slick.css"/>
        <link rel="stylesheet" type="text/css" href="slick/slick-theme.css"/>
          <style>
              body{
                  background:black;
              }
          </style>
    </head>
    <body>
		

		<div class="variable-width">
			<div><img src="img/wedding/7.jpg"/></div>
			<div><img src="img/wedding/8.jpg"/></div>
			<div><img src="img/wedding/9.jpg"/></div>
			<div><img src="img/wedding/10.jpg"/></div>
			<div><img src="img/wedding/12.jpg"/></div>
			<div><img src="img/wedding/13.jpg"/></div>
			<div><img src="img/wedding/22.jpg"/></div>
			<div><img src="img/wedding/23.jpg"/></div>
			<div><img src="img/wedding/24.jpg"/></div>
			<div><img src="img/wedding/25.jpg"/></div>
			<div><img src="img/wedding/28.jpg"/></div>
			<div><img src="img/wedding/29.jpg"/></div>
			<div><img src="img/wedding/30.jpg"/></div>
		</div>
        
<!-- jQuery-->
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.3/jquery.min.js"></script>
        
<!--menu js-->
    <script src="menu/script.js"></script>
        
<!--    slick-->
  <script type="text/javascript" src="http://code.jquery.com/jquery-migrate-1.2.1.min.js"></script>
  <script type="text/javascript" src="slick/slick.min.js"></script>
    <script type="text/javascript">
    $(document).ready(function(){
        $('.variable-width').slick({
            mobileFirst: true,
          infinite: true,
          speed: 300,
          slidesToShow: 1,
          centerMode: false,
          variableWidth: true,
            focusOnSelect: true
            
        });
    });
  </script>
    
        
    </body>
</html>

我的CSS:

/* Slider */
.slick-slider
{
    position: relative;
    display: block;

    -moz-box-sizing: border-box;
         box-sizing: border-box;

    -webkit-user-select: none;
       -moz-user-select: none;
        -ms-user-select: none;
            user-select: none;

    -webkit-touch-callout: none;
    -khtml-user-select: none;
    -ms-touch-action: pan-y;
        touch-action: pan-y;
    -webkit-tap-highlight-color: transparent;
}

.slick-list
{
    position: relative;

    display: block;
    overflow: hidden;

    margin: 0 20px;
    padding: 0;
}
.slick-list:focus
{
    outline: none;
}
.slick-list.dragging
{
    cursor: pointer;
    cursor: hand;
}

.slick-slider .slick-track,
.slick-slider .slick-list
{
    -webkit-transform: translate3d(0, 0, 0);
       -moz-transform: translate3d(0, 0, 0);
        -ms-transform: translate3d(0, 0, 0);
         -o-transform: translate3d(0, 0, 0);
            transform: translate3d(0, 0, 0);
}

.slick-track
{
    position: relative;
    top: 0;
    left: 0;

    display: block;
}
.slick-track:before,
.slick-track:after
{
    display: table;

    content: '';
}
.slick-track:after
{
    clear: both;
}
.slick-loading .slick-track
{
    visibility: hidden;
}

.slick-slide
{
    display: none;
    float: left;

    min-height: 1px;
}
[dir='rtl'] .slick-slide
{
    float: right;
}
.slick-slide img
{
    display: block;
}
.slick-slide.slick-loading img
{
    display: none;
}
.slick-slide.dragging img
{
    pointer-events: none;
}
.slick-initialized .slick-slide
{
    display: block;
}
.slick-loading .slick-slide
{
    visibility: hidden;
}
.slick-vertical .slick-slide
{
    display: block;

    height: auto;

    border: 1px solid transparent;
}
.slick-arrow.slick-hidden {
    display: none;
}

5 个答案:

答案 0 :(得分:1)

我遇到了同样的问题 - 删除变量宽度解决了问题并将内容缩放到浏览器窗口。通过响应选项,您可以为不同的断点提供不同的插件选项。因此,您可以保持较大屏幕的可变宽度,并在需要响应时进行相应更改。

$(document).ready(function(){
    $('.variable-width').slick({
          mobileFirst: true,
          infinite: true,
          speed: 300,
          slidesToShow: 1,
          centerMode: false,
          variableWidth: false,
          focusOnSelect: true,
          responsive: [
                {
                  breakpoint: 1024,
                  settings: {
                      mobileFirst: true,
                      infinite: true,
                      speed: 300,
                      slidesToShow: 1,
                      centerMode: false,
                      variableWidth: true,
                      focusOnSelect: true

                  }
                }
              ]
    });
});

我在问题上找到了以下页面:https://github.com/kenwheeler/slick/issues/1024

答案 1 :(得分:1)

如果您知道图像的宽度和高度,请计算图像的纵横比并创建变量:

$aspectRatio: width / height;

然后你可以使用这个简单的解决方案:

.slick-slider{ max-width: calc(100vh * #{$aspectRatio});}

这使滑块宽度根据视口高度而变化,滑块高度将按比例变化。只需根据需要更改值。

答案 2 :(得分:0)

我不太确定Slick Slider是如何进行标记的,但是当我使用类似的js插件时,我不得不添加另一个函数来强制浏览器在窗口调整大小时重绘图像。

这是我使用的功能:

var debounceTimeout;
$(window).on("resize", function(){
    clearTimeout(debounceTimeout);
    debounceTimeout = setTimeout(function(){
        $(".ps-current img:visible").css("max-height","99.9%");
        setTimeout(function(){
            $(".ps-current img:visible").removeAttr("style");
        }, 10)
    }, 300);
});

图片的CSS只是:

.slider img {
    max-width: 100%;
    max-height: 100%;
}

请记住,要为元素指定相对高度(以百分比表示的高度),它的父元素必须具有已定义的高度。

要让一个元素占据浏览器的100%,该元素及其所有父元素(最多bodyhtml)需要在其css样式中使用height:100%;

答案 3 :(得分:0)

我用过

.slick-slide img{max-width: 60vw;}

调整最大宽度以找到特定项目的良好平衡。

与Slick的响应断点配合使用可获得更好的效果。

答案 4 :(得分:0)

尝试这些代码

$('.mySlides').slick({<br>
      autoplay:true,<br>
      dots: true,<br>
      infinite: true,<br>
      speed: 300,<br>
      slidesToShow: 1,<br>
      responsive: [{breakpoint:1024},{breakpoint:600}]<br>
      });<br><br>
 $('.mySlides').on('breakpoint', function(event, slick, breakpoint){<br>
    $('.slick-slide').css('height',$('.slick-current img').height());<br>
 });