廢話不多說,直接分享一個帶放大動畫效果的 popup dialog,沒有使用任何 jQuery 插件。專案中你可以依需求自行調整 CSS,我也打算把它複用到 Metro 風格的網站上。
核心是遮罩層 #mask 與中央的對話框 #transition/#content,用絕對定位與高 z-index 疊在頁面上:
body { background: #ace; font: 12px/1.2 Arial, Helvetica, sans-serif; }
ul li { background:#fff; margin:5px; width:100px; height:100px; float:left; }
#transition {
background:transparent;
display:none;
position:absolute; top:50%; left:50%; z-index: 10001;
}
#content {
background:#fff;
border:1px solid #666;
margin:-50px 0 0 -50px;
width:100px; height:100px;
z-index: 10001;
}
#mask{
position: absolute; top: 0; left: 0;
width: 100%; height: 100%;
background-color: #000000;
display: none; z-index: 10000;
}
點擊縮圖時,先淡入遮罩,再讓對話框從點擊位置放大移動到螢幕中央,最後撐大成一個大視窗並載入內容:
<script type="text/javascript">
$(document).ready(function () {
$('ul li').click(function (e) {
$("#mask").fadeTo(500, 0.25);
$("#content").html("<div>Loading....</div>");
var $t = $('#transition'),
to = $(this).offset();
var height = $(document).height();
var width = $(document).width();
$('#content').css({ width: 100, height: 100 });
$t.css({
top: to.top + 50,
left: to.left + 50,
display: 'block'
}).animate({
top: height / 2,
left: width / 2
}, 600, function () {
$(this).animate({
top: 125,
left: 175
}, 600);
$('#content').animate({
width: width * 0.8,
height: height * 0.8
}, 600, function () {
// 在這裡載入對話框內容
$("#content").html("<div>Hello, please put content here.</div>");
});
});
});
$('#transition').click(function (e) {
$("#transition").hide();
$("#mask").hide();
});
});
</script>
一排縮圖加上遮罩層與對話框容器:
<ul> <li>thumb</li> <li>thumb</li> <!-- ...更多縮圖... --> </ul> <div id='mask'></div> <div id="transition"><div id="content">Loading....</div></div>
加入 mask 遮罩效果能突顯焦點,如果不需要可以直接刪掉相關程式碼。整體動畫相當流暢,加上一點後處理會更加分。