如何在加载img后强制脚本工作

how can i force script work after loading img?

本文关键字:脚本 工作 img 加载      更新时间:2024-03-15

我有下一个脚本。它在img加载之前工作,你我使用方法加载。

$(document).ready(function() {
  $('.slide-img img').load(function(){
    var height = $(this).height();
    var width = $(this).width();
    if (width > height) {
      $(this).attr('style', 'max-height: 100%');
    };
    if (height > width) {
      $(this).attr('style', 'max-width: 100%');
    };
  });
 });

如何在加载img后强制脚本工作?

使用window.load方法(jquery):

$(window).load(function() {
// this will fire after the entire page is loaded, including images
});

或者使用window.onload方法(javascript):

window.onload = function() {
// this will fire after the entire page is loaded, including images
};