Le div reste en haut et la barre de défilement défile, mais le div est toujours en haut ! Téléchargez directement le code source :
Première méthode :
Copiez le code comme suit :
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd ">
<html xmlns="http://www.w3.org/1999/xhtml ">
<tête>
<title>Couche flottante div, la barre de défilement se déplace, gardant la position inchangée</title>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<style type="text/css">
corps{background-image:url(about:blank);background-attachment:fixed;}
#float{largeur:344px;hauteur:34px;bordure:1px solide #C0DBF8;position:absolue;haut:0px;}
</style>
</tête>
<corps>
<div id="float" >ddd</div>
<br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br> ><br><br><br><br><br><br><br><br>
<br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br> ><br><br><br><br><br><br><br><br>
<br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br> ><br><br><br><br><br><br><br><br>
<br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br> ><br><br><br><br><br><br><br><br>
<br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br> ><br><br><br><br><br><br><br><br>
<br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br> ><br><br><br><br><br><br><br><br>
</corps>
<script type="text/javascript">
var IO=document.getElementById('float'),Y=IO,H=0,IE6;
IE6=window.ActiveXObject&&!window.XMLHttpRequest;
while(Y){H+=Y.offsetTop;Y=Y.offsetParent};
si (IE6)
IO.style.cssText="position:absolute;top:(this.fix?(document.documentElement.scrollTop-(this.javascript||"+H+")):0)";
window.onscroll=fonction (){
var d=document,s=Math.max(d.documentElement.scrollTop,document.body.scrollTop);
if(s>H&&IO.fix||s<=H&&!IO.fix)return;
if(!IE6)IO.style.position=IO.fix?"": "fixé";
IO.fix=!IO.fix;
} ;
essayez{document.execCommand("BackgroundImageCache",false,true)}catch(e){};
</script>
</html>
Deuxième méthode :
Copiez le code comme suit :
<STRONG><body topmargin="0" bottommargin="0" leftmargin="0" rightmargin="0" style="overflow: caché;">
<!-- div</STRONG> pour implémenter <STRONG> -->
<div>
<br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br> <br>
<br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br> <br>
<br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br> <br>
<br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br> <br>
</div>
<div style="border: 1px red solid; width: 300px; height: 300px; margin:-150px 0 0 -150px; position: absolue; left: 50%; top: 50%;"></STRONG>
Calque centré
<STRONG> </div>
</body></STRONG>
Troisième méthode :
Copiez le code comme suit :
<body topmargin="0" bottommargin="0" leftmargin="0" rightmargin="0" style="overflow: caché;">
<!-- iframe à implémenter -->
<iframe src="//www.VeVB.COm" frameborder="0"></iframe>
<div style="border: 1px red solid; width: 300px; height: 300px; margin:-150px 0 0 -150px; position: absolue; left: 50%; top: 50%;">
Calque centré
</div>
</corps>
Méthode 4 : [Utiliser CSS pour centrer]
Copiez le code comme suit :
<style type="text/css">
<!--
html, corps {hauteur : 100 % ; marge : 0px ; taille de police : 12px ;}
.mondiv {
couleur d'arrière-plan : #f9fff6 ;
bordure : 1px solide #009900 ;
alignement du texte : centre ;
hauteur de ligne : 25 px ;
taille de police : 13 px ;
poids de la police : gras ;
indice z : 99 ;
largeur : 300 px ;
hauteur : 50px ;
gauche : 50 % ;/*FF IE7*/
haut : 50 % ;/*FF IE7*/
margin-left:-150px!important;/*FF IE7 la moitié de sa largeur */
margin-top:-60px!important;/*FF IE7 la moitié de sa hauteur*/
marge supérieure : 0 px ;
position:fixe!important;/*FF IE7*/
position:absolue;/*IE6*/
_top : expression(eval(document.compatMode &&
document.compatMode=='CSS1Compat') ?
documentElement.scrollTop + (document.documentElement.clientHeight-this.offsetHeight)/2 :/*IE6*/
document.body.scrollTop + (document.body.clientHeight - this.clientHeight)/2);/*IE5 IE5.5*/
}
-->
</style>
<langage de script="javascript" type="text/javascript">
fonction showDiv(){
document.getElementById('popDiv').style.display='block';
}
window.onload=fonction(){
showDiv();
}
</script>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<tête>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312" />
<title>Toujours au milieu DIV (prend en charge IE FF)</title>
</tête>
<corps>
<div id="popDiv" style="display:none;">Toujours au milieu<br/>
</div>
<br/><br/><br/><br/><br/><br/><br/><br/><br/><br/><br/><br/><br /><br/>
</corps>
</html>