简体   繁体   English

如何使用fadeIn()过渡到最后一张幻灯片

[英]How do I transition to the last slide using fadeIn()

I made a script in order to create this "slideshow": http://jsfiddle.net/LWBJG/2/ 我创建了一个脚本来创建这个“幻灯片”: http//jsfiddle.net/LWBJG/2/

yet I can not go back to the last slide if I hit "previous" when it is on the first slide 但如果我在第一张幻灯片上点击“上一张”,我就不能回到最后一张幻灯片了

I tried using the last-child selector but had no success 我尝试使用last-child选择器但没有成功

I also tried resetting the count directly like so: 我也试过直接重置计数:

 if(count==1){    
    count=2;
    $(".slideshow :nth-child("+count+")").fadeIn(); 
}

I've been stuck with this for two days, I'm trying to understand what I'm doing wrong! 我已经坚持了两天,我正在努力理解我做错了什么!

all I what's left to do is to go to the last slide if I hit "previous" while I'm "standing" on the first slide 剩下要做的就是在我第一张幻灯片上“站着”的时候,如果我点击“上一张”,则转到最后一张幻灯片

First you need to hide all the other .slideshow img elements not being shown when your page first loads. 首先,您需要隐藏页面首次加载时未显示的所有其他.slideshow img元素。 You can do that multiple ways, but here's an example: 你可以采用多种方式,但这是一个例子:

$(".slideshow img:not(:nth-child(" + count + "))").hide();

Next, you need to hide the current showing slide when going to the previous one: 接下来,您需要在转到上一个时隐藏当前显示的幻灯片:

$(".slideshow :nth-child(" + count + ")").fadeOut();

Finally, you need to set the count to the number of elements in .slideshow img when going to the last image: 最后,您需要在.slideshow img最后一个图像时将计数设置为.slideshow img的元素数:

count = $(".slideshow img").length;

Here's a working example: http://jsfiddle.net/LWBJG/22/ 这是一个有效的例子: http//jsfiddle.net/LWBJG/22/

You simply were not fading out in the prev loop like you were in the next button loop. 你只是没有像在下一个按钮循环中那样淡出prev循环。

$(".slideshow :nth-child("+count+")").fadeOut()

Also, in the css, i put the following code: 另外,在css中,我输入以下代码:

.slideshow > img:not(:first-child) { display:none;}

This makes only the first img appear as default, and then the other ones will fade in as necessary. 这使得只有第一个img显示为默认值,然后其他的img将根据需要淡入。 The way you currently have it, is count is updating properly but your images are inline and are appearing behind the current image number 1.. But on your next loop click, you properly fade out the images. 你现在拥有它的方式是计数正确更新,但你的图像是内联的,并且出现在当前图像编号1后面。但是在你的下一个循环点击时,你正确淡出了图像。

Hope this helps. 希望这可以帮助。 Here is the http://jsfiddle.net/LWBJG/18/ 这是http://jsfiddle.net/LWBJG/18/

Using multiple fadeIn() s might lead to a mess. 使用多个fadeIn()可能会导致混乱。 In your case, if you really want to do so, you can first fadeOut() all images before fadeIn() the desired one. 在你的情况下,如果你真的想这样做,你可以先在fadeIn()之前fadeOut()所有图像。 This solves the problem. 这解决了这个问题。

$(".slideshow img").fadeOut(); 
$(".slideshow :nth-child("+count+")").fadeIn(); 

hide() should also work. hide()也应该有效。

Yes i know, i'am late to the party, but I played around a bit. 是的,我知道,我来晚了,但我玩了一下。 Maybe someone will find the solution useful later on. 也许有人会在以后找到解决方案。

Here is a demo: http://jsfiddle.net/NicoO/LWBJG/25/ 这是一个演示: http//jsfiddle.net/NicoO/LWBJG/25/

var $slideContainer = $(".slideshow")
var totalSlides = getAllSlides().length;
var slideIndex = 0;

function getAllSlides(){
    return $slideContainer.children("img");
}

function getSlide(index){
    return $slideContainer.children().eq(index);
}

function slideExists(index){
    return getSlide(index).length;
}

function getCurrentSlide(){
   return getSlide(slideIndex);
}

function animateSlide(){
   getAllSlides().fadeOut();
   getCurrentSlide().fadeIn(); 
}

$("#next").on("click", function () {
   slideIndex++;
   if(!slideExists(slideIndex))
       slideIndex = 0;

    animateSlide();    

});

$("#prev").on("click", function () {
   slideIndex--;
   if(!slideExists(slideIndex))
       slideIndex = totalSlides-1;

    animateSlide();
});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM