简体   繁体   English

在内容完全加载后尝试阻止功能

[英]Trying to prevent function until after content is fully loaded

Hi I'm attempting to prevent an animation from occurring until after images that are being pulled from a separate html document are fully loaded. 嗨,我试图阻止动画发生,直到从单独的html文档中拉出的图像完全加载之后。

As far as I can tell, right now, the code is saying: load div with id of "images" then run function. 据我所知,现在的代码在说:将id为“ images”的div加载,然后运行函数。 I guess what I want it to say is pull id of images, wait for images to load fully, then run function. 我想我想说的是图像的ID,等待图像完全加载,然后运行功能。 Help is much appreciated. 非常感谢您的帮助。

$('#nav a, .section a').click(function(event) {
event.preventDefault();

$('.project-detail').load(this.href + " #images", function() {
    $('.project-detail').removeClass('no-transition');
    $('.project-detail').addClass('move-left');
    $('#close-tag').addClass('desktop-close-move');
    $('#doit').addClass('close-move');
});

});

Bind a load event handler to your images which will be executed when images are loaded and thus run your animations. load事件处理程序绑定到您的图像,该事件将在加载图像并运行动画时执行。

$(your_images).load(function() {
  //run your animations here when images are loaded
});

Code that needs to be run only after everything else on the page has been fully loaded can be bound to the window like so... 只需将页面上的所有其他内容都完全加载后才需要运行的代码可以像这样绑定到窗口...

$(window).on("load", function() {
    // after everything has fully loaded, do this
});

From the jQuery docs... 从jQuery文档...

The load event is sent to an element when it and all sub-elements have been completely loaded. 当加载事件和所有子元素都已完全加载时,会将加载事件发送到该元素。 This event can be sent to any element associated with a URL: images, scripts, frames, iframes, and the window object. 该事件可以发送到与URL关联的任何元素:图像,脚本,框架,iframe和window对象。 https://api.jquery.com/load-event/ https://api.jquery.com/load-event/

Caveats of the load event when used with images 与图像一起使用时加载事件的警告

A common challenge developers attempt to solve using the .load() shortcut is to execute a function when an image (or collection of images) have completely loaded. 开发人员尝试使用.load()快捷方式解决的一个常见挑战是,在完全加载图像(或图像集合)后执行函数。 There are several known caveats with this that should be noted. 有几个已知的注意事项,应该注意。 These are: 这些是:

  • It doesn't work consistently nor reliably cross-browser 它不能始终如一地工作,也不可靠地跨浏览器
  • It doesn't fire correctly in WebKit if the image src is set to the same src as before 如果图像src设置为与以前相同的src,则无法在WebKit中正确触发
  • It doesn't correctly bubble up the DOM tree 它无法正确冒泡DOM树
  • Can cease to fire for images that already live in the browser's cache 可以停止为已经存在于浏览器缓存中的图像触发

Image Caching Issues 图像缓存问题

imagesLoaded - jQuery Plugin imagesLoaded-jQuery插件

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

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