jCrop (jQuery) sometimes fails to load image/cropper area

后端 未结 6 1634
一个人的身影
一个人的身影 2021-02-07 08:46

I\'ve got a pretty simple problem, but I\'ve become clueless on what is causing the problem. In one of my applications I\'m using jCrop as a small add-on to crop images to fit i

6条回答
  •  名媛妹妹
    2021-02-07 09:14

    Creating an 'Image' object and setting up the 'src' attribute does not apply that you can treat the image like it had already been loaded. Also, giving any fixed timeout interval does not guaranty the image has already been loaded.

    Instead, you should set up an 'onload' callback for the Image Object - which will then initialize the Jcrop Object:

    var src = 'https://example.com/imgs/someimgtocrop.jpg'; 
    var tmpImg = new Image();
    tmpImg.onload = function() {
       //This is where you can safely create an image and a Jcrop Object
    };
    tmpImg.src = src; //Note that the 'src' attribute is only added to the Image Object after the 'onload' listener was defined
    

提交回复
热议问题