简体   繁体   English

css3图像淡化

[英]css3 image fadein

I'm trying to have images fade in with css3 once they're loaded. 我试图让图像在加载后用css3淡入。 The problem is the way my methods are currently chained it fades it in and out for a split second twice. 问题是我的方法当前被链接的方式,它会两次淡入淡出。 instead of just being blank and fading in. 而不只是空白和淡入。

my solution was to try and split out the animation code into a seperate class that i apply AFTER i initially set the opacity to zero (i do this in JS so people without js enabled can still see the images). 我的解决方案是尝试将动画代码拆分成一个单独的类,我应用AFTER我最初将不透明度设置为零(我在JS中这样做,所以没有启用js的人仍然可以看到图像)。

It's still not working though 它仍然没有工作

I assume its because in this code its setting the opacity to zero and immediately adding an animation transition class which somehow catches the opacity .css() method while its changing still (dont know how this is possible,... shouldnt it complete opacity before moving on to add class?) 我假设它是因为在这段代码中它将不透明度设置为零并立即添加一个动画过渡类,它以某种方式捕获不透明度.css()方法,同时它仍在变化(不知道这是怎么可能的,...不应该在之前完成不透明度继续添加课程?)

// nice thumbnail loading
$('.thumb').css('opacity','0').addClass('thumb-animated').on('load', function(){
    $(this).css('opacity','1');
});   


.resources .thumb-animated {
    -webkit-transition: opacity .2s;
       -moz-transition: opacity .2s;
        -ms-transition: opacity .2s;
         -o-transition: opacity .2s;
            transition: opacity .2s;
}   

Well... 好...

Why do you set opacity to 1 in jQuery? 为什么在jQuery中将opacity设置为1? If you want to use CSS3 and not simply fadeIn(200) why don't you add "opacity: 1" to css class thumb-animated? 如果你想使用CSS3而不是简单的fadeIn(200)为什么不在css类中添加“opacity:1”拇指动画?

EDIT: 编辑:

Note that load will not be triggered if the image is already in cache. 请注意,如果图像已在缓存中,则不会触发加载。
Also, !important has to be added to rewrite the rule modified via javascript. 此外,还必须添加!important来重写通过javascript修改的规则。

There you go: http://jsfiddle.net/enTCe/5/ This seems to work perfectly outside JSfiddle, on JSfiddle looks like it waits for all the images to be loaded. 在那里你去: http//jsfiddle.net/enTCe/5/这似乎在JSfiddle之外完美地工作,在JSfiddle看起来它等待所有图像被加载。

What about using just css animations? 使用css动画怎么样? No JS code is needed. 不需要JS代码。

        @-webkit-keyframes opacityChange {
            0%   { opacity: 0; }
            100% { opacity: 1; }
        }
        @-moz-keyframes opacityChange {
            0%   { opacity: 0; }
            100% { opacity: 1; }
        }
        @-ms-keyframes opacityChange {
            0%   { opacity: 0; }
            100% { opacity: 1; }
        }
        .thumb {
            width: 200px;
            height: 200px;
            opacity: 1;
            -webkit-animation: opacityChange 5s;
            -moz-animation:    opacityChange 5s;
            -ms-animation:     opacityChange 5s;
        }

You can wait adding the class to the image is loaded 您可以等待添加该类以加载图像

$('.thumb').css('opacity','0').on('load', function(){
    $(this).addClass('thumb-animated').css('opacity','1');
});

Try something like this: 尝试这样的事情:

$('#thumb').hide();

myImg = $('<img>').attr('src', 'thumb.png').load(function(){
    $('#thumb').html(myImg).fadeIn(200);
});

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

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