JQuery switches the Page Transition animation effect and jquery Page Transition Animation
I hope you will like the production process.
HTML Structure
The HTML structure of this page switching special effect uses a <main> element as the page's wrap element, div. cd-cover-layer is used to create the mask layer for page switching, div. cd-loading-bar is the loading progress bar during ajax loading.
<main> <div class="cd-index cd-main-content"> <div>
CSS style
In this page switching effect, the body: before AND body: after pseudo elements are used to create two mask layers during the page switching process to cover the page content. They are positioned at a fixed position, with a height equal to 50 Gbit/s and a width of 100%. By default, the CSS transform attribute is used to hide them (translateY (-100%)/translateY (100% )). When you switch pages, these elements are moved to the playback interface (by adding. page-is-changing class to the <body> element ).
The following figure demonstrates the process:
Page switching Special Effects
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);}
During page switching, the fade-in and fade-out effect of the page content is achieved by changing the transparency of div. cd-cover-layer. It overwrites. the cd-main-content element has the same background color and is added to <body>. when page-is-changing class is used, the transparency is changed from 0 to 1.
The Loading progress bar is created using the. cd-loading-bar: before pseudo element. By default, it is reduced (scaleX (0) and transform-origin: left center. When page switching starts, scaleX (1) is used to enlarge the original size.
.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);}
The smooth transition effect in the special effect is implemented using CSS Transitions. Each animation element is added with different transition-delay to achieve different element animation sequence.
JAVASCRIPT
The data-type = "page-transition" attribute is used on the link in this page switching effect to trigger page switching events. When the plugin detects a user Click Event, the changePage () method is executed.
$('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);});
This method triggers the page switching animation and loads new content using the loadNewContent () method.
function changePage(url, bool) { isAnimating = true; // trigger page animation $('body').addClass('page-is-changing'); //... loadNewContent(url, bool); //...}
When the new content is loaded, it replaces the content in the original <main> element .. The page-is-changing class is removed from the body, and the newly loaded content is added to window. history (using the pushState () method ).
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); } });}
To trigger the same page switching animation effect when you click the back button of the browser, the plug-in listens to the popstate event and runs the changePage () function when it triggers the event.
$(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);});