基于javascript實(shí)現(xiàn)漂亮的頁(yè)面過渡動(dòng)畫效果附源碼下載
用戶通過點(diǎn)擊頁(yè)面左側(cè)的菜單,對(duì)應(yīng)的頁(yè)面加載時(shí)伴隨著滑動(dòng)過濾動(dòng)畫,并帶有進(jìn)度條效果。當(dāng)然頁(yè)面的加載是Ajax驅(qū)動(dòng)的,整個(gè)加載過渡過程非常流暢,非常好的用戶體驗(yàn)。
HTML
HTML結(jié)構(gòu)中,.cd-main包含頁(yè)面主體內(nèi)容,.cd-side-navigation包含著側(cè)邊導(dǎo)航條,#cd-loading-bar則是用來做進(jìn)度條動(dòng)畫用的。
<nav class="cd-side-navigation">
<ul>
<li>
<a href="index.html" class="selected" data-menu="index">
<svg><!-- svg content here --></svg>
Intro
</a>
</li>
<li>
<!-- ... -->
</li>
<!-- other list items here -->
</ul>
</nav> <!-- .cd-dashboard -->
<main class="cd-main">
<section class="cd-section index visible">
<header>
<div class="cd-title">
<h2>Animated Page Transition #2</h2>
<span>Some text here</span>
</div>
<a href="#index-content" class="cd-scroll">Scroll Down</a>
</header>
<div class="cd-content" id="index-content">
<!-- content here -->
</div> <!-- .cd-content -->
</section> <!-- .cd-section -->
</main> <!-- .cd-main -->
<div id="cd-loading-bar" data-scale="1" class="index"></div> <!-- lateral loading bar -->
CSS
我們將.cd-side-navigation固定在頁(yè)面左側(cè),并且設(shè)置它的高度為100%,這樣左側(cè)導(dǎo)航菜單就始終占據(jù)左側(cè)邊欄,右側(cè)主體內(nèi)容滾動(dòng)時(shí),左側(cè)導(dǎo)航菜單不動(dòng)。
.cd-side-navigation {
position: fixed;
z-index: 3;
top: 0;
left: 0;
height: 100vh;
width: 94px;
overflow: hidden;
}
.cd-side-navigation ul {
height: 100%;
overflow-y: auto;
}
.cd-side-navigation::before {
/* background color of the side navigation */
content: '';
position: absolute;
top: 0;
left: 0;
height: 100%;
width: calc(100% - 4px);
background-color: #131519;
}
.cd-side-navigation li {
width: calc(100% - 4px);
}
.cd-side-navigation a {
display: block;
position: relative;
}
.cd-side-navigation a::after {
/* 4px line to the right of the item - visible on hover */
content: '';
position: absolute;
top: 0;
right: -4px;
height: 100%;
width: 4px;
background-color: #83b0b9;
opacity: 0;
}
.no-touch .cd-side-navigation a:hover::after {
opacity: 1;
}
JavaScript
當(dāng)我們點(diǎn)擊左側(cè)菜單時(shí),調(diào)用triggerAnimation()函數(shù),這個(gè)函數(shù)會(huì)觸發(fā)加載進(jìn)度條動(dòng)畫函數(shù)loadingBarAnimation(),接著加載頁(yè)面內(nèi)容函數(shù):loadNewContent()。
function loadingBarAnimation() {
var scaleMax = loadingBar.data('scale');
if( scaleY + 1 < scaleMax) {
newScaleValue = scaleY + 1;
}
// ...
loadingBar.velocity({
scaleY: newScaleValue
}, 100, loadingBarAnimation);
}
當(dāng)新頁(yè)面被選中時(shí),一個(gè)新的元素.cd-section將會(huì)被創(chuàng)建并且插入到DOM中,然后load()新的url內(nèi)容。
function loadNewContent(newSection) {
var section = $('<section class="cd-section overflow-hidden '+newSection+'"></section>').appendTo(mainContent);
//load the new content from the proper html file
section.load(newSection+'.html .cd-section > *', function(event){
loadingBar.velocity({
scaleY: scaleMax //this is the scaleY value to cover the entire window height (100% loaded)
}, 400, function(){
section.addClass('visible');
var url = newSection+'.html';
if(url!=window.location){
//add the new page to the window.history
window.history.pushState({path: url},'',url);
}
// ...
});
});
}
通過異步加載的頁(yè)面要返回上一頁(yè)歷史瀏覽記錄的話,可以點(diǎn)擊瀏覽器上的返回即可。返回上一頁(yè)同樣有過渡動(dòng)畫效果。
- jQuery+CSS3實(shí)現(xiàn)3D立方體旋轉(zhuǎn)效果
- JS實(shí)現(xiàn)從頂部下拉顯示的帶動(dòng)畫QQ客服特效代碼
- JavaScript+CSS實(shí)現(xiàn)仿Mootools豎排彈性動(dòng)畫菜單效果
- JS實(shí)現(xiàn)適合于后臺(tái)使用的動(dòng)畫折疊菜單效果
- js實(shí)現(xiàn)div拖動(dòng)動(dòng)畫運(yùn)行軌跡效果代碼分享
- js+css實(shí)現(xiàn)文字散開重組動(dòng)畫特效代碼分享
- js實(shí)現(xiàn)按鈕顏色漸變動(dòng)畫效果
- javascript+HTML5的Canvas實(shí)現(xiàn)Lab單車動(dòng)畫效果
- javascript動(dòng)畫算法實(shí)例分析
- WebGL利用FBO完成立方體貼圖效果完整實(shí)例(附demo源碼下載)
相關(guān)文章
js實(shí)現(xiàn)一個(gè)逐步遞增的數(shù)字動(dòng)畫
可視化大屏項(xiàng)目使用最多的組件就是數(shù)字組件,本文主要介紹了js實(shí)現(xiàn)一個(gè)逐步遞增的數(shù)字動(dòng)畫,文中通過示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2021-12-12
JS基于設(shè)計(jì)模式中的單例模式(Singleton)實(shí)現(xiàn)封裝對(duì)數(shù)據(jù)增刪改查功能
這篇文章主要介紹了JS基于設(shè)計(jì)模式中的單例模式(Singleton)實(shí)現(xiàn)封裝對(duì)數(shù)據(jù)增刪改查功能.結(jié)合實(shí)例形式分析了javascript基于單例模式結(jié)合ajax針對(duì)數(shù)據(jù)庫(kù)進(jìn)行增刪改查的相關(guān)操作技巧,需要的朋友可以參考下2018-02-02
JavaScript判斷頁(yè)面是否滾動(dòng)到底部的技巧
在 JavaScript 中,我們可以通過一些技巧來判斷頁(yè)面是否滾動(dòng)到底部,本文將介紹一些常用的方法,幫助你在項(xiàng)目中實(shí)現(xiàn)這一功能,文中通過代碼示例介紹的非常詳細(xì),需要的朋友可以參考下2023-11-11
微信小程序?qū)崿F(xiàn)簡(jiǎn)單跑馬燈效果
這篇文章主要為大家詳細(xì)介紹了微信小程序?qū)崿F(xiàn)簡(jiǎn)單跑馬燈效果 ,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2018-12-12
TensorFlow.js 微信小程序插件開始支持模型緩存的方法
這篇文章主要介紹了TensorFlow.js 微信小程序插件開始支持模型緩存的方法,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2020-02-02

