简体   繁体   English

如何更新jQuery Mobile全局弹出窗口的位置?

[英]How do I update the position of a jQuery Mobile global popup?

I have a jQuery Mobile global popup, whose contents are generated dynamically. 我有一个jQuery Mobile全局弹出窗口,其内容是动态生成的。 So by default it's empty. 因此,默认情况下为空。

I'm listening for the beforeposition event to capture the popup being opened. 我正在侦听beforeposition事件,以捕获正在打开的弹出窗口。 Then I load a config file/content file, generate the content and append it to the popup. 然后,我加载一个配置文件/内容文件,生成内容并将其附加到弹出窗口。

However by the time I'm appending, JQM is already done calculating the position of the popup so it will be misplaced on the screen. 但是,在我追加内容时,JQM已经完成了计算弹出窗口的位置的操作,因此它将在屏幕上放错位置。

Here is what I'm doing: 这是我在做什么:

$(document).find("#global-popup")
    .on("popupbeforeposition", function (e) {
    factory.util.generatePopupContents(app.generateActionObject(e));
});


factory.util.generatePopupContents = function (obj) {
    var i, j, promises, fragment, popup, reference, state;

    popup = obj.gadget,
    reference = popup.getAttribute("data-reference"),
    state = popup.getAttribute("data-state");

    // don't reload if same popup is opened
    if (state !== reference) {
        if (reference === null) {
            util.errorHandler({
                "error": "Global Bindings: No handler for popup"
            });
        } else {
            popup.setAttribute("data-state", reference);
            popup.setAttribute("data-reference", reference);
            promises = [];

            // fetch content > THIS WILL LOAD A JSON DICT
            app.fetchConfiguration({
                "storage": app.default_dict.storage_dict.settings,
                    "file": app.default_dict.storage_dict.gadgets,
                    "attachment": reference,
                    "pass": undefined
            })
                .then(function (reply) {
                obj.gadget.setAttribute("data-reference", reference);
                // loop children in JSON dict
                if (reply.children) {
                    for (i = 0; i < reply.children.length; i += 1) {
                        promises[i] = app.setContent(reply.children[i], {}, false);
                    }
                }
                return RSVP.all(promises)
            })
                .then(function (content) {
                // create a fragment and append generated content
                fragment = document.createDocumentFragment();
                for (j = 0; j < content.length; j += 1) {
                    fragment.appendChild(content[j]);
                }

                // translate fragment if needed
                if (i18n) {
                    map.actions.translateNodeList(fragment);
                }

                // empty gadget and reload
                obj.gadget.innerHTML = "";
                obj.gadget.appendChild(fragment);
            })
                .fail(util.errorHandler);
        }
    }
};

I'm wondering how to reposition the popup ( $(obj.gadget) ) after I have appended the content. 我想知道在追加内容后如何重新定位弹出窗口( $(obj.gadget) )。

I tried: 我试过了:

 $(obj.gadget).trigger("updatelayout");

Or: 要么:

 $(obj.gadget).popup("reposition", {"y": 0});

But they both don't work. 但是它们都不起作用。 Neither does triggering updatelayout on document . 也不对上触发UpdateLayout请document

Question
How can I update the position of a global popup? 如何更新全局弹出窗口的位置?

You need to bind it to popupafteropen . 您需要将其绑定到popupafteropen

$(".selector").on("popupafteropen", function () {
    $(this).popup("reposition", {
        "positionTo": "window",
        // or
        x: 100,
        y: 200
    });
});

Demo 演示

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

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