par
zecreator » 08 août 2008, 09:08
Bravo!
Juste un petit souci, la valeur récupérée dans les attributs width, height, left, top... peut contenir l'unité(px, pt, %...), ce qui fausse le calcul numérique (résultat retourné = NaN).
Donc, on peut ajouté au code JS ceci :
Code : Tout sélectionner
var calque = document.getElementById(divId),x ,y;
var reg=new RegExp("(px)(pt)(%)", "g"); // Expression régulière
if( window.getComputedStyle ) {
// On nettoie les valeurs des attributs qui nous interessent
ws = parseInt(window.getComputedStyle(calque,null).width.replace(reg,""));
hs = parseInt(window.getComputedStyle(calque,null).height.replace(reg,""));
x = (screen.width - ws) / 2;
y = (screen.height - hs) / 2;
} else if( calque.currentStyle ) {
// On nettoie les valeurs des attributs qui nous interessent
ws = parseInt(calque.currentStyle.width.replace(reg,""));
hs = parseInt(calque.currentStyle.height.replace(reg,""));
x = (screen.width - ws) / 2;
y = (screen.height - ws) / 2;
}
calque.style.left = x + "px";
calque.style.top = y + "px";
Bravo!
Juste un petit souci, la valeur récupérée dans les attributs width, height, left, top... peut contenir l'unité(px, pt, %...), ce qui fausse le calcul numérique (résultat retourné = NaN).
Donc, on peut ajouté au code JS ceci :
[code] var calque = document.getElementById(divId),x ,y;
var reg=new RegExp("(px)(pt)(%)", "g"); // Expression régulière
if( window.getComputedStyle ) {
// On nettoie les valeurs des attributs qui nous interessent
ws = parseInt(window.getComputedStyle(calque,null).width.replace(reg,""));
hs = parseInt(window.getComputedStyle(calque,null).height.replace(reg,""));
x = (screen.width - ws) / 2;
y = (screen.height - hs) / 2;
} else if( calque.currentStyle ) {
// On nettoie les valeurs des attributs qui nous interessent
ws = parseInt(calque.currentStyle.width.replace(reg,""));
hs = parseInt(calque.currentStyle.height.replace(reg,""));
x = (screen.width - ws) / 2;
y = (screen.height - ws) / 2;
}
calque.style.left = x + "px";
calque.style.top = y + "px";[/code]