简体   繁体   English

在图像交换功能中添加淡入淡出效果

[英]Adding Fadein Fadeout to Image Swap Function

Im am trying to fade in and fade out a transition between an image and a gif as they are swapped. 我正在尝试淡入和淡出图像和gif之间的过渡。 I have tried using CSS3 transition with opacity, but it fades the gif to nothing. 我已经尝试过使用CSS3过渡来实现不透明度,但是它会使gif消失了。 I have tried using the fadeIn() jQuery function, but I haven't been successful. 我试过使用fadeIn()jQuery函数,但没有成功。 Can someone help/point me in the right direction? 有人可以帮助/指出我正确的方向吗?

fiddle 小提琴

code: 码:

html
<figure>
  <img src="http://reneinla.com/tasha/style/images/stills/FORD-SUPER-BOWL.jpg" data-alt-src="http://reneinla.com/tasha/style/images/gifs/giphy.gif"/>
</figure>
<figure>
  <img src="http://reneinla.com/tasha/style/images/stills/OPERATOR.jpg" data-alt-src="http://reneinla.com/tasha/style/images/gifs/giphy.gif"/>
</figure>
<figure>
  <img src="http://reneinla.com/tasha/style/images/stills/OPERATOR.jpg" data-alt-src="http://reneinla.com/tasha/style/images/gifs/giphy.gif"/>
</figure>
<figure>
  <img src="http://reneinla.com/tasha/style/images/stills/FORD-SUPER-BOWL.jpg" data-alt-src="http://reneinla.com/tasha/style/images/gifs/giphy.gif"/>
</figure>

javascript:
    var sourceSwap = function () {
        var $this = $(this);
        var newSource = $this.data('alt-src');
        $this.data('alt-src', $this.attr('src'));
        $this.attr('src', newSource);
    }

    $(function() {
        $('img[data-alt-src]').each(function() { 
            new Image().src = $(this).data('alt-src');
        }).fadeIn().hover(sourceSwap, sourceSwap);
    });

Thanks! 谢谢!

You cannot fade between source changes. 您无法在源更改之间淡入淡出。

What you would need to do is create two img elements (with the same position) and hide one. 您需要做的是创建两个img元素(位置相同)并隐藏一个。

On hover, transition the opacity on the currently visible img to 0 whilst transitioning the currently hidden img to 1. 悬停时,将当前可见img的不透明度转换为0,同时将当前隐藏的img转换为1。

The benefit to this method is that it can all be done with CSS. 这种方法的好处是可以全部使用CSS来完成。 I've kept your $.each function to create the additional img elements to in the example but if it were me i'd add the extra img to your html and do this purely with CSS. 在示例中,我保留了$ .each函数来创建其他img元素,但是如果是我,我会在您的html中添加额外的img并纯粹使用CSS来完成。

img { transition: opacity 1s;}

A slightly simplified example is here: (updated your fiddle) 这里是一个稍微简化的示例:( 更新了小提琴)

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

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