jQuery實(shí)現(xiàn)切換頁(yè)面過渡動(dòng)畫效果
直接為大家介紹制作過程,希望大家可以喜歡。
HTML結(jié)構(gòu)
該頁(yè)面切換特效的HTML結(jié)構(gòu)使用一個(gè)<main>元素來作為頁(yè)面的包裹元素,div.cd-cover-layer用于制作頁(yè)面切換時(shí)的遮罩層,div.cd-loading-bar是進(jìn)行ajax加載時(shí)的loading進(jìn)度條。
<main> <div class="cd-index cd-main-content"> <div> <h1>Page Transition</h1> <!-- your content here --> </div> </div> </main> <div class="cd-cover-layer"></div> <!-- this is the cover layer --> <div class="cd-loading-bar"></div> <!-- this is the loading bar -->
CSS樣式
該頁(yè)面切換特效中使用body::before和body::after偽元素在頁(yè)面切換過程中創(chuàng)建兩個(gè)遮罩層來遮住頁(yè)面內(nèi)容。它們的定位是固定定位,高度等于50vh,寬度為100%。默認(rèn)情況下,使用CSS transform屬性將它們隱藏起來(translateY(-100%)/translateY(100%))。當(dāng)用戶切換頁(yè)面的時(shí)候,這些元素被移動(dòng)回視口當(dāng)中(通過在<body>元素上添加.page-is-changing class)。
下面的圖片演示了這個(gè)過程:

頁(yè)面切換特效
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);
}
頁(yè)面切換時(shí),頁(yè)面內(nèi)容的淡入淡出效果是通過改變div.cd-cover-layer的透明度實(shí)現(xiàn)的。它覆蓋了.cd-main-content元素,并具有相同的背景色,然后在<body>被添加.page-is-changing class的時(shí)候,將透明度從0修改為1。
Loading進(jìn)度條使用.cd-loading-bar::before偽元素來制作。默認(rèn)它被縮?。╯caleX(0))和transform-origin: left center。當(dāng)頁(yè)面切換開始時(shí)它被使用scaleX(1)放大會(huì)原來的尺寸。
.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來實(shí)現(xiàn)。每一個(gè)動(dòng)畫元素都被添加了不同的transition-delay,以實(shí)現(xiàn)不同的元素動(dòng)畫順序。
JAVASCRIPT
該頁(yè)面切換特效中在鏈接上使用data-type="page-transition"屬性,用于觸發(fā)頁(yè)面切換事件。當(dāng)插件檢測(cè)到用戶點(diǎn)擊事件,changePage()方法將被執(zhí)行。
$('main').on('click', '[data-type="page-transition"]', function(event){
event.preventDefault();
//detect which page has been selected
var newPage = $(this).attr('href');
//if the page is not animating - trigger animation
if( !isAnimating ) changePage(newPage, true);
});
這個(gè)方法會(huì)觸發(fā)頁(yè)面切換動(dòng)畫,并通過loadNewContent()方法加載新內(nèi)容。
function changePage(url, bool) {
isAnimating = true;
// trigger page animation
$('body').addClass('page-is-changing');
//...
loadNewContent(url, bool);
//...
}
當(dāng)新的內(nèi)容被加載后,會(huì)替代原來<main>元素中的內(nèi)容。.page-is-changing class被從body中移除,新加載的內(nèi)容會(huì)被添加到window.history中(使用pushState()方法)。
function loadNewContent(url, bool) {
var newSectionName = 'cd-'+url.replace('.html', ''),
section = $('<div class="cd-main-content '+newSectionName+'"></div>');
section.load(url+' .cd-main-content > *', function(event){
// load new content and replace <main> content with the new one
$('main').html(section);
//...
$('body').removeClass('page-is-changing');
//...
if(url != window.location){
//add the new page to the window.history
window.history.pushState({path: url},'',url);
}
});
}
為了在用戶點(diǎn)擊瀏覽器的回退按鈕時(shí)觸發(fā)相同的頁(yè)面切換動(dòng)畫效果,插件中監(jiān)聽popstate事件,并在它觸發(fā)時(shí)執(zhí)行changePage()函數(shù)。
$(window).on('popstate', function() {
var newPageArray = location.pathname.split('/'),
//this is the url of the page to be loaded
newPage = newPageArray[newPageArray.length - 1];
if( !isAnimating ) changePage(newPage);
}); 以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
- 簡(jiǎn)單實(shí)現(xiàn)js頁(yè)面切換功能
- js實(shí)現(xiàn)的鼠標(biāo)滾輪滾動(dòng)切換頁(yè)面效果(類似360默認(rèn)頁(yè)面滾動(dòng)切換效果)
- js實(shí)現(xiàn)單一html頁(yè)面兩套css切換代碼
- echarts同一頁(yè)面中四個(gè)圖表切換的js數(shù)據(jù)交互方法示例
- 基于JS實(shí)現(xiàn)翻書效果的頁(yè)面切換樣式
- 使用AngularJS實(shí)現(xiàn)可伸縮的頁(yè)面切換的方法
- javascript單頁(yè)面手勢(shì)滑屏切換原理詳解
- JavaScript實(shí)現(xiàn)鼠標(biāo)滾輪控制頁(yè)面圖片切換功能示例
- jquery結(jié)合html實(shí)現(xiàn)中英文頁(yè)面切換
- JavaScript/jQuery實(shí)現(xiàn)切換頁(yè)面效果
相關(guān)文章
jQuery中$this和$(this)的區(qū)別介紹(一看就懂)
這篇文章主要介紹了jQuery中$this和$(this)的區(qū)別介紹(一看就懂),本文用簡(jiǎn)潔的語(yǔ)言講解了它們之間的區(qū)別,并給出了一個(gè)例子來說明,需要的朋友可以參考下2015-07-07
jQuery+php實(shí)時(shí)獲取及響應(yīng)文本框輸入內(nèi)容的方法
這篇文章主要介紹了jQuery+php實(shí)時(shí)獲取及響應(yīng)文本框輸入內(nèi)容的方法,涉及jQuery響應(yīng)鍵盤事件及ajax調(diào)用php文件針對(duì)輸入內(nèi)容的處理與回調(diào)相關(guān)技巧,非常簡(jiǎn)單易懂,需要的朋友可以參考下2016-05-05
jQuery css() 方法動(dòng)態(tài)修改CSS屬性
在jquery中我們要?jiǎng)討B(tài)的修改css屬性我們只要使用css()方法就可以實(shí)現(xiàn)了,下面我來給各位同學(xué)詳細(xì)介紹介紹。2016-09-09
jquery獲取html元素的絕對(duì)位置和相對(duì)位置的方法
這篇文章主要介紹了jquery獲取html元素的絕對(duì)位置和相對(duì)位置的方法,需要的朋友可以參考下2014-06-06
JQuery.get提交頁(yè)面不跳轉(zhuǎn)的解決方法
這篇文章主要介紹了JQuery.get提交頁(yè)面不跳轉(zhuǎn)的解決方法,本文分析了不跳轉(zhuǎn)的原因同時(shí)給出了跳轉(zhuǎn)的方法,需要的朋友可以參考下2015-01-01
jQuery-Easyui 1.2 實(shí)現(xiàn)多層菜單效果的代碼
早上打開郵箱,一位朋友問我之前JQuery-Easyui 怎么做可以實(shí)現(xiàn)多級(jí)菜單2012-01-01
jQuery中attr()與prop()函數(shù)用法實(shí)例詳解(附用法區(qū)別)
這篇文章主要介紹了jQuery中attr()與prop()函數(shù)用法,結(jié)合實(shí)例形式詳細(xì)分析了attr()與prop()函數(shù)的使用技巧與相關(guān)注意事項(xiàng),并附帶了attr()與prop()函數(shù)用法的區(qū)別,需要的朋友可以參考下2015-12-12
讓元素在網(wǎng)頁(yè)中可拖動(dòng)示例代碼
讓元素可拖動(dòng)的方法有很多,本文為大家介紹下使用jquery簡(jiǎn)單實(shí)現(xiàn)拖動(dòng)效果,感興趣的朋友可以參考下,希望對(duì)大家有所幫助2013-08-08
jquery實(shí)現(xiàn)文本框數(shù)量加減功能的例子分享
在做商城項(xiàng)目的時(shí)候,需要在購(gòu)物車中增加一個(gè)商品加減功能,并在加減時(shí)同時(shí)修改總價(jià)格的顯示,并且保證文本框text只能輸入純數(shù)字2014-05-05

