代码之家  ›  专栏  ›  技术社区  ›  gnorbert

如何设置jQuery UI对话框相对于不同按钮的位置?

  •  0
  • gnorbert  · 技术社区  · 7 年前

    我使用的是jQuery UI对话框,它是通过单击页面上的不同按钮触发的。 如何设置对话框相对于其自身触发器的位置?

    以下是我目前的代码:

    <script>
    var dialog = $("#dialog").dialog({
            autoOpen: false,
            width: 300,
            height: 400,
            modal: true,
            open: function(event, ui) {
                $('#btn-quote').css('z-index',1042);
                $('.ui-widget-overlay').on('click', function() {
                    dialog.dialog('close');
                });
            },
            position: {
                my: "center top",
                at: "center top+50",
                of: $('#btn-quote')
            }
    });
    $("#btn-quote, #btn-quote-contact").click(function (e) {
            e.preventDefault()
            dialog.dialog("open");
    });
    </script>
    

    谢谢

    1 回复  |  直到 7 年前
        1
  •  0
  •   plonknimbuzz    7 年前

    您可以使用 $(this) 参考触发按钮。

    <script>
    var dialog = 
    function openDialog(el){
        var dialog = $("#dialog").dialog({
                autoOpen: false,
                width: 300,
                height: 400,
                modal: true,
                open: function(event, ui) {
                    $('#btn-quote').css('z-index',1042);
                    $('.ui-widget-overlay').on('click', function() {
                        dialog.dialog('close');
                    });
                },
                position: {
                    my: "center top",
                    at: "center top+50",
                    of: el
                }
        });
        dialog.dialog("open");
    }
    $("#btn-quote, #btn-quote-contact").click(function (e) {
            e.preventDefault()
            openDialog($(this));
    });
    </script>