If you want to enhance your website with some CSS and Javascript magic, here's a nifty solution for you:
Instead of using traditional <a>
tags in your DOM for links to other pages, try using a <span>
with an onclick attribute like so:
<span onclick="goToPage('https://www.google.com')"></span>
(You can stick to <a>
with href
, but extra parsing is needed to override the default event.)
In your Javascript file, add the following function:
window.goToPage = function(href) {
document.querySelector('body').style.opacity = 0
setTimeout(function() {
window.location.href = href
}, 500)
}
document.addEventListener('DOMContentLoaded', function(event) {
document.querySelector('body').style.opacity = 1
})
And don't forget to include this snippet in your CSS file:
body {
opacity: 0;
transition: opacity .5s;
}
This clever setup provides the following benefits:
- Smooth fade-in when the page loads
- Graceful fade-out and swift transition to the next page upon link click
Happy coding adventures!