【发布时间】:2014-01-10 21:41:12
【问题描述】:
编辑:下面是我经过一番折腾后得到的代码。
这是一个基本的轮播,可以自动设置每张幻灯片的样式,为每个项目提供一个可用于控制内容的类,如果需要,可以选择在悬停时暂停,使用每个幻灯片上的 data 属性设置背景图片div,也适用于同一页面上的多个实例。
只需要通过以下方式调用它: $('#divName,#divName,#divName').startTheCaro();
解决方案:
(function($) {
$.fn.startTheCaro = function() {
return this.each(function() {
var $this = $(this);
var $thisChild = $this.children();
var theTransitionSpeed = 500; // Transition Speed
var theTransitionDelay = 3000; // Amount Of Time Slide Is Shown
var pauseOnHover = true; // Pause The Slide When Hovered // true or false
var thisImagePath = 'images/careers/'; // Background Image Path
var thisSlide = 0;
var thisSlideNum = 0;
var hoverPause = false;
var caroIsPlaying = false;
var newSlideName = 'thisCaroSlide';
var theSlidesTotal = $this.children().length-1;
// Setup each slide
$this.children().each(function() {
var $tC = $(this);
$tC.css('display', 'none');
var thisDataBgPath = $tC.data('bg');
// Set styles and classes
$(this).addClass('thisCaroSlide' + thisSlideNum);
$tC.css('position', 'absolute');
$tC.css('top', '0');
$tC.css('left', '0');
$tC.css('background', 'url(' + thisImagePath + thisDataBgPath + ') no-repeat top center');
$tC.css('background-position','center '+((topDist($(this))/$(window).height())*(-1*parAmount))+'px');
thisSlideNum++;
});
var playTheCaro = function() {
var getTheClass = ('.thisCaroSlide' + thisSlide);
if (hoverPause == true) {
// Pause on hover
} else {
if (thisSlide == theSlidesTotal) {
$this.find(getTheClass).fadeIn(theTransitionSpeed, function(){
$this.find('div:not(:first-child, :last-child)').css('display', 'none');
thisSlide++;
});
} else if (thisSlide == (theSlidesTotal+1)) {
$this.find('.thisCaroSlide' + theSlidesTotal).fadeOut(theTransitionSpeed, function(){
});
thisSlide = 1;
} else {
$this.find(getTheClass).fadeIn(theTransitionSpeed, function(){
});
thisSlide++;
};
};
};
playTheCaro();
// Pause the Carousel on Hover
var refreshIntervalId = setInterval(playTheCaro, theTransitionDelay);
$this.hover(function() {
if (pauseOnHover == true) {
refreshIntervalId = clearInterval(refreshIntervalId);
hoverPause = true;
}
}, function() {
if (pauseOnHover == true) {
refreshIntervalId = setInterval(playTheCaro, theTransitionDelay);
hoverPause = false;
}
});
});
};
}(jQuery));
【问题讨论】:
-
将其淡入
.each。theCurrentSlide.delay(i*2000).fadeIn() -
所以,如果我不理解用例,我深表歉意。因此,您将有(假设 3 个)幻灯片放映。只会出现一张图像(这就是我们想要延迟的原因)?或者这是一种级联的淡入淡出效果,所有图像在页面上一个接一个地淡入淡出?
-
对不起,这只是一个基本的轮播。图像一个在另一个之上。图像淡入最后一个。他们使用视差布局,并希望中间部分成为轮播)
-
那么,本质上,1 个幻灯片放映 3 个幻灯片放映?
-
感谢 Nicholas 最终选择了不同的路线,但非常感谢您的帮助。
标签: javascript jquery arrays