Home
Login
Contact
Articles
Newsletter
Bookmark and Share
XSLT
CSS
Javascript
Java
Linux
Username
Password
Annuler
Connexion

Recent Articles

javascript print partial pages

In developping web applications and designing websites, you've probably come accross a situation where you wanted the user to be able to press a print...

Marquee in javascript

The Marquee Element has been deprecated by the W3C and is commonly ill-advised but nevertheless, if you really want to do it, then javascript is the w...

Sending an email in Java

Sending an email in Java is actually quite simple, as always, there is an API that will do most of the work for you and it becomes just a matter of im...

Opacity in Firefox 3.5

If you've upgraded to Firefox 3.5 and you've been using -moz-opacity in your CSS, then you will see that the transparency or opacity (depending on ho...

Installing Tomcat on Linux in a few minutes

Installing tomcat is actually very quick and easy. Assuming you already have the JDK installed, this will only take a few minutes. In my years of exp...

Centering an element in javascript

Matt Castonguay, 03 June at 11:20AM 

With the simulation of popup windows layered in the page, the need to play with the z-index CSS property and being able to dynamically center an element, commonly a div, becomes a necessity for everyone's javascript library. In this article, we will look at a simple function which will take the user's browser dimensions and center a given element's id.

The logic behind this is very simple, we must first get the view pane dimensions, in order to calculate the height and width that we are working with. This has a few conditional factors due to the various document level variables for each browser:

function getViewPaneSize() {
  var myWidth = 0, myHeight = 0;
  if( typeof( window.innerWidth ) == 'number' ) {
    //FF and others
    myWidth = window.innerWidth;
    myHeight = window.innerHeight;
  } else if( document.documentElement && ( document.documentElement.clientWidth
      || document.documentElement.clientHeight ) ) {
    //IE
    myWidth = document.documentElement.clientWidth;
    myHeight = document.documentElement.clientHeight;
  } else if( document.body && ( document.body.clientWidth
      || document.body.clientHeight ) ) {
    myWidth = document.body.clientWidth;
    myHeight = document.body.clientHeight;
  }
  return [ myWidth, myHeight ];
}

 

Aferwards, we need to know if the user has scrolled down or to the right. Since scrolling down midway to the page, the "top" is no longer 0, it is a value in pixels of how much distance has been scrolled:

function getScrollXY() {
  var scrOfX = 0, scrOfY = 0;
  if( typeof( window.pageYOffset ) == 'number' ) {
    //Netscape

    scrOfY = window.pageYOffset;
    scrOfX = window.pageXOffset;
  } else if( document.body && ( document.body.scrollLeft
      || document.body.scrollTop ) ) {
    //FF and others

    scrOfY = document.body.scrollTop;
    scrOfX = document.body.scrollLeft;
  } else if( document.documentElement && ( document.documentElement.scrollLeft
      || document.documentElement.scrollTop ) ) {
    //IE6
    scrOfY = document.documentElement.scrollTop;
    scrOfX = document.documentElement.scrollLeft;
  }
  return [ scrOfX, scrOfY ];
}

 

Once we've got our utility functions to calculate the size of the browser and where we are in terms of scrolling, we can do some simple math to center the element:

function centerElement(elementId) {
    var div = document.getElementById(elementId);
    var w = div.offsetWidth;
    var h = div.offsetHeight;
    var dimensions = getViewPaneSize();
    var scrollOffset = getScrollXY();
        var offsetTop = parseInt((((dimensions[1] - h) / 2) + scrollOffset[1]));
        if (offsetTop <= 0) {
            offsetTop = 50;
        }
    div.style.left = ((((dimensions[0] - w) / 2) + scrollOffset[0]) + "px");
    div.style.top = offsetTop + "px";
}

Enjoy!

Category:   CSS, Javascript

Tags:   position, offsetleft, offsettop

Random Tags

bash    offsetleft    offsetleft    ant    textarea    print    apply-templates    proxypass    xsl:if    offsettop    hashmap    ie6    template    optimization    memory    custom css    float    firefox    email    wysiwyg    clear    apache    xsl:import    xsl:include    iframe    weakhashmap    min-width    javamail    css classes    library    function    css classes    mode    min-height    svn    position    math    tinymce    tomcat    tinymce   
MG2 Media Inc. - A Web Development Company
Top