How to ensure a DOM object has rendered before calling a function on it?

天大地大妈咪最大 提交于 2019-12-11 20:38:27

问题


I'm using a jquery plugin (jcrop) which has one of its options set by the the value of a DOM element.

jQuery(function($){
  var CircleSel = function(){ };
  CircleSel.prototype = new $.Jcrop.component.Selection();
  $.extend(CircleSel.prototype,{
    zoomscale: $('#img_container').data('zoomscale'),
    # more stuff......

The problem is that '#img_container' is only created by jcrop once the page has loaded, and so zoomscale is being set to null. I can't think of any other way to get this value — I need the #img_container's zoomscale, which is calculated from the width, to ensure crops are correctly positioned from the original image.

I guess I need to either delay the CircleSel function, or enforce a strict order to ensure rendering of #img_container completes before CircleSel is run.

This is stretching my limited javascript knowledge, but I'd like to learn how to approach this with jquery/ javascript. Appreciate any advice or pointers to the necessary functions or examples.

来源:https://stackoverflow.com/questions/36218979/how-to-ensure-a-dom-object-has-rendered-before-calling-a-function-on-it

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!