简体   繁体   English

jQuery在资产加载时运行

[英]jQuery run when assets load

I have a big image which is full width and full height, I have a jQuery script with document.ready but the script runs still before the image is loaded. 我有一个全宽和全高的大图像,我有一个带有document.ready的jQuery脚本,但是该脚本在图像加载之前仍然运行。 Can I prepare the statement in another way so the image loads. 我可以用另一种方式准备语句,以便加载图像。

$(document).ready(function(){
var currWidth = $(window).width();
console.log(currWidth);

var startPos = -100;
var endPos = (currWidth / 2) + (startPos / 2);
console.log(endPos);
$('#welcome').animate({left: endPos}, 1000);
});

http://jsfiddle.net/KtUEL/151/ http://jsfiddle.net/KtUEL/151/

$(window).load(function() { $(window).load(function(){

This instead of document.ready this fully loads the dom and all assets document.ready just loads the page. 这将代替document.ready完全加载dom和所有资产document.ready仅加载页面。

thanks! 谢谢!

Solved 解决了

You can also do this as part of the html of your image via the onload event: 您也可以通过onload事件将其作为图像html的一部分来执行:

<img src="..." onload="myFunction()" />

This will trigger myFunction when that particular image is loaded rather than when the entire page has loaded. 当加载特定图像而不是加载整个页面时,这将触发myFunction。

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

相关问题 如何使加载程序仅在页面资产首次加载时才运行,而不是在它们被缓存后才运行? - How to make loader only run when page assets load the first time - but not after they get cached? 在 Rails 6 jquery 中加载主题资产和资产加载顺序 - Loading theme assets in rails 6 jquery and assets load order 当在页面中插入html时,Emberjs不会加载jquery / javascript,运行代码 - Emberjs will not load jquery/javascript, run code when html is inserted in the page 使用jquery / javascript加载页面时运行click事件 - Run a click event when page is load using jquery / javascript 表单提交后运行时,jQuery load()使浏览器崩溃 - jquery load() crashes browser when run after form submission div加载时无法运行jquery脚本,在DOM加载之前运行jquery代码 - impossible to run jquery script when div is loaded, run jquery code before DOM load 使用文件加载器从webpack的jQuery UI加载资产 - Load assets from jQuery UI in webpack using file loader Rails:无法从正确的资产位置加载Jquery / Javascript - Rails: Jquery/Javascript fails to load from correct assets location 在jquery的$(window).load(function(){})中运行时,google.load无法正常工作吗? - google.load not working when run in jquery's $(window).load(function(){});? jQuery加载后不运行“ if” - jQuery do not run “if” after load
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM