按下右側的「點擊預覽」按鈕能夠在當前頁面預覽,點擊連接能夠全屏預覽。css
https://codepen.io/comehope/pen/GYXvezhtml
此視頻是能夠交互的,你能夠隨時暫停視頻,編輯視頻中的代碼。前端
請用 chrome, safari, edge 打開觀看。git
https://scrimba.com/p/pEgDAM/cNzVnALgithub
每日前端實戰系列的所有源代碼請從 github 下載:chrome
https://github.com/comehope/front-end-daily-challengesapi
定義 dom,一個名爲 .main
的容器中包含 1 個連接:dom
<div class="main"> <a href="#" class="open-popup">open popup</a> </div>
設置頁面的基本屬性:無邊距、全高、忽略溢出:佈局
body { margin: 0; height: 100vh; overflow: hidden; }
設置主界面的背景和其中按鈕的佈局方式:flex
.main { height: inherit; background: linear-gradient(dodgerblue, darkblue); display: flex; align-items: center; justify-content: center; }
設置按鈕樣式:
.open-popup { box-sizing: border-box; color: white; font-size: 16px; font-family: sans-serif; width: 10em; height: 4em; border: 1px solid; text-align: center; line-height: 4em; text-decoration: none; text-transform: capitalize; }
設置按鈕懸停效果:
.open-popup:hover { border-width: 2px; }
至此,主界面完成,接下來製做彈窗。
在 dom 中增長的 .popup
小節表示彈窗內容,其中的 <a>
是返回按鈕,<p>
是具體內容,這裏咱們把內容簡化爲一些陸生動物的 unicode 字符,爲了可以觸發這個彈窗,設置 .popup
的 id
爲 terrestrial
,並在 .main
的 <a>
連接中指向它:
<div class="main"> <a href="#terrestrial" class="open-popup">terrestrial animals</a> </div> <section id="terrestrial" class="popup"> <a href="#" class="back">< back</a> <p>🦓🦒🐅🐆🐘🦏🐃🦌🐐🐫</p> </section>
設置彈窗的尺寸,它將覆蓋剛纔的 .main
的內容:
.popup { position: absolute; top: 0; width: 100%; height: inherit; display: flex; flex-direction: column; justify-content: start; }
設置返回按鈕的樣式:
.popup .back { font-size: 20px; font-family: sans-serif; text-align: center; height: 2em; line-height: 2em; background-color: #ddd; color: black; text-decoration: none; } .popup .back:visited { color: black; } .popup .back:hover { background-color: #eee; }
設置內容的樣式:
.popup p { font-size: 100px; text-align: center; margin: 0.1em 0.05em; }
設置彈窗內容默認是不顯示的,只有點擊主界面的連接時才顯示:
.popup { display: none; } .popup:target { display: flex; }
至此,彈窗完成,但彈窗中的內容是重疊在主界面上面的,接下來製做從主界面到彈窗的動畫效果。
動畫效果包含 3 個步驟:頁面中間的一條直線從左端橫穿到右端,頁面中間大幕向上下兩端拉開,最後內容淡入,下面的製做過程依次是第 3 個步驟、第 2 個步驟、第 1 個步驟。
先讓彈窗內容淡入:
.popup > * { filter: opacity(0); animation: fade 0.5s ease-in forwards; } @keyframes fade{ to { filter: opacity(1); } }
用僞元素 ::before
製做一個白色背景,從頁面中間向上下兩端展開:
.popup::before { content: ''; position: absolute; box-sizing: border-box; width: 100%; height: 0; top: 50%; background-color: white; animation: open-animate 0.5s cubic-bezier(0.8, 0.2, 0, 1.2) forwards; } @keyframes open-animate { to { height: 100vh; top: 0; } }
設置彈窗淡入動畫的延時時長,造成先大幕拉開再顯示內容的效果:
.popup > * { animation-delay: 0.5s; }
用僞元素 ::after
製做一條橫線,從頁面左端橫穿到右端:
.popup::after { content: ''; position: absolute; width: 0; height: 2px; background-color: white; top: calc((100% - 2px) / 2); left: 0; animation: line-animate 0.5s cubic-bezier(0.8, 0.2, 0, 1.2); } @keyframes line-animate { 50%, 100% { width: 100%; } }
再設置彈窗淡入動畫和大幕拉開動畫的延時時長,讓動畫效果依次執行:
.popup > * { animation-delay: 1s; } .popup::before { animation-delay: 0.5s; }
至此,整個動畫效果完成。
在 dom 再中增長一組水生動物的內容,以及打開它的連接:
<div class="main"> <a href="#terrestrial" class="open-popup">terrestrial animals</a> <a href="#aquatic" class="open-popup">aquatic animals</a> </div> <section id="terrestrial" class="popup"> <a href="#" class="back">< back</a> <p>🦓🦒🐅🐆🐘🦏🐃🦌🐐🐫</p> </section> <section id="aquatic" class="popup"> <a href="#" class="back">< back</a> <p>🐋🐳🐬🐟🐠🐡🐙🦑🦐🦀</p> </section>
最後,設置一下主界面上按鈕的間距:
.open-popup { margin: 1em; }
大功告成!