简体   繁体   English

jQuery:图像淡入另一个图像

[英]jQuery: Image fading to another image

I have the following code in jQuery: 我在jQuery中有以下代码:

if (klick = true) $(this).fadeOut('fast', function(){
    $(this).attr("src", "../img/daniel_effects.png").fadeIn();
});

The changing of the image now works so: - Image1 fade-out - No image is displayed - Image2 fade-in 现在可以更改图像了:-Image1淡出-没有图像显示-Image2淡入

How can I fix this, that the images fading together, without a lil time with no image between them? 我该如何解决这些问题,使图像渐渐消失,而彼此之间没有图像的闲暇时间?

Here's the site where you can see what I mean: 在这里,您可以了解我的意思:

http://anthraxbeats.com/pages/biography.html http://anthraxbeats.com/pages/biography.html

When you're hovering on a image, theres a short empty space before the image loads in. How can I fix it? 当您将鼠标悬停在图像上时,在图像加载之前会有一个空白空间。如何解决?

Use two different images. 使用两个不同的图像。 Have them cover the same space by setting their css properties "position: absolute". 通过将其CSS属性设置为“ position:absolute”,使它们覆盖相同的空间。 Fade the first one out while setting the other one to false. 淡出第一个,同时将另一个设为false。 You may need a proper container with position: relative as position: absolute may cause them to behave...unexpectedly. 您可能需要一个具有position:relative作为position的正确容器:absolute可能导致它们的行为...出乎意料。

#container{
position: relative;
width: 100px;
height: 100px;
}
.img1, .img2{
position: absolute;
width: 100%;
}

Adding [queue: false] to the animation will allow for multiple animations at the same time 在动画中添加[queue:false]将允许同时播放多个动画

var $theOtherThing = $(this).attr("src", "../img/daniel_effects.png");

if(klick === true){
    $(this).animate({
        "opacity": "0"
     }, {
        duration: 200,
        queue: false
     });
    $theOtherThing.animate({
        "opacity": "1"
     }, {
        duration: 200,
        queue: false
    });

You can try preloading your image outside of the click handler. 您可以尝试将图片预加载到点击处理程序之外。 Something like this should work: 这样的事情应该起作用:

(new Image()).src = "../img/daniel_effects.png";

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

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