HTML結構
該頁面切換特效的HTML結構使用一個
Page Transition
CSS樣式
該頁面切換特效中使用body::before和body::after偽元素在頁面切換過程中創建兩個遮罩層來遮住頁面內容。它們的定位是固定定位,高度等于50vh,寬度為100%。默認情況下,使用CSS transform屬性將它們隱藏起來(translateY(-100%)/translateY(100%))。當用戶切換頁面的時候,這些元素被移動回視口當中(通過在
元素上添加.page-is-changing class)。頁面切換特效
body::after, body::before { /* these are the 2 half blocks which cover the content once the animation is triggered */ height: 50vh; width: 100%; position: fixed; left: 0; } body::before { top: 0; transform: translateY(-100%); } body::after { bottom: 0; transform: translateY(100%); } body.page-is-changing::after, body.page-is-changing::before { transform: translateY(0); }
頁面切換時,頁面內容的淡入淡出效果是通過改變div.cd-cover-layer的透明度實現的。它覆蓋了.cd-main-content元素,并具有相同的背景色,然后在
被添加.page-is-changing class的時候,將透明度從0修改為1。.cd-loading-bar { /* this is the loading bar - visible while switching from one page to the following one */ position: fixed; height: 2px; width: 90%; } .cd-loading-bar::before { /* this is the progress bar inside the loading bar */ position: absolute; left: 0; top: 0; height: 100%; width: 100%; transform: scaleX(0); transform-origin: left center; } .page-is-changing .cd-loading-bar::before { transform: scaleX(1); }
特效中平滑的過渡效果使用CSS Transitions來實現。每一個動畫元素都被添加了不同的transition-delay,以實現不同的元素動畫順序。
JAVASCRIPT
該頁面切換特效中在鏈接上使用data-type="page-transition"屬性,用于觸發頁面切換事件。當插件檢測到用戶點擊事件,changePage()方法將被執行。
這個方法會觸發頁面切換動畫,并通過loadNewContent()方法加載新內容。
當新的內容被加載后,會替代原來
為了在用戶點擊瀏覽器的回退按鈕時觸發相同的頁面切換動畫效果,插件中監聽popstate事件,并在它觸發時執行changePage()函數。
聲明:本網頁內容旨在傳播知識,若有侵權等問題請及時與本網聯系,我們將在第一時間刪除處理。TEL:177 7030 7066 E-MAIL:11247931@qq.com