燈箱(Lightbox)效果是將圖片放大彈出顯示,並將背景變暗,以突出圖片。這種效果可用於單張圖片,也可用於圖片庫。其原理跟模態框(Modal)基本一樣。W3Schools有分Modal Images和Lightbox兩個教學,主要差別在於Lightbox是爲多張圖片設計,而Modal Images則是單張圖片。爲了方便理解這裏選擇Modal Images,若能理解此例,加上之前的幻燈片,就差不多等於Lightbox中的例子。今天我們就來看看W3Schools是怎樣實理燈箱效果。
視頻連結
燈箱效果(Lightbox)
實現燈箱效果的重點在於:
- 預設隱藏燈箱。
- 當小圖被點擊時,顯示燈箱。
- 圖片放大的效果可透過CSS動畫實現。
以下是燈箱效果的HTML部分:
<img id="myImg" src="img_snow.jpg" alt="Snow" style="width:100%;max-width:300px">
<div id="myModal" class="modal">
<span class="close">×</span>
<img class="modal-content" id="img01">
<div id="caption"></div>
</div>
第一張圖片是小圖,設定了最大寬度爲300px。接着是燈箱,也是一個擬態框(Modal),當中有關閉按鈕、大圖和標題。其中圖片是沒有指定src的,留給JavaScript來處理,標題也一樣。燈箱一開始是隱藏的。
再來看CSS的部分:
/* Style the Image Used to Trigger the Modal */
#myImg {
border-radius: 5px;
cursor: pointer;
transition: 0.3s;
}
#myImg:hover {opacity: 0.7;}
/* The Modal (background) */
.modal {
display: none; /* Hidden by default */
position: fixed; /* Stay in place */
z-index: 1; /* Sit on top */
padding-top: 100px; /* Location of the box */
left: 0;
top: 0;
width: 100%; /* Full width */
height: 100%; /* Full height */
overflow: auto; /* Enable scroll if needed */
background-color: rgb(0,0,0); /* Fallback color */
background-color: rgba(0,0,0,0.9); /* Black w/ opacity */
}
/* Modal Content (Image) */
.modal-content {
margin: auto;
display: block;
width: 80%;
max-width: 700px;
}
/* Caption of Modal Image (Image Text) - Same Width as the Image */
#caption {
margin: auto;
display: block;
width: 80%;
max-width: 700px;
text-align: center;
color: #ccc;
padding: 10px 0;
height: 150px;
}
/* Add Animation - Zoom in the Modal */
.modal-content, #caption {
animation-name: zoom;
animation-duration: 0.6s;
}
@keyframes zoom {
from {transform:scale(0)}
to {transform:scale(1)}
}
/* The Close Button */
.close {
position: absolute;
top: 15px;
right: 35px;
color: #f1f1f1;
font-size: 40px;
font-weight: bold;
transition: 0.3s;
}
.close:hover,
.close:focus {
color: #bbb;
text-decoration: none;
cursor: pointer;
}
/* 100% Image Width on Smaller Screens */
@media only screen and (max-width: 700px){
.modal-content {
width: 100%;
}
}
重點在.modal
,一開始不顯示,且是佔滿全屏。背景顏色是黑色,加了一點透明度。
放大動畫用CSS動畫實現,其實就是用transform將圖片和標題的scale從0放大到1。
最後來看JavaScript:
// Get the modal
var modal = document.getElementById('myModal');
// Get the image and insert it inside the modal - use its "alt" text as a caption
var img = document.getElementById('myImg');
var modalImg = document.getElementById("img01");
var captionText = document.getElementById("caption");
img.onclick = function(){
modal.style.display = "block";
modalImg.src = this.src;
captionText.innerHTML = this.alt;
}
// Get the <span> element that closes the modal
var span = document.getElementsByClassName("close")[0];
// When the user clicks on <span> (x), close the modal
span.onclick = function() {
modal.style.display = "none";
}
JavaScript的部分,主要是將第一張圖片的資料用到燈箱當中,像是src等,並將燈箱顯示出來。另外也完成了關閉按鈕的功能:隱藏燈箱。
W3Schools系列的代碼都在GitHub上:W3Schools GitHub
W3Schools教學系列
W3Schools是知名的網頁設計/前端開發教學網站,不僅提供HTML、CSS、JavaScript等的詳盡教學,還可以把它當作說明文件(Documents)。有經驗的前端或多或少已經接觸過這個網站,因爲它經常出現在搜索結果的前幾項。其中,它的How To部分更是包含了大量非常實用的例子,例如,如何製作SlideShow(圖片輪播)、Lightbox、Parallax(視差效果)等等。因此我想做一系列的影片專門介紹這些How To。
W3Schools系列全部視頻: