TweenMax.js创建的8款文字鼠标悬停动画效果
作者:admin 时间:2022-1-27 15:35:24 浏览:前文介绍了lettering.js创建的5款文字按钮鼠标悬停动画效果,本文继续介绍8款文字鼠标悬停动画效果,使用TweenMax.js创建实现。
HTML
<ul class="grid">
<li class="grid__item">
<h2 class="letter-wrap">卡卡测速网</h2>
</li>
<li class="grid__item">
<h2 class="letter-wrap" data-duration="0.4" data-stagger="0.1">webkaka.com</h2>
</li>
<li class="grid__item">
<h2 class="letter-wrap" data-duration="0.2" data-stagger="-0.05">卡卡测速网</h2>
</li>
<li class="grid__item">
<h2 class="letter-wrap" data-duration="0.1" data-stagger="0.1">webkaka.com</h2>
</li>
<li class="grid__item">
<h2 class="letter-wrap" data-stagger="-0.015">卡卡测速网</h2>
</li>
<li class="grid__item">
<h2 class="letter-wrap" data-duration="1" data-stagger="0.025">webkaka.com</h2>
</li>
<li class="grid__item">
<h2 class="letter-wrap" data-duration="0.8" data-stagger="-0.025">卡卡测速网</h2>
</li>
<li class="grid__item">
<h2 class="letter-wrap" data-duration="0.1" data-stagger="-0.075">webkaka.com</h2>
</li>
</ul>
文字容器使用h2
元素标签,其class值是letter-wrap
。
data-duration
属性表示动画运行时间,值越大,速度越慢,运行时间越长。
data-stagger
是每个文字运行间隔时间,值越大,每个文字运行间隔时间越长,负值表示动画从右向左运动。
JS
实例文字动画使用第三方插件TweenMax.min.js
,因此需要引用该JS库文件。
<script src='TweenMax.min.js'></script>
<script>
const letterWrapClass = 'letter-wrap';
const letterWrapElements = document.getElementsByClassName(letterWrapClass);
[...letterWrapElements].forEach(el => {
letterWrap(el, letterWrapClass);
letterAnimation(el, letterWrapClass);
});
function letterWrap(el, cls) {
const words = el.textContent.split(' ');
const letters = [];
cls = cls || 'letter-wrap';
words.forEach(word => {
let html = '';
for (var letter in word) {
html += `
<span class="${cls}__char">
<span class="${cls}__char-inner" data-letter="${word[letter]}">
${word[letter]}
</span>
</span>
`;
};
let wrappedWords = `<span class="${cls}__word">${html}</span>`;
letters.push(wrappedWords);
});
return el.innerHTML = letters.join(' ');
}
function letterAnimation(el, cls) {
const tl = new TimelineMax({ paused: true });
const characters = el.querySelectorAll(`.${cls}__char-inner`);
const duration = el.hasAttribute('data-duration') ? el.dataset.duration : 0.3;
const stagger = el.hasAttribute('data-stagger') ? el.dataset.stagger : 0.03;
el.animation = tl.staggerTo(characters, duration, {
y: '-100%',
ease: Power4.easeOut },
stagger);
el.addEventListener('mouseenter', event => event.currentTarget.animation.play());
el.addEventListener('mouseout', event => el.animation.reverse());
}
</script>
CSS
h2 {
font-size: var(--h2-fs);
text-align: center;
color: var(--primary-clr);
}
.letter-wrap {
display: inline-block;
position: relative;
}
.letter-wrap * {
pointer-events: none;
}
.letter-wrap__word {
display: inline-flex;
position: relative;
overflow: hidden;
}
.letter-wrap__char {
display: inline-block;
position: relative;
text-transform: uppercase;
}
.letter-wrap__char-inner {
display: inline-block;
}
.letter-wrap__char-inner:after {
content: attr(data-letter);
position: absolute;
top: 100%;
left: 0;
color: var(--secondary-clr);
}
h2
、.letter-wrap
等CSS属性,主要是设置文字位置、大小、颜色等样式,不涉及到动画设置,动画完全由TweenMax.js
来完成。
总结
本文介绍了8款文字鼠标悬停动画效果,使用TweenMax.js创建实现。
有关文字动画的文章,也可参考前文lettering.js创建的5款文字按钮鼠标悬停动画效果。
相关文章
标签: TweenMax.js 鼠标悬停效果
相关文章
x
- 站长推荐