开发者社区> 问答> 正文

想让jquery先获取iframe的宽和高,然后dialog再居中,我的代码问题在哪?

通过一下代码,现在得到的结果是dialog先居中,然后再通过iframe的宽高改变了dialog大小,这样我的dialog就没法居中了,要怎么办才能居中呢?怎样让load()方法执行完后程序再执行下一步?

var iframeBoxHeight = $("#" + idName).height();
var iframeBoxWidth = $("#" + idName).width();
contentBox = "<div id=\"" + idName + "-contentBox\" class=\"green-ui-dialog-contentBox\"></div>";
var contentBoxObj = $(contentBox);
$("<iframe id=\"" + idName + "-iframeBox\" src=\"" + urlStr + "\"></iframe>").appendTo(contentBoxObj);
$this.append($(contentBoxObj));
$("#" + idName + "-iframeBox").load(function() {
    iframeBoxHeight = $(this).contents().find("p").height() + 30;
    iframeBoxWidth = $(this).contents().find("p").width() + 30;
    $("#" + idName + "-iframeBox").css({
        "height": iframeBoxHeight + "px",
        "width": iframeBoxWidth + "px"
    });
});
 
$this.css({
    "display": "none",
    "left": ($(window).width() / 2 - iframeBoxWidth / 2) + "px",
    "top": ($(window).height() / 2 - iframeBoxHeight / 2) + "px",
    "z-index": "10000"
});

展开
收起
a123456678 2016-07-11 15:41:30 2040 0
1 条回答
写回答
取消 提交回答
  • jquery的load方法为异步,所以会和下面的代码同时执行,建议写在load(function())内部

    2019-07-17 19:54:16
    赞同 展开评论 打赏
问答排行榜
最热
最新

相关电子书

更多
低代码开发师(初级)实战教程 立即下载
冬季实战营第三期:MySQL数据库进阶实战 立即下载
阿里巴巴DevOps 最佳实践手册 立即下载