模態相簿
初衷:很多人在初學前端的時候都會問,“如何入門前端?”
同為在前端學習道路上,奮力追趕的一員,本人對於目前網路上所能看到的 “入門級” 的教材並不太滿意。學習一門新知識,例項是尤其重要的。在這裡本人整理了目前頁面上常見功能實現的具體例項。願能為大家提供一些幫助。
希望能夠與大家互相分享,共同進步。
HTML部分
<!-- 圖片欄 -->
<div class="row">
<div class="column">
<img src="img1.jpg" class="hover-shadow triggerImg">
</div>
<div class="column">
<img src="img2.jpg" class="hover-shadow triggerImg">
</div>
<div class="column">
<img src="img3.jpg" class="hover-shadow triggerImg">
</div>
<div class="column">
<img src="img4.jpg" class="hover-shadow triggerImg">
</div>
</div>
<!-- 模態框 -->
<div id="myModal" class="modal">
<span class="close cursor" id="closeBtn">×</span>
<!-- 模態內容 -->
<div class="modal-content">
<!-- 模態內容中的照片 -->
<div class="mySlides">
<div class="numbertext">1 / 4</div>
<img src="img1.jpg">
</div>
<div class="mySlides">
<div class="numbertext">2 / 4</div>
<img src="img2.jpg">
</div>
<div class="mySlides">
<div class="numbertext">3 / 4</div>
<img src="img3.jpg">
</div>
<div class="mySlides">
<div class="numbertext">4 / 4</div>
<img src="img4.jpg">
</div>
<!-- 後翻頁 -->
<a class="prev">❮</a>
<!-- 前翻頁 -->
<a class="next">❯</a>
<!-- 圖片標題 -->
<div class="caption-container">
<p id="caption"></p>
</div>
<!-- 列出待選圖片(包括正在顯示的圖片) -->
<div class="column">
<img class="demo" src="img1.jpg" alt="圖片1">
</div>
<div class="column">
<img class="demo" src="img2.jpg" alt="圖片2">
</div>
<div class="column">
<img class="demo" src="img3.jpg" alt="圖片3">
</div>
<div class="column">
<img class="demo" src="img4.jpg" alt="圖片4">
</div>
</div>
</div>
CSS 部分
/*初始化*/
* {
margin: 0;
padding: 0;
box-sizing: border-box;
}
.row > .column {
padding: 0 8px;
}
/*清除浮動*/
.row:after {
content: "";
display: table;
clear: both;
}
.column {
float: left;
width: 25%;
}
.column img {
width: 100%;
cursor: pointer;
}
/*模態框*/
.modal {
display: none;
position: fixed;
z-index: 1;
padding-top: 100px;
left: 0;
top: 0;
width: 100%;
height: 100%;
overflow: auto;
background-color: rgba(0,0,0,0.9);
}
/*模態內容*/
.modal-content {
position: relative;
background-color: #fefefe;
margin: 0 auto;
padding: 0;
width: 90%;
max-width: 1200px;
animation: zoomShow 0.5s; /*新增動畫*/
}
@keyframes zoomShow {
from {transform: scale(0)}
to {transform: scale(1)}
}
/*關閉按鈕*/
.close {
color: white;
position: absolute;
top: 10px;
right: 25px;
font-size: 35px;
font-weight: bold;
}
.close:hover {
color: #999;
text-decoration: none;
cursor: pointer;
}
/*模態圖片*/
.mySlides {
display: none;
background-color: rgba(0,0,0,0.9);
animation: fadeShow 0.5s; /*新增動畫*/
}
@keyframes fadeShow {
from {opacity: 0}
to {opacity: 1}
}
.mySlides img {
width: 100%;
}
/*前翻後翻*/
.prev,
.next {
cursor: pointer;
position: absolute;
top: 50%;
width: auto;
padding: 16px;
margin-top: -50px;
color: white;
font-weight: bold;
font-size: 20px;
transition: 0.6s ease;
border-radius: 0 3px 3px 0;
user-select: none;
}
.next {
right: 0;
border-radius: 3px 0 0 3px;
}
.prev:hover,
.next:hover {
background-color: rgba(0, 0, 0, 0.8);
}
/*定位數字*/
.numbertext {
color: #f2f2f2;
font-size: 12px;
padding: 8px 12px;
position: absolute;
top: 0;
}
/*圖片標題*/
.caption-container {
text-align: center;
background-color: rgba(0,0,0,0.9);
padding: 5px 16px 10px;
color: white;
}
img.demo {
opacity: 0.6;
}
.active,
.demo:hover {
opacity: 1;
}
img.hover-shadow {
transition: 0.3s
}
img.hover-shadow:hover {
box-shadow: 0 4px 8px 0 rgba(0, 0, 0, 0.2), 0 6px 20px 0 rgba(0, 0, 0, 0.19)
}
JavaScript 部分
(function() {
/*開啟模態框*/
function openModal() {
var myModal = document.getElementById(`myModal`);
myModal.style.display = "block";
}
/*關閉模態框*/
function closeModal() {
var myModal = document.getElementById(`myModal`);
myModal.style.display = "none";
}
/*切換模態圖*/
function changeSlide(num) {
var currentSlide,
slides = document.getElementsByClassName("mySlides");
for (index = 0; index < slides.length; index++) {
if(slides[index].style.display == "block") {
currentSlide = index + 1;
}
}
showSlide(currentSlide += num);
}
/*顯現模態圖*/
function showSlide(num) {
var index,
slides = document.getElementsByClassName("mySlides"),
dots = document.getElementsByClassName("demo"),
captionText = document.getElementById("caption");
if (num > slides.length) {
num = 1
}
if (num < 1) {
num = slides.length
}
for (index = 0; index < slides.length; index++) {
slides[index].style.display = "none";
}
for (index = 0; index < dots.length; index++) {
dots[index].classList.remove("active");
}
slides[num - 1].style.display = "block";
dots[num - 1].classList.add("active");
captionText.innerHTML = dots[num - 1].alt;
}
/*點選模態內容以外,自動關閉*/
function clickOutside() {
var myModal = document.getElementById(`myModal`);
window.onclick = function(event) {
if(event.target == myModal) {
closeModal();
}
}
}
/*繫結事件監聽*/
function addEvent() {
var index,
triggerImgs = document.getElementsByClassName("triggerImg"),
dots = document.getElementsByClassName("demo"),
closeBtn = document.getElementById("closeBtn"),
prevBtn = document.getElementsByClassName("prev")[0],
nextBtn = document.getElementsByClassName("next")[0];
for(index = 0; index < triggerImgs.length; index++) {
(function(index) {
triggerImgs[index].onclick = function() {
openModal();
showSlide(index + 1);
}
})(index);
}
for(index = 0; index < dots.length; index++) {
(function(index) {
dots[index].onclick = function() {
showSlide(index + 1);
}
})(index);
}
prevBtn.onclick = function() {
changeSlide(-1);
}
nextBtn.onclick = function() {
changeSlide(1);
}
closeBtn.onclick = function() {
closeModal();
}
}
/*初始化*/
function init() {
addEvent();
clickOutside();
}
/*呼叫*/
init();
})();
好啦,現在所有的程式碼都寫完啦!
趕快開啟瀏覽器,看看效果吧!
在這裡,只是給大家提供一種思路,參考。
具體的實現,每個人都可以有不同的方法。
請大家趕快發揮想象,把你最想實現的功能,在電腦敲出來吧!