Get top dialogue position in jQuery

I use the position parameter in the jQuery dialog plugin . I am trying to figure out how to get only the "top" and "left" values ​​from this position object. However, it is difficult for me to understand this. Right now I have a dialog defined as follows:

<a href="#" onclick="showHelp();">help</a>
<div id="helpDialog" title="Help">
  Some Help related text
</div>

<script type="text/javascript">
  $(document).ready(function () {
    $("#helpDialog").dialog({
      autoOpen: false,
      modal: true,
      buttons: {
        'OK': function() {
          $(this).dialog('close');
        }
      }
    });
  });

  function showHelp() {
    $("#helpDialog").dialog("open");
    var p = ("#helpDialog").dialog( "option", "position" );
    alert( /* what goes here? */);
  }
</script>

When this dialog box opens, I want to display the "top" and "left" position of the dialog box in the warning window. But I can’t understand. Can someone show me? Thank!

+3
source share
1 answer

You can use .offset()to get the offset <div> in the following way:

function showHelp() {
  var o = $("#helpDialog").dialog("open").offset();
  alert("Top: " + o.top + " Left : " + o.left);
}​

, .ui-dialog, .closest(), :

function showHelp() {
  var o = $("#helpDialog").dialog("open").closest('.ui-dialog').offset();
  alert("Top: " + o.top + " Left : " + o.left);
}​

, /, div , .

+6

Source: https://habr.com/ru/post/1752089/


All Articles