简体   繁体   English

jQuery 淡入淡出效果未按预期工作

[英]jQuery fading effect not working as intended

I am modifying the working code from here to change image with fading effect on click.我正在修改此处的工作代码,以更改单击时具有褪色效果的图像。

This is the modded code:这是修改后的代码:

 $(function() { $("input:button").click(function() { $("img:last").fadeToggle("slow", function() { $(this).prependTo(".frame").show() }); }); }); function Forward() { $("img:first").fadeToggle("slow", function() { $(this).appendTo(".frame").show() }); } function Backward() { $("img:last").fadeToggle("slow", function() { $(this).prependTo(".frame").show() }); }
 .frame { position: relative; left: 35%; } img { position: absolute; max-width: 30%; }
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.3/jquery.min.js"></script> <h1> <input type="button" value="PrependTo" /> <button onclick="Forward()">Go Forward</button> <button onclick="Backward()">Go Backward</button> </h1> <div class="frame"> <img src="img/home-00.jpg"> <img src="img/home-01.jpg"> <img src="img/home-02.jpg"> <img src="img/home-03.jpg"> </div>

The original code, which is placed into function Backward() works properly.放置在函数Backward()的原始代码正常工作。 However, the modified function Forward() does not seem to work right.但是,修改后的函数Forward()似乎无法正常工作。 The fading effect is not observed.没有观察到衰落效应。 Also, I don't quite get why the image shown is the one at the bottom of the stack of <div class="frame"> .另外,我不太明白为什么显示的图像是<div class="frame">堆栈底部的图像。 Could someone help?有人可以帮忙吗?

This is a visual phenomenon caused by the fact that the four images are all piled one-on-top-of-the-other, in a stack.这是一种视觉现象,因为这四个图像都一个叠一个地堆叠在一起。

Because the parent div is styled relative and each image is absolute, the images all sit on top of each other, with the last image being the only visible one (because the other 3 images are underneath it).因为父 div 的样式是相对的并且每个图像都是绝对的,所以所有图像都位于彼此的顶部,最后一个图像是唯一可见的一个(因为其他 3 个图像在其下方)。

When you click the Forward button, the first image (bottom of the image pile, ie invisible) is faded out (which you cannot see, because it is at the bottom of the pile) and re-inserted PLOP!当您单击“前进”按钮时,第一张图像(图像堆的底部,即不可见)淡出(您看不到,因为它在堆的底部)并重新插入 PLOP! as the last element in the div ( appendTo() ) - which makes it the final absolutely-positioned image, therefore top-of-the-pile, which makes it visible all-of-a-sudden.作为 div 中的最后一个元素 ( appendTo() ) - 这使它成为最终的绝对定位图像,因此是堆顶,这使得它突然可见。

When you hit backwards, the last image (visible) is faded out (which you CAN see) and reinserted as the first image in the div ( prependTo() ), with all the other images piled on top of it, so you cannot see the insertion.当你向后点击时,最后一个图像(可见)淡出(你可以看到)并作为第一个图像重新插入 div( prependTo() ),所有其他图像堆积在它上面,所以你看不到插入。 All you see is the nice, gentle, fade-out into the underlying image.你所看到的只是漂亮、温和、淡出底层图像。

To improve this, try adding a fadeIn() transition to the Forward as it plops the newly-visible image onto the bottom of the div.为了改善这一点,尝试向 Forward 添加一个fadeIn()过渡,因为它将新可见的图像扑通到 div 的底部。 (Note that you don't need the fadeIn() method on the Backward func, because the insertion is not visible to the user.) (请注意,您不需要 Backward func 上的fadeIn()方法,因为插入对用户不可见。)

 $(function() { $("input:button").click(function() { $("img:last").fadeToggle("slow", function() { $(this).prependTo(".frame").fadeIn() }); }); }); function Forward() { $("img:first").fadeToggle("slow", function() { $(this).appendTo(".frame").fadeIn(800) }); } function Backward() { $("img:last").fadeToggle("slow", function() { $(this).prependTo(".frame").fadeIn() }); }
 .frame { position: relative; left: 35%; } img { position: absolute; max-width: 30%; }
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.2.4/jquery.min.js"></script> <h1> <input type="button" value="PrependTo" /> <button onclick="Forward()">Go Forward</button> <button onclick="Backward()">Go Backward</button> </h1> <div class="frame"> <img src="http://placeimg.com/240/180/animals"> <img src="http://placeimg.com/240/180/nature"> <img src="http://placeimg.com/240/180/people"> <img src="http://placeimg.com/240/180/sepia"> </div>

Update:更新:

Turns out you do need the fadeIn() on the Back button to address the shortcoming pointed out by user Obscure.原来你确实需要后退按钮上的fadeIn() 来解决用户Obs​​cure 指出的缺点。 Good eye.好眼光。

I change cssyphus 's code as follows and the fading speed back and forth seems rather even:我将cssyphus的代码更改如下,来回褪色速度似乎相当均匀:

function Forward() {
    $("img:first").fadeToggle(150, function() {
        $(this).appendTo(".frame").fadeIn(450)
    });
}
function Backward() {
    $("img:last").fadeToggle(600, function() {
        $(this).prependTo(".frame").show()
    });
}

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

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