我一直在使用jQuery 3.1.1开发自己的JavaScript幻灯片。它工作正常,但由于某种原因,过渡动画并不流畅。当只有两张照片时,它可以正常工作,但是一旦我添加了第三张照片,它就完全不能正常工作。
我已经成了这个小提琴,如果有人可以提供一些见解,那将非常感激。
!function($) {
"use strict";
$.slideshow = function(container) {
let $container = $(container);
let children = $container.find('.slide').length;
$container.find('.slide').each(function(index) {
let zIndex = 210 - index;
let bg = $(this).data('bg');
$(this).css('background-image', 'url(' + bg + ')');
});
let settings = {
'transition': 500,
'delay': 6000,
};
let currentSlide = 0;
$.fn.showNext = function() {
let nextSlide = ((currentSlide + 1) < children) ? (currentSlide + 1) : 0;
let $current = $container.find('.slide:nth-child(' + (currentSlide + 1) + ')');
let $next = $container.find('.slide:nth-child(' + (nextSlide + 1) + ')');
$current.animate({
'opacity': 0
}, settings.transition);
setTimeout(function() {
$current.css('z-index', 200);
$current.css('opacity', 1);
$next.css('z-index', 210);
}, settings.transition + 100);
console.log(currentSlide, nextSlide);
currentSlide = ((currentSlide + 1) < children) ? (currentSlide + 1) : 0;
setTimeout(function() {
$container.showNext();
}, settings.delay);
};
setTimeout(function() {
$container.showNext();
}, settings.delay);
};
}(jQuery);
$.slideshow('.slideshow');
.slideshow-wrapper {
height: 100vh;
overflow: hidden;
position: relative;
width: 100%;
}
.slideshow {
height: 100%;
position: relative;
width: 100%;
}
.slide {
background-position: center;
background-size: cover;
height: 100%;
left: 0;
overflow: hidden;
position: absolute;
top: 0;
width: 100%;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.1.1/jquery.min.js"></script>
<div class="slideshow-wrapper">
<div class="slideshow">
<div class="slide" data-bg="https://s3.amazonaws.com/StartupStockPhotos/uploads/20160503/6.jpg"></div>
<div class="slide" data-bg="https://s3.amazonaws.com/StartupStockPhotos/uploads/20160503/5.jpg"></div>
<div class="slide" data-bg="https://s3.amazonaws.com/StartupStockPhotos/uploads/20160503/4.jpg"></div>
</div>
</div>
答案 0 :(得分:2)
jQuery代码中存在多个问题。这是我发现的:
试试这个edited JS Fiddle,修复这些问题。 顺便说一句,这是jQuery .animate方法。
CSS更改:
<强> CSS:强>
.slideshow-wrapper {
height: 100vh;
overflow: hidden;
position: relative;
width: 100%;
}
.slideshow {
height: 100%;
position: relative;
width: 100%;
}
.slide {
background-position: center;
background-size: cover;
height: 100%;
left: 0;
opacity: 0; /* <-- New --- */
overflow: hidden;
position: absolute;
top: 0;
width: 100%;
}
.slide:first-child { /* <-- New --- */
opacity: 1;
}
JavaScript有很多编辑
!function($) {
"use strict";
$.slideshow = function(selector) {
let container = $(selector);
let children = container.find('.slide').length;
container.find('.slide').each(function(index) {
let zIndex = 210 - index;
$(this).css('z-index', zIndex); // <-- This was missing, as it was loading the last image as the 1st image.
let bg = $(this).data('bg');
$(this).css('background-image', 'url(' + bg + ')');
});
let settings = {
'delay': 2000
};
let currentSlide = 0;
$.fn.showNext = function() {
let nextSlide = ((currentSlide + 1) < children) ? (currentSlide + 1) : 0;
let current = container.find('.slide:nth-child(' + (currentSlide + 1) + ')');
let next = container.find('.slide:nth-child(' + (nextSlide + 1) + ')');
next.animate({
'opacity': 1,
'z-index': 210
}, settings.transition);
// Creates the Cross-Dissolve, as 1 image "next" is fadding up from opacity 0 to 1, while current is fadding down from 1 to 0.
current.animate({
'opacity': 0,
'z-index': 200
}, settings.transition);
};
// Starts the animation loop.
setInterval(function() {
container.showNext();
currentSlide = ((currentSlide + 1) < children) ? (currentSlide + 1) : 0;
}, settings.delay);
};
}(jQuery);
setTimeout(function() {
$.slideshow('.slideshow');
}, 0); // Fixes the race condition, by allowing the HTML to load before the JS runs.
HTML - 无变化,但在使用jsfiddle时删除脚本标记
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.1.1/jquery.min.js"></script>
<div class="slideshow-wrapper">
<div class="slideshow">
<div class="slide" data-bg="https://s3.amazonaws.com/StartupStockPhotos/uploads/20160503/6.jpg"></div>
<div class="slide" data-bg="https://s3.amazonaws.com/StartupStockPhotos/uploads/20160503/5.jpg"></div>
<div class="slide" data-bg="https://s3.amazonaws.com/StartupStockPhotos/uploads/20160503/4.jpg"></div>
</div>
</div>
玩edited JS Fiddle玩得开心,以进一步自定义它!
答案 1 :(得分:0)
主要问题在于
setTimeout(function() {
$current.css('z-index', 200);
$current.css('opacity', 1); // this should be $next.css('opacity', 1);
$next.css('z-index', 210);
}
您还应该尝试摆脱3 setTimeout(),它们看起来多余。 您可以使用setInteval()来更改图像。在animate的完成回调中更改css属性,你可以去掉另一个setTimeout()。
检查一下:
!function($) {
"use strict";
$.slideshow = function(container) {
let $container = $(container);
let children = $container.find('.slide').length;
$container.find('.slide').each(function(index) {
let zIndex = 210 - index;
let bg = $(this).data('bg');
$(this).css('background-image', 'url(' + bg + ')');
});
let settings = {
'transition': 500,
'delay': 6000,
};
let currentSlide = 0;
$.fn.showNext = function() {
let nextSlide = ((currentSlide + 1) < children) ? (currentSlide + 1) : 0;
let $current = $container.find('.slide:nth-child(' + (currentSlide + 1) + ')');
let $next = $container.find('.slide:nth-child(' + (nextSlide + 1) + ')');
$current.animate({
'opacity': 0
}, settings.transition,function(){
$current.css('z-index', 200);
$next.css('opacity', 1);
$next.css('z-index', 210);
});
currentSlide = ((currentSlide + 1) < children) ? (currentSlide + 1) : 0;
};
setInterval(function() {
$container.showNext();
}, settings.delay);
};
}(jQuery);
$.slideshow('.slideshow');
.slideshow-wrapper {
height: 100vh;
overflow: hidden;
position: relative;
width: 100%;
}
.slideshow {
height: 100%;
position: relative;
width: 100%;
}
.slide {
background-position: center;
background-size: cover;
height: 100%;
left: 0;
overflow: hidden;
position: absolute;
top: 0;
width: 100%;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.1.1/jquery.min.js"></script>
<div class="slideshow-wrapper">
<div class="slideshow">
<div class="slide" data-bg="https://s3.amazonaws.com/StartupStockPhotos/uploads/20160503/6.jpg"></div>
<div class="slide" data-bg="https://s3.amazonaws.com/StartupStockPhotos/uploads/20160503/5.jpg"></div>
<div class="slide" data-bg="https://s3.amazonaws.com/StartupStockPhotos/uploads/20160503/4.jpg"></div>
</div>
</div>