[CSS]モーダルウインドウをCSS3アニメーションを使ってエレガントに表示するチュートリアル
Post on:2012年4月12日
sponsorsr
モーダルウインドウを表示する際、CSS3アニメーションでズームイン・アウトさせるチュートリアルを紹介します。

Create Elegant Modal Window Using CSS Scale Transform
[ad#ad-2]
デモ
デモでは2種類のモーダルウインドウがあります。

左はズームインで、大きいサイズのモーダルウインドウがぎゅっと縮小のアニメーションで表示されます。

デモページ:ズームイン
[ad#ad-2]
右はズームアウトで、小さいサイズのモーダルウインドウがしだいに拡大されて表示されます。

デモページ:ズームアウト
実装
HTML
div要素でモーダルウインドウを作成し、ページ全体を覆うオーバーレイのdiv要素で内包します。
<div class="overlay-container"> <div class="window-container zoomin"> <h3>Hello I'm zoom in modal window</h3> I'm coming using scale transform, using transform: scale() <br> From <strong>scale(1.2)</strong> to scale(1) with transition property make me looks like <strong>come from you</strong> <img src="http://www.webstuffshare.com/wp-includes/images/smilies/icon_smile.gif" alt=":)" class="wp-smiley"> <br> <span class="close">Close Me</span> </div> </div>
CSS
まずは、オーバーレイとモーダルウインドウのスタイルです。
これらは初期時は「opacity: 0;」を使って、見えないようにしておきます。
.overlay-container {
display: none;
content: " ";
height: 100%;
width: 100%;
position: absolute;
left: 0;
top: 0;
background: radial-gradient(center, ellipse cover, rgba(127,127,127,0) 0%,rgba(127,127,127,0.9) 100%);
}
.window-container {
display: block;
background: #fcfcfc;
margin: 8em auto;
width: 500px;
padding: 10px 20px 20px;
text-align: left;
z-index: 3;
border-radius: 3px;
box-shadow: 0px 0px 30px rgba(0,0,0,0.2);
transition: 0.4s ease-out;
opacity: 0;
}
.window-container h3 {
margin: 1em 0 0.5em;
font-family: "Oleo Script";
font-weight: normal;
font-size: 25px;
text-align: center;
}
.close {
margin: 1em auto;
display: block;
width: 52px;
background: linear-gradient(top, #fafafa 0%,#f4f4f4 40%,#e5e5e5 100%);
border: 1px solid #aaa;
padding: 5px 14px;
color: #444;
font-family: Helvetica, sans-serif;
font-size: 12px;
border-radius: 3px;
box-shadow: 0 1px 3px #ddd;
transition: 0.2s linear;
cursor: pointer;
}
.close:hover {
background: linear-gradient(top, #fefefe 0%,#f8f8f8 40%,#e9e9e9 100%);
box-shadow: 0 1px 3px rgba(0, 0, 0, 0.2);
border: 1px solid #aaa;
}
.close:active {
background: linear-gradient(top, #f4f4f4 0%,#efefef 40%,#dcdcdc 100%);
box-shadow: inset 0 1px 3px rgba(0, 0, 0, 0.2);
}
拡大・縮小のアニメーションは「scale」を使用します。また、「opacity: 1;」で見えるようにするのを忘れずに。
.zoomin {
transform: scale(1.2);
}
.zoomout {
transform: scale(0.7);
}
.window-container-visible {
transform: scale(1);
opacity: 1;
}
JavaScript
最後に、モーダルウインドウを表示・非表示する際のクリックイベントをjQueryで処理します。
$('.button').click(function() {
type = $(this).attr('data-type');
$('.overlay-container').fadeIn(function() {
window.setTimeout(function(){
$('.window-container.'+type).addClass('window-container-visible');
}, 100);
});
});
$('.close').click(function() {
$('.overlay-container').fadeOut().end().find('.window-container').removeClass('window-container-visible');
});
sponsors











