Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/heroku/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
调整coffeescript以使用window.onload_Coffeescript - Fatal编程技术网

调整coffeescript以使用window.onload

调整coffeescript以使用window.onload,coffeescript,Coffeescript,我有下面的coffeescript,它可以正常工作,只是当加载图像需要一段时间时,它会在加载图像之前运行,然后没有达到预期效果: ready = -> jQuery -> $('.box').find('img').each -> imgClass = if @width / @height > 1 then 'wide' else 'tall' $(this).addClass imgClass return re

我有下面的coffeescript,它可以正常工作,只是当加载图像需要一段时间时,它会在加载图像之前运行,然后没有达到预期效果:

ready = ->
  jQuery ->
    $('.box').find('img').each ->
      imgClass = if @width / @height > 1 then 'wide' else 'tall'
      $(this).addClass imgClass
      return
    return


$(document).ready(ready)
$(document).on('page:load', ready)
如何仅在加载整个窗口后运行函数

到目前为止,我最好的尝试是

window.onload = ->
  $('.box').find('img').each ->
    imgClass = if @width / @height > 1 then 'wide' else 'tall'
    $(this).addClass imgClass
    return
  return
但它不起作用。我也尝试过其他几种可能性,但我不知道出了什么问题


我的问题与此不同,因为我不想等到所有的咖啡脚本加载完毕,我想等到我网页中的所有图像加载完毕。

要在所有图像加载后执行所需的代码,请在窗口中使用jQuery的
load()
方法<代码>文档监视DOM,而
窗口
监视内容

在咖啡中,它看起来是这样的:

$(window).load ->
  alert 'All images are loaded'

这与您使用的不同之处在于您使用的是
window.onload
,而我建议的是
$(window.load()

可能的重复肯定不是重复。请参阅我的编辑,其中解释了原因。感谢您的澄清。