简体   繁体   English

图像加载时的动画不透明度

[英]Animate opacity on image load

I have tried a bunch of different solutions to similar problems on here but none of them seem to be doing anything for me.我在这里尝试了许多不同的解决方案来解决类似的问题,但它们似乎都没有为我做任何事情。 See my jsFiddle to see an example of what I would like to happen: http://jsfiddle.net/Amp3rsand/HSNY5/6/请参阅我的 jsFiddle 以查看我希望发生的事情的示例: http : //jsfiddle.net/Amp3rsand/HSNY5/6/

It animates how I would like but it relies on .delay when I would prefer it to fire as soon as the image is finished loading.它以我想要的方式设置动画,但它依赖于 .delay 当我希望它在图像加载完成后立即触发。 The commented out sections in the js are the things that I have tried. js中注释掉的部分是我尝试过的东西。

Could the problem be that the image is actually the background of a div rather than its own element?问题可能是图像实际上是 div 的背景而不是它自己的元素吗? I tested making it its own <img> tag as well but it didn't seem to make a difference.我也测试了它自己的<img>标签,但它似乎没有什么区别。 I have the image as the background so that when I use media queries it is easy to swap in a different, smaller image for mobile users or small screens.我将图像作为背景,这样当我使用媒体查询时,很容易为移动用户或小屏幕交换不同的、较小的图像。

HTML: HTML:

<header></header>
<div id="image">
    <div id="blah"></div>
</div>

The image I would like to fire after it finishes loading is the background of '#image'.我想在加载完成后触发的图像是“#image”的背景。 Then I would like for it to animate to 'opacity:1;'然后我希望它动画为'opacity:1;' while '#blah' and 'header' are animated into place.而 '#blah' 和 'header' 动画就位。

Here is the jQuery I'm using right now but it is not correct:这是我现在正在使用的 jQuery,但它不正确:

$('#image').hide().delay(800).fadeTo(600, 1);
$('#blah').css("left", "-650px").delay(1400).animate({left:'30px'},400);
$('header').css("top", "-150px").delay(2000).animate({top:'-5px'},400);

On my website it is quite a large image so it takes about half a second to load but this code doesn't take into account caching or different network speeds.在我的网站上,它是一个相当大的图像,因此加载大约需要半秒钟,但此代码未考虑缓存或不同的网络速度。

What am I doing wrong or what should I do differently?我做错了什么或者我应该怎么做?

Thanks everyone感谢大家


EDIT:编辑:

I gave the imagesLoaded plugin a go earlier and it seems to work on my website but I can't tell if it is actually doing what I want or just emulating my code from above.我更早地使用了 imagesLoaded 插件,它似乎可以在我的网站上运行,但我不知道它是否真的在做我想做的事情,还是只是从上面模拟我的代码。

$(document).ready(function() {
    $('body').hide().fadeTo(500, 1)
});
imagesLoaded( document.querySelector('#homeimg'), function( instance ) {
  $('article').hide().fadeTo(600, 1);
  $('#caption').css("left", "-650px").delay(800).animate({left:'30px'},400);
$('header').css("top", "-150px").delay(1400).animate({top:'-5px'},400);
});

'#homeimg' being the div with the image as the background and 'article' being the container for '#homeimg' and '#caption' '#homeimg' 是图像作为背景的 div,'article' 是 '#homeimg' 和 '#caption' 的容器

I can only test with the website loaded locally at the moment so I can't simulate a slow connection.我目前只能使用本地加载的网站进行测试,因此无法模拟慢速连接。 Does the code above do what I am looking for?上面的代码是否符合我的要求? Sorry if it should be obvious对不起,如果它应该很明显

Your image is loaded via a CSS background property, you will not be able to detect the loading of that.您的图片是通过 CSS 背景属性加载的,您将无法检测到它的加载。 Why not use <img> tag for images?为什么不为图像使用<img>标签?

If you use <img> you can read this question: Browser-independent way to detect when image has been loaded for a bullet-proof solution.如果您使用<img>您可以阅读这个问题: Browser-independent way to detection when image has been loaded for an bullet-proof solution。

If you insist on using a background CSS property you will need to implement a way of sending your image as a data url encoded as base64 as described in this answer: https://stackoverflow.com/a/13989806/2788如果您坚持使用background CSS 属性,则需要实现一种将图像作为编码为 base64 的数据 url 发送的方法,如本答案所述: https : //stackoverflow.com/a/13989806/2788

尝试动画

$('#image').animate({ opacity: '1'}, 600);

You can set the initial opacity to 0, and when the image onload ed, set the opacity to 1. With CSS you can make a transition between the two states:您可以将初始不透明度设置为 0,当图像onload ,将不透明度设置为 1。使用 CSS 您可以在两种状态之间进行转换:

 <style> .easeload{ opacity: 0; -webkit-transition: all 2s ease; -moz-transition: all 2s ease; -ms-transition: all 2s ease; -o-transition: all 2s ease; } </style> <img class="easeload" onload="this.style.opacity=1" src="https://dummyimage.com/320x240">

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

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