JSFiddle - React, Tailwind, and code Playground
by interlife
HTML
<h1 class="text-slide js-inview">テキスト帯スライドアニメーション</h1>
SCSS
@keyframes mask-bg {
0% {
transform: translate(-101%, 0)
}
40%, 60% {
transform: translate(0, 0%)
}
100% {
transform: translate(100%, 0)
}
}
.text-slide {
display: inline-block;
position: relative;
overflow: hidden;
color: transparent;
transition: color 0ms 450ms;
&:before {
display: block;
position: absolute;
top: 0;
right: 0;
bottom: 0;
left: 0;
transform: translate(101%,0);
background: #ddd;
content: '';
}
&.animation {
color: inherit;
background: inherit;
&:before {
animation: mask-bg 1.2s cubic-bezier(0.8, 0, 0.170, 1);
}
}
}
JavaScript
function animeType(className) {
const targets = document.querySelectorAll('.js-' + className);
if (!targets.length) return;
const observer = new IntersectionObserver((entries, obs) => {
entries.forEach(entry => {
if (entry.isIntersecting) {
entry.target.classList.add('animation');
obs.unobserve(entry.target);
}
});
}, {
root: null,
rootMargin: "0px 0px -15% 0px",
threshold: 0
});
targets.forEach(target => observer.observe(target));
}
animeType('inview');