こんにちは、
ダイアログの表示
ウェブアプリでよく使われるインタフェースとして、
なお、
さて、
まずはCSSだけで処理するアプローチを見ていきましょう。表示する要素のサイズが固定で、
.outer{
position:fixed;
top:50%;
left:50%;
}
.inner{
position:relative;
top:-250px;
left:-250px;
width:500px;
height:500px;
background:#ccc;
}
入れ子になった要素を用意して、
IE 6はposition:fixedに対応していないため、
if (!window.XMLHttpRequest && window.ActiveXObject){
div.style.position = 'absolute';
var root = document.documentElement;
window.attachEvent('onscroll', function(evt){
div.style.top = root.scrollTop + root.clientHeight / 2 + 'px';
});
}
IE 6かどうかの判定はXMLHttpRequestが存在するかどうかで判定します
IE 6のときはpositionをabsoluteにして、
これで一応はIE 6でもposition:fixed相当が実現できました。しかし、
スクロール時のちらつきをなくすには、
.IE6POSITION_FIXED{
behavior:expression(
this.style.top = document.documentElement.scrollTop +
document.documentElement.clientHeight / 2
);
}
behavior:expressionの中はJavaScriptとして解釈されてCSSの描画に合わせて実行されます。描画のたびにJavaScriptが実行されるので、
このCSSがIE 6だけに適用されるように、
if (!window.XMLHttpRequest && window.ActiveXObject){
div.style.position = 'absolute';
div.className += ' IE6POSITION_FIXED';
document.body.style.backgroundImage = 'url(about:blank)';
document.body.style.backgroundAttachment = 'fixed';
}
これでIE 6でもスムーズなposition:fixedが実現できました。しかし、
オーバーレイの表示
続いて、
<div class="outer">
<div class="background_inner">
</div>
<div class="content">
</div>
</div>
まず入れ物となるdivを用意し、
divに対して、
.outer{
width:100%;
height:100%;
position:fixed;
top:0px;
left:0px;
bottom:0px;
right:0px;
}
.background_inner{
background:#000;
width:100%;
height:100%;
position:absolute;
top:0px;
left:0px;
bottom:0px;
right:0px;
opacity:0.6;
filter:alpha(opacity=60);
}
.content{
position:relative;
}
さて、
button.onclick = function(){
document.body.appendChild(div); // body直下に移動
div.style.display = 'block';
};
div.onclick = function(){
div.style.display = 'none';
};
if (!window.XMLHttpRequest && window.ActiveXObject){
div.style.position = 'absolute';
var root = document.documentElement;
window.attachEvent('onscroll', function(evt){
div.style.top = root.scrollTop + 'px';
div.style.height = root.clientHeight + 'px';
});
}
そこで、
button.onclick = function(){
document.body.appendChild(div);
if (ie6) { // IE 6のとき、画面全体の高さに
div.style.height = root.scrollHeight + 'px';
}
div.style.display = 'block';
};
div.onclick = function(){
div.style.display = 'none';
};
var ie6 = false;
if (!window.XMLHttpRequest && window.ActiveXObject){
ie6 = true;
div.style.position = 'absolute';
var root = document.documentElement;
var content = div.children[1];
window.attachEvent('onscroll', function(evt){
// contentだけをスクロールに追従
content.style.top = root.scrollTop + 'px';
});
}
もしくは、
.IE6POSITION_FIXED2{
behavior:expression(
this.style.top = document.documentElement.scrollTop,
this.style.left = document.documentElement.scrollLeft,
this.style.width = document.documentElement.clientWidth,
this.style.height = document.documentElement.clientHeight
);
}
まとめ
今回は画面を覆う方法のIE 6対応を中心に扱いました。クロスブラウザらしい内容ではありますが、
次回も引き続きCSSに絡んだJavaScriptのサンプルを見ていきたいと思います。