简体   繁体   English

如何确保图像已完全加载到DOM中

[英]How to make sure that an image is fully loaded in the DOM

I am integrating a cropping tool to my application. 我正在将裁剪工具集成到我的应用程序中。 (Jcrop) (杰克罗普)

I have the following javascript integrated into the page: 我将以下JavaScript集成到页面中:

$(function() {
  $("#imgInp").change(function(){
    readURL(this);
  });
});

function update_crop(coords) {
  var rx = 100/coords.w;
  var ry = 100/coords.h;
  $('#preview').css({
    width: Math.round(rx * 760) + 'px',
    height: Math.round(ry * 1000) + 'px',
    marginLeft: '-' + Math.round(rx * coords.x) + 'px',
    marginTop: '-' + Math.round(ry * coords.y) + 'px'
  });
}

function readURL(input) {
  if (input.files && input.files[0]) {
    var reader = new FileReader();
    reader.onload = function (e) {
      $('#cropbox').attr('src', e.target.result);
      $('#preview').attr('src', e.target.result);
      $('#cropbox').Jcrop({
        onChange: update_crop,
        onSelect: update_crop,
        setSelect: [0, 0, 500, 500],
        aspectRatio: 10 / 13.15
      });

    }
    reader.readAsDataURL(input.files[0]);
 }

} }

And in my HTML i have the following: 在我的HTML中,我有以下内容:

<h1>Image to load</h1>
<form id="form1" runat="server">
  <input type='file' id="imgInp" />
</form>

<img id="cropbox" src="#" alt="your image">
<h4>Preview:</h4>
<div style="width:100px; height:100px; overflow:hidden">
  <img id="preview" src="#" alt="your image">
</div>

So basically i give the user the possibility to upload a pic, then let him visualize the area to be cropped. 因此,基本上,我为用户提供了上传图片的可能性,然后让他可视化要裁剪的区域。

The problem is that when i load the first pic, it is fine, but when i load the another pic the second time, it does not change in the img#cropbox element. 问题是,当我加载第一个图片时,它很好,但是当我第二次加载另一个图时,它在img#cropbox元素中没有更改。

So basically $('#cropbox').Jcrop event is executing before the browser changes the $("#cropbox").attr('src',e.target.result) 因此,基本上$('#cropbox').Jcrop事件在浏览器更改$("#cropbox").attr('src',e.target.result)

How can i make sure that $('#cropbox').Jcrop is executed only when $("#cropbox").attr('src',e.target.result) is executed and the image is fully loaded ? 我如何确保仅在执行$("#cropbox").attr('src',e.target.result)并且图像已完全加载$('#cropbox').Jcrop执行$('#cropbox').Jcrop

Image elements also have a load event. 图像元素也有加载事件。 Before setting the src attribute, wrap that function in a load event callback on that image. 在设置src属性之前,请将该函数包装在该映像的加载事件回调中。

$('#cropbox').load(function(){
    $('#cropbox').Jcrop({
      onChange: update_crop,
      onSelect: update_crop,
      setSelect: [0, 0, 500, 500],
      aspectRatio: 10 / 13.15
    });
});
$('#cropbox').attr('src', e.target.result);

Try setting an interval poll to see if the new image source matches the expected source. 尝试设置间隔轮询以查看新的图像源是否与预期的源匹配。

The new Image source (actual) should match the expected image source (the one you set) before you continue to the JCrop. 在继续进行JCrop之前,新的图像源(实际)应与预期的图像源(您设置的图像源)匹配。

Here is a simplified example: 这是一个简化的示例:

function checkImageLoaded(newImg){

if (newImg == expectedImg){
     $('#cropbox').Jcrop({
        onChange: update_crop,
        onSelect: update_crop,
        setSelect: [0, 0, 500, 500],
        aspectRatio: 10 / 13.15
      });

      //clear the interval
      window.clearInterval(imageInterval);
  }
}

function pollImageSource() {

    //get the new Image source
    //set the expect Image source

    //then check if they match
    var imageInterval = setInterval(function(){ checkImageLoaded(newImageSource); }, 300);
}

There are lots of image plugins that detect if an image is loaded, but I don't think that is necessary. 有很多图像插件可以检测是否加载了图像,但是我认为这不是必需的。

Also, David Walsh has a more robust polling mechanism. 而且,David Walsh具有更强大的轮询机制。

https://davidwalsh.name/javascript-polling https://davidwalsh.name/javascript-polling

Excerpt: 摘抄:

// Usage:  ensure element is visible
poll(function() {
    return document.getElementById('lightbox').offsetWidth > 0;
}, 2000, 150);

But that might not work because your image is already loaded. 但这可能不起作用,因为您的图像已经加载。 You would have to heavily modify that script to compare image source as above. 您将不得不对该脚本进行大量修改以比较上述图像源。

I think you should use the image's onload event. 我认为您应该使用图片的onload事件。 It's fired when the browser is done rendering your image, and every time the image changes, it starts over. 当浏览器完成渲染图像后,就会触发该图像,并且每次图像更改时都会重新开始。

Simple as a dot: 简单点

You can achieve the same with jQuery: $("img").on("load",function() {....}); 您可以使用jQuery实现相同的功能:$(“ img”)。on(“ load”,function(){....});

(There's an earlier answer with almost the same thing, sorry) (有一个差不多的事情,有一个更早的答案,对不起)

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM