自定义jQuery轮播过渡错误

时间:2018-08-07 15:47:18

标签: javascript jquery css css-transitions

我正在使用jQuery和CSS制作自定义图片轮播。由于 performance 的原因,我试图避免使用从Github下载的 multiple-features 轮播插件。

我的目标是获得垂直过渡,就像 www.pcgarage.ro 上的过渡一样,但是不使用(可能)已使用的插件。为此,我写了:

var $elm = $('.slider'),
  $slidesContainer = $elm.find('.slider-container'),
  slides = $slidesContainer.children('a'),
  slidesCount = slides.length,
  slideHeight = $(slides[0]).find('img').outerHeight(false);

//Set slide height
$(slides).css('height', slideHeight);

// Append bullets
for (var i = 0; i < slidesCount; i++) {
  var bullets = '<a href="#">' + i + '</a>';
  if (i == 0) {
    // active bullet
    var bullets = '<a href="#" class="activeSlide">' + i + '</a>';
    // active slide
    $(slides[0]).addClass('active');
  }
  $('.slider-nav').append(bullets);
}

// Set (initial) z-index for each slide
var setZindex = function() {
  for (var i = 0; i < slidesCount; i++) {
    $(slides[i]).css('z-index', slidesCount - i);
  }
}
setZindex();

$('.slider-nav a').on('click', function() {
  activeIdx = $(this).text();
  $('.slider-nav a').removeClass('activeSlide');
  $(this).addClass('activeSlide');
  setActiveSlide();
  slideUpDown();
});

var setActiveSlide = function() {
  $(slides).removeClass('active');
  $(slides[activeIdx]).addClass('active');
}

var slideUpDown = function() {
  // set top property for all the slides
  $(slides).css('top', slideHeight);
  // then animate to the next slide
  $(slides[activeIdx]).animate({
    'top': 0
  });
}
body {
  margin: 0;
  padding: 0;
}

body * {
  box-sizing: border-box;
}

.container {
  max-width: 1200px;
  margin: 0 auto;
}

.slider {
  width: 100%;
  height: 300px;
  position: relative;
  overflow: hidden;
}

.slider .slider-nav {
  position: absolute;
  left: 10px;
  bottom: 10px;
  z-index: 30;
}

.slider .slider-nav a {
  display: block;
  float: left;
  width: 10px;
  height: 10px;
  border-radius: 50%;
  margin-right: 3px;
  text-indent: -9999px;
  background: #fff;
}

.slider .slider-nav a.activeSlide {
  background: transparent;
  border: 2px solid #fff;
}

.slider .slider-container {
  width: 100%;
  text-align: center;
}

.slider .slider-container a {
  display: block;
  position: absolute;
  top: 0;
  left: 0;
  right: 0;
}

.slider .slider-container img {
  transform: translateX(-50%);
  margin-left: 50%;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.1.1/jquery.min.js"></script>
<div class="container">
  <div class="slider slider-homepage">
    <div class="slider-nav"></div>
    <div class="slider-container">
      <a href="#">
        <img src="https://picsum.photos/1200/300/?gravity=east" alt="">
      </a>
      <a href="#">
        <img src="https://picsum.photos/1200/300/?gravity=south" alt="">
      </a>
      <a href="#">
        <img src="https://picsum.photos/1200/300/?gravity=north" alt="">
      </a>
    </div>
  </div>
</div>

我的代码的问题是每次转换都伴随着(明显的)白屏,我不理解其原因。

我的错误在哪里?

2 个答案:

答案 0 :(得分:1)

我添加了一些变量和函数来修复此问题,请检查脚本。

var $elm = $('.slider'),
        $slidesContainer = $elm.find('.slider-container'),
        slides = $slidesContainer.children('a'),
        slidesCount = slides.length,
        slideHeight = $(slides[0]).find('img').outerHeight(false);
    
    //Set slide height
    $(slides).css('height', slideHeight);
    
    // Append bullets
    for (var i = 0; i < slidesCount; i++) {
        var bullets = '<a href="#">' + i + '</a>';
        if (i == 0) {
            // active bullet
            var bullets = '<a href="#" class="activeSlide">' + i + '</a>';
            // active slide
            $(slides[0]).addClass('active');
        }
        $('.slider-nav').append(bullets);
    }
    
    // Set (initial) z-index for each slide
    var setZindex = function () {
        for (var i = 0; i < slidesCount; i++) {
            $(slides[i]).css('z-index', slidesCount - i);
        }
    }
    setZindex();
    
    var displayImageBeforeClick = null;
    
    $('.slider-nav a').on('click', function () {
        displayImageBeforeClick = $(".slider-container .active");
        activeIdx = $(this).text();
        if($(slides[activeIdx]).hasClass("active")){ return false; }
        $('.slider-nav a').removeClass('activeSlide');
        $(this).addClass('activeSlide');
        setActiveSlide();
        slideUpDown();
    });
    
    var setActiveSlide = function () {
        $(slides).removeClass('active');
        $(slides[activeIdx]).addClass('active');
    }
    
    var slideUpDown = function () {
        // set top property for all the slides
        $(slides).not(displayImageBeforeClick).css('top', slideHeight);
        // then animate to the next slide
        $(slides[activeIdx]).animate({
            'top': 0
        });
    
        $(displayImageBeforeClick).animate({
            'top': "-100%"
        });
    }
body {
  margin: 0;
  padding: 0;
}

body * {
  box-sizing: border-box;
}

.container {
  max-width: 1200px;
  margin: 0 auto;
}

.slider {
  width: 100%;
  height: 300px;
  position: relative;
  overflow: hidden;
}

.slider .slider-nav {
  position: absolute;
  left: 10px;
  bottom: 10px;
  z-index: 30;
}

.slider .slider-nav a {
  display: block;
  float: left;
  width: 10px;
  height: 10px;
  border-radius: 50%;
  margin-right: 3px;
  text-indent: -9999px;
  background: #fff;
}

.slider .slider-nav a.activeSlide {
  background: transparent;
  border: 2px solid #fff;
}

.slider .slider-container {
  width: 100%;
  text-align: center;
}

.slider .slider-container a {
  display: block;
  position: absolute;
  top: 0;
  left: 0;
  right: 0;
}

.slider .slider-container img {
  transform: translateX(-50%);
  margin-left: 50%;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.1.1/jquery.min.js"></script>

<div class="container">
  <div class="slider slider-homepage">
    <div class="slider-nav"></div>
    <div class="slider-container">
      <a href="#">
        <img src="https://picsum.photos/1200/300/?gravity=east" alt="">
      </a>
      <a href="#">
        <img src="https://picsum.photos/1200/300/?gravity=south" alt="">
      </a>
      <a href="#">
        <img src="https://picsum.photos/1200/300/?gravity=north" alt="">
      </a>
    </div>
  </div>
</div>

答案 1 :(得分:0)

将过渡添加到具有过渡持续时间和过渡计时功能的“ .slider .slider-container a”中。...供参考,您可以查看https://www.w3schools.com/css/css3_transitions.asp