我是jQuery的新手,我有一个问题,我相信,对于你们中的一些人来说,这将是一块蛋糕。 :)
我创建了一个自定义图像滑块,它几乎可以像我希望的那样工作,除了一件事。只有在通过所有幻灯片后才会显示每个幻灯片转换应发生的效果。在此之前,他们立即显示 - 完全没有效果。
以下是JSFiddle链接,下面是代码段。
$(document).ready(function () {
var slideHovered = null;
var slideDuration = 7000;
var slideEffectDuration = 2500;
//Initial hiding of inactive slides - is it actually necessary?
$('.slide:not(.active)').each(function () {
$(this).css({
"visibility": "hidden"
});
});
//Check if mouse is hovering over slider
setInterval(function () {
slideHovered = $('.leftContentWrapper').is(":hover");
}, 500);
//Function to start slideshow
function startSlides(slideDirection) {
var slideNumber = $('.slide.active').data('slide');
var highestSlideNumber = null;
//Function to find highest slide number
$('.slide').each(function () {
var value = parseFloat($(this).attr('data-slide'));
highestSlideNumber = (value > highestSlideNumber) ? value : highestSlideNumber;
});
slideNumber = slideDirection == 'next' ? slideNumber = slideNumber + 1 : slideNumber = slideNumber - 1;
slideNumber = slideNumber <= 0 ? slideNumber = highestSlideNumber : slideNumber = slideNumber;
slideNumber = slideNumber > highestSlideNumber ? slideNumber = 1 : slideNumber = slideNumber;
$('.slide.active').removeClass('active').hide();
$('[data-slide="' + slideNumber + '"]').addClass('active').fadeIn(slideEffectDuration, function () {});
$('.slide.active').css({
'visibility': 'visible'
});
}
//Function to change slide every x seconds if there is no hovering over
setInterval(function () {
if (slideHovered === false) {
startSlides('next');
} else {
return false;
}
}, slideDuration);
$('.control').click(function () {
var slideDirection = $(this).attr('id');
startSlides(slideDirection);
});
});
P.S。任何其他代码建议或建议表示赞赏。
答案 0 :(得分:1)
您的初始隐藏是问题,请将其更改为:
$(this).css({
"display": "none"
});