Never been to TextSnippets before?

Snippets is a public source code repository. Easily build up your personal collection of code snippets, categorize them with tags / keywords, and share them with the world (or not, you can keep them private!)

About this user

Denis Laprise http://poly9.com

« Newer Snippets
Older Snippets »
3 total  XML / RSS feed 

Center a DOM element, prototype based

Generally used to center a dialog (a div element).
Usage:
Position.Center(dialog);
The element can also be centered relatively to another node by using the parent parameter.
It requires the this snippet

Position.Center = function(element, parent) {
        var w, h, pw, ph;
        var d = Element.getDimensions(element);
        w = d.width;
        h = d.height;
        Position.prepare();
        if (!parent) {
                var ws = Position.GetWindowSize();
                pw = ws[0];
                ph = ws[1];
        } else {
                pw = parent.offsetWidth;
                ph = parent.offsetHeight;
        }
        element.style.top = (ph/2) - (h/2) -  Position.deltaY + "px";
        element.style.left = (pw/2) - (w/2) -  Position.deltaX + "px";
}

Prototype-based javascript window dimensions

Usage:
var dim = Position.GetWindowSize();
dim[0] is the window width
dim[1] is the window height

window can be specified, useful if you work with frames..

Position.GetWindowSize = function(w) {
        w = w ? w : window;
        var width = w.innerWidth || (w.document.documentElement.clientWidth || w.document.body.clientWidth);
        var height = w.innerHeight || (w.document.documentElement.clientHeight || w.document.body.clientHeight);
        return [width, height]
}

Prototype based Javascript min(...), max(...) functions


Use cases
min(1,-1,2,3,4) == -1
max(2,10) == 10

function min() {
 return $A(arguments).min();
}

function max() {
 return $A(arguments).max();
}

« Newer Snippets
Older Snippets »
3 total  XML / RSS feed