How to save image width and height from onload?

前端 未结 1 1124
日久生厌
日久生厌 2021-01-24 13:49

I have an array of

  • elements that I am trying to make into a carousel, just that I want the images to be resized if bigger than 640x480 as well as centere
  • 相关标签:
    1条回答
    • 2021-01-24 14:06

      You can basically set an attribute of an image, something like data-loaded="true" once the image is loaded and when the last image is loaded you can then iterate the images and load the array

      In your onload method, do the following changes

      var img = new Image();
      img.onload = function() {
          console.log(this.width + 'x' + this.height);
      
          //set this attribute to indicate that this image has been loaded already
          $(this).attr( "data-loaded", "true" );
      
          //check if all images have this attribute
          if ( $( "img[data-loaded='true']" ).size() == $( "img" ).size() ) 
          {
            //now iterate the images to load your array
          }
      };
      img.src = $(this).find('img').attr('src');
      
      0 讨论(0)
    提交回复
    热议问题