【问题标题】:Delay some jQuery function until all images are loaded completely延迟一些 jQuery 函数,直到所有图像完全加载
【发布时间】:2014-07-24 21:51:06
【问题描述】:

如何延迟一些 jQuery / JavaScript 函数,直到页面上的所有图像都完成加载? 实际上,我所说的 jQuery 函数是用于设置 div 的偏移位置。问题是一旦图像完全加载,页面就会调整大小,所以偏移量是错误的。

jQuery函数参考这个问题:Issues with Fixed div on bottom of page that stops at given place

【问题讨论】:

    标签: javascript jquery


    【解决方案1】:

    您可以使用在加载所有图像或外部资源后运行的onload 事件:

    $(window).load(function(){
      // your code here, all images loaded
    });
    

    您还可以对单个图像使用load 事件并在它们加载后运行您的代码:

    $('img.ImageClass').load(function(){
      // The image loaded....
    });
    

    【讨论】:

      【解决方案2】:

      只需将函数写在像

      这样的变量下
      function newfunction(){
      //put all the stuff here
      }
      

      之后在每次图像加载时调用此函数

      $('img').load(function(){
      newfunction()
      })
      

      因此,每次加载新图像时,新函数都会调用。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2020-08-20
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2020-05-29
        • 1970-01-01
        • 1970-01-01
        • 2011-09-15
        相关资源
        最近更新 更多