AJAX Modal Box Positioning

Discussion in 'JavaScript' started by thrillseeker30, Apr 26, 2009.

  1. #1
    Hey guys, I'm learning how to validate a form field using Javascript/PHP being passed asynchronously.

    However, I'm stuck with trying to position the modal box in the center of the screen. With the following code, it positions the modal box too far down, and too far from the left:

    function overlay() {
    	
    	var modal_window = document.getElementById("modal_window");
    	var modal_background = document.getElementById("modal_background");
    	
    	var windowHeight = window.innerHeight;
    	var windowWidth = window.innerWidth;
    	
    	var divHeight = modal_window.clientHeight;
    	var divWidth = modal_window.clientWidth;
    	
    	modal_window.style.top = (windowHeight/2) - (divHeight/2) + "px";
    	modal_window.style.left = (windowWidth/2) - (divWidth/2) + "px";
    }
    Code (markup):
    Any thoughts of how I can improve this bit of code or how to accurately calculate the center of any browser window.

    Thanks guys!
     
    thrillseeker30, Apr 26, 2009 IP
  2. JavaScriptBank.com

    JavaScriptBank.com Peon

    Messages:
    141
    Likes Received:
    4
    Best Answers:
    0
    Trophy Points:
    0
    #2
    JavaScriptBank.com, Apr 27, 2009 IP