Skip to content

Instantly share code, notes, and snippets.

@Tommy-b
Last active June 25, 2023 15:14
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Tommy-b/8378414cdd92dea887ee2f902a74a802 to your computer and use it in GitHub Desktop.
Save Tommy-b/8378414cdd92dea887ee2f902a74a802 to your computer and use it in GitHub Desktop.
Basic setup for using Swup page transitions and GSAP in a Wordpress site
$(function() {
// Add your sites functions to under one main function names somwthing
// along the lines of:
initMain();
const options = [
{
from: '(.*)',
to: '(.*)',
in: function(next) {
document.querySelector('#swup').style.opacity = 0;
TweenLite.to(document.querySelector('#swup'), 0.5, {
opacity: 1,
onComplete: next
});
},
out: (next) => {
document.querySelector('#swup').style.opacity = 1;
TweenLite.to(document.querySelector('#swup'), 0.5, {
opacity: 0,
onComplete: next
});
}
}
];
// List of needed Swup plugins
const swup = new Swup({
plugins: [
new SwupJsPlugin(options),
new SwupBodyClassPlugin(),
new SwupHeadPlugin(),
new SwupGaPlugin(),
]
});
swup.on('contentReplaced', function() {
// Run sites main functions after transition
initMain();
});
// ****** An example of how to access page content during transitions. Is
// being used below to update the main nav to reflect current page ****** //
// swup.on('transitionStart', function() {
// const oldMenuItem = $('.current-menu-item');
// oldMenuItem.removeClass('current-menu-item');
// });
// swup.on('popState', function() {
// const oldMenuItem = $('.current-menu-item');
// oldMenuItem.removeClass('current-menu-item');
// const page = swup.cache.getCurrentPage();
// const newMenuItem = $(page.originalContent).find('.current-menu-item').attr('id');
// $('#' + newMenuItem).addClass('current-menu-item');
// });
// swup.on('willReplaceContent', function() {
// const page = swup.cache.getCurrentPage();
// const newMenuItem = $(page.originalContent).find('.current-menu-item').attr('id');
// $('#' + newMenuItem).addClass('current-menu-item');
// });
// ******* Un-comment if you want Swup disabled on mobile devices ******* //
// function checkWidth(x) {
// if (x.matches) { // If media query matches
// } else {
// swup.destroy();
// }
// }
// var x = window.matchMedia('(min-width: 768px)')
// checkWidth(x) // Call listener function at run time
// x.addListener(checkWidth) // Attach listener function on state changes
});
Basic setup for using Swup page transitions and GSAP in a Wordpress site.
Notes (Will update as time goes on) :
1. Be sure that if you use ID's on elements there is only one throughout the site.
Since there is no actual page load Chrome has issues determining that you are not
using the same ID multiple times.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment