代碼儲存在Github
效果預覽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>
/*初始化*/ * { 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) }
(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(); })();
好啦,如今全部的代碼都寫完啦!git
趕快打開瀏覽器,看看效果吧!github
在這裏,只是給你們提供一種思路,參考。
具體的實現,每一個人均可以有不一樣的方法。
請你們趕快發揮想象,把你最想實現的功能,在電腦敲出來吧!web