1

For an app I am working on, I recently implemented an overlay that absorbs user input. Of course, that was only one of my objectives - I also need to center the popover that lives 'inside' this overlay.

Here's my setup so far:

Markup

<!-- snip extra HTML -->

<div id='some-overlay' class='input-eater hidden'>
  <div id='some-prompt' class='overlay dialogue'>
    This is a message that should be hidden.<br /><br />
    <a href='javascript: $('#some-overlay').fadeOut();'>Click Here To Close</a>
  </div>
</div>

<!-- ...SNIP... -->

...Some action on the background page causes #some-overlay to fade in, and thus occlude the page until the close action happens. This is the offending JavaScript:

<!-- ...Continued from the markup above... -->
<script>
var $button = $('.some-element'),
    $overlay = $('#some-overlay'),
    $prompt = $('#some-prompt');

$(document).ready(function () {
  $button.click(function(e) {
    e.preventDefault();

    var args = {
      // ...snip.  Unnecessary.
    };

    $.get('Home/SomeAction', args, function(result) {
      // Some processing...

      // !!!! This is the failing code... !!!!
      $prompt.css({
        'left': (window.width - $prompt.width()) / 2,
        'top': (window.height - $prompt.height()) / 2
      });
    });
  });
});
</script>

Styling

.input-eater {
  position: absolute;
  top: 0;
  left: 0;
  z-index: 1;
  width: 100%;
  height: 100%;
  background: rgba(0,0,0, 0.5);
}

.overlay {
  position: absolute;
  z-index: 1;
}

.dialogue {
  box-sizing: border-box;
  // Other platform-specific variants also included.
  // Cosmetic attributes such as box-shadow, background color, etc. also set.
}

As noted in the script section above, the $.css() call is failing to reposition the prompt that lives inside the overlay. It should be centering vertically and horizontally, yet it is staying in the top-left corner of the page.

Question: What am I doing wrong, that is preventing the overlay from centering?

1 Answer 1

5

There is no window.width or height. You can fortunately use $(window).width() and $(window).height() instead.

Sign up to request clarification or add additional context in comments.

1 Comment

No wonder it didn't work. I feel slightly embarrassed. Thanks for the info!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.