简体   繁体   English

时间jQuery的fadeTo效果?

[英]Time a jQuery fadeTo Effect?

Currently I am using jQuery fadeTO to fade in a div. 目前,我正在使用jQuery fadeTO在div中淡入淡出。 However I have a minor issue of timing here. 但是,我在这里有一个小问题。 I have an Animated GIF file that I want to time with the jQuery fadeTO effect. 我有一个动画GIF文件,我想用jQuery fadeTO效果fadeTO

I have attempted to uses window.onload to not have the script fire until the page is fully loaded but while it technically works in that it does not fire the script until the page loads. 我尝试使用window.onload在页面完全加载之前不会触发脚本,但是从技术上讲,它在页面加载之前不会触发脚本。 Depending on the internet connection of the user this can have good timing or throw the timing off. 取决于用户的互联网连接,这可能会带来良好的时机或放弃时机。 I would like to time the effect with the GIF animation to have as precise as possible timing. 我想用GIF动画对效果进行定时,以使其具有尽可能精确的定时。 Here is my current script I am using: 这是我当前正在使用的脚本:

var $j = jQuery.noConflict();
   window.onload = function(){ 
    $j('#fadein').fadeTo(7500, 1, function() {
  });       
 };

My question is how do I have the script fire as soon as the GIF is fully loaded and showing on the screen? 我的问题是,当GIF完全加载并显示在屏幕上后,如何启动脚本? My thought is once the GIF is loaded and the script starts when the GIF does I can time the script with the GIF to fade in properly. 我的想法是,一旦加载了GIF,并且脚本在GIF完成后开始运行,那么我就可以将带有GIF的脚本计时为正确淡入。

I am open to other thoughts as to how to time these better. 我对如何更好地计时这些想法持开放态度。

It is impossible to time JavaScript and animated GIFs reliably. 无法可靠地为JavaScript和动画GIF计时。

The only way to get anything close is to have the individual frames as separate images (or possibly as a sprite sheet if you can), and then animate the frames manually in JavaScript. 封闭所有内容的唯一方法是将各个框架作为单独的图像(或者,如果可能的话,可以作为精灵表),然后在JavaScript中手动对框架进行动画处理。

I actually have an example of this in action for a coursework site I made: link and the relevant JS file . 实际上,对于我制作的课程网站,实际上有一个示例: 链接相关的JS文件

Here is an example of executing code after the image has loaded. 这是在图像加载后执行代码的示例。

image=new Image();
image.src="mygif.gif"
image.onload=function(){
    alert('image Loaded');
}

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

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