如何在光滑的滑块中垂直对齐图像

时间:2019-05-07 09:29:51

标签: html css

我有一个光滑的滑块,我想垂直对齐 a元素。我尝试了vertical-align:middle;的高度:100%;并尝试了更多,但我还没有找到解决方案。我也尝试使用<span class="helper"></span,就像在stackoverflow(this)上的jsfiddle解决方案中找到的那样。我希望你们能帮助我。

我的滑块HTML如下:

.slick-slide {
    margin: 0px 20px;
}

.slick-slide img {
    width: 100%;
}

.slick-slider
{
    position: relative;
    display: block;
    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;
    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;
    height: 100%;
    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;
}
<div class="container-fluid">
  <h2 class="text-center text-secondary">Partners</h2>
   <section class="customer-logos slider">
      <a href="#" target="_blank" class="slide"><img src="https://cdn.pixabay.com/photo/2016/08/26/17/33/landscape-1622739_960_720.jpg"></a>
      <a href="#" target="_blank" class="slide"><img src="https://cdn.pixabay.com/photo/2017/02/15/02/52/tree-2067496__340.jpg"></a>
      <a href="#" target="_blank" class="slide"><img src="https://cdn.pixabay.com/photo/2016/08/26/17/33/landscape-1622739_960_720.jpg"></a>
      <a href="#" target="_blank" class="slide"><img src="https://cdn.pixabay.com/photo/2017/02/15/02/52/tree-2067496__340.jpg"></a>
      <a href="#" target="_blank" class="slide"><img src="https://cdn.pixabay.com/photo/2016/08/26/17/33/landscape-1622739_960_720.jpg"></a>
      <a href="#" target="_blank" class="slide"><img src="https://cdn.pixabay.com/photo/2017/02/15/02/52/tree-2067496__340.jpg"></a>
      <a href="#" target="_blank" class="slide"><img src="https://cdn.pixabay.com/photo/2016/08/26/17/33/landscape-1622739_960_720.jpg"></a>
      <a href="#" target="_blank" class="slide"><img src="https://cdn.pixabay.com/photo/2017/02/15/02/52/tree-2067496__340.jpg"></a>
   </section>
</div>

当然包括jQuery和slick.js。 您可以看到演示权here,但带有正方形图像。我的图像不是方形的,因此我需要在水平滑块中垂直对齐图像。

谢谢!

2 个答案:

答案 0 :(得分:1)

将此添加到您的Jquery。如果添加以下代码,它将解决您的问题。 它给所有图像一个高度和一个位置。

$( ".slick-slide" ).each(function( index ) { 
   $( this ).css('margin-top',
   ($('.slider').height()-$(this).height())/2+'px' );
 });

答案 1 :(得分:0)

在您的滑动功能中

\d{4}

});

$(document).ready(function(){ $('.customer-logos').slick({ slidesToShow: 6, slidesToScroll: 1, autoplay: true, autoplaySpeed: 1500, arrows: false, dots: false, vertical: true, pauseOnHover: false, responsive: [{ breakpoint: 768, settings: { slidesToShow: 4 } }, { breakpoint: 520, settings: { slidesToShow: 3 } }] }); <---仅添加此内容。这会将您的图片设置为垂直