While cleaning a few landers and removing jQuery, I realized that the most common reasons to use jQuery is for animations and slideshows. The most common one being the fadeIn / fadeOut. Here is a small script that I found and modified, to replicate the jQuery fadeIn / fadeOut functions.
Feel free to ask questions on how it works and I will explain more if needed.
Basic fadeOut: make the element disappear
function fadeOut(element) {
element.style.opacity = 1;
(function fade() {
if ((element.style.opacity -= 0.05) < 0) {
element.style.display = "none";
} else {
requestAnimationFrame(fade);
}
})();
}
function fadeIn(element) {
element.style.opacity = 0;
element.style.display = "block";
(function fade() {
var val = parseFloat(element.style.opacity);
if (!((val += 0.1) > 1)) {
element.style.opacity = val;
requestAnimationFrame(fade);
}
})();
}
function fadeOut(element, duration = false) {
element.style.opacity = 1;
(function fade() {
if ((element.style.opacity -= 0.1) < 0) {
element.style.display = "none";
} else {
if (duration) {
setTimeout(() => {
requestAnimationFrame(fade);
}, duration / 10);
} else {
requestAnimationFrame(fade);
}
}
})();
}
function fadeIn(element, duration = false) {
element.style.opacity = 0;
element.style.display = "block";
(function fade() {
var val = parseFloat(element.style.opacity);
if (!((val += 0.1) > 1)) {
element.style.opacity = val;
if (duration) {
setTimeout(() => {
requestAnimationFrame(fade);
}, duration / 10);
} else {
requestAnimationFrame(fade);
}
}
})();
}
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>FadeIn/Out Test</title>
<style>
* {
padding: 20px;
margin: 20px;
}
#square {
background-color: chartreuse;
display: block;
width: 200px;
height: 200px
}
</style>
</head>
<body>
<input type='number' id='duration' value='500'>
<button id='btn-fadein'>FADE IN</button>
<button id='btn-fadeout'>FADE OUT</button>
<div id='square'></div>
<script>
var d = document,
sq = d.getElementById('square');
d.getElementById('btn-fadein').addEventListener('click', () => {
if (dur = parseInt(d.getElementById('duration').value)) {
fadeIn(sq, dur);
} else {
bfadeIn(sq);
}
}, false)
d.getElementById('btn-fadeout').addEventListener('click', () => {
if (dur = parseInt(d.getElementById('duration').value)) {
fadeOut(sq, dur);
} else {
bfadeOut(sq);
}
}, false)
/* bfadeOut: make the element disappear
function bfadeOut(element) {
element.style.opacity = 1;
(function fade() {
if ((element.style.opacity -= 0.05) < 0) {
element.style.display = "none";
} else {
requestAnimationFrame(fade);
}
})();
}
*/
/* bfadeIn: make the element appear
function bfadeIn(element) {
element.style.opacity = 0;
element.style.display = "block";
(function fade() {
var val = parseFloat(element.style.opacity);
if (!((val += 0.1) > 1)) {
element.style.opacity = val;
requestAnimationFrame(fade);
}
})();
}
*/
/* fadeOut: make the element disappear with duration
*/
function fadeOut(element, duration = false) {
element.style.opacity = 1;
(function fade() {
if ((element.style.opacity -= 0.1) < 0) {
element.style.display = "none";
} else {
if (duration) {
setTimeout(() => {
requestAnimationFrame(fade);
}, duration / 10);
} else {
requestAnimationFrame(fade);
}
}
})();
}
/* fadeIn: make the element appear with duration
*/
function fadeIn(element, duration = false) {
element.style.opacity = 0;
element.style.display = "block";
(function fade() {
var val = parseFloat(element.style.opacity);
if (!((val += 0.1) > 1)) {
element.style.opacity = val;
if (duration) {
setTimeout(() => {
requestAnimationFrame(fade);
}, duration / 10);
} else {
requestAnimationFrame(fade);
}
}
})();
}
</script>
</body>
</html>
Okay... but why? This can be done with css animation and few lines of js.
I just checked and animation is supported in all browsers except Opera Mini - so this one, and really ancient browser versions would be the only cases that it is worth adding js version :P
https://developer.mozilla.org/en-US/...CSS_Animations
https://caniuse.com/css-animation
CSS animation solution (modern approach):
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>FadeIn/Out Test</title>
<style>
* {
padding: 20px;
margin: 20px;
}
#square {
background-color: chartreuse;
display: block;
width: 200px;
height: 200px
}
@keyframes fadeIn {
0% {
opacity: 0.0;
display: block;
}
100% {
opacity: 1.0;
}
}
@keyframes fadeOut {
0% {
opacity: 1.0;
}
100% {
opacity: 0.0;
display: none;
}
}
:root {
--animation-duration: 5s
}
.fadeIn {
animation-name: fadeIn;
animation-duration: var(--animation-duration);
animation-fill-mode: forwards;
}
.fadeOut {
animation-name: fadeOut;
animation-duration: var(--animation-duration);
animation-fill-mode: forwards;
}
</style>
</head>
<body>
<button id='btn-fadein'>FADE IN</button>
<button id='btn-fadeout'>FADE OUT</button>
<div id='square'></div>
<script>
var d = document,
sq = d.getElementById('square');
d.getElementById('btn-fadein').addEventListener('click', () => {
sq.classList.remove('fadeOut');
sq.classList.add('fadeIn');
}, false)
d.getElementById('btn-fadeout').addEventListener('click', () => {
sq.classList.remove('fadeIn');
sq.classList.add('fadeOut');
}, false)
</script>
</body>
$('#square').addClass('fadeIn').removeClass('fadeOut')
$('#square').addClass('fadeOut').removeClass('fadeIn')
I know that CSS animation is the modern approach. If I start a lander from scratch, yes, I would use that solution too.
Yet:
- not everyone has the knowledge of CSS keyframes.
- when I rip a lander and just want to remove the jQuery module, I find is faster not to have to dig into the CSS and just do a straight replacement.