gpt4 book ai didi

html - 纯 CSS 响应式文字效果

转载 作者:行者123 更新时间:2023-12-04 07:55:35 28 4
gpt4 key购买 nike

我的问题很简单。考虑以下 CodePen .是否有可能仅使用 css 就可以获得相同的结果?换句话说,如果不使用 javascrip 如何做到这一点?非常感谢!

<div class="text">
<p>Nachos are</p>
<p>
<span class="word wisteria">tasty.</span>
<span class="word belize">wonderful.</span>
<span class="word pomegranate">fancy.</span>
<span class="word green">beautiful.</span>
<span class="word midnight">cheap.</span>
</p>
</div>

@import url(https://fonts.googleapis.com/css?family=Open+Sans:600);

body {
font-family: 'Open Sans', sans-serif;
font-weight: 600;
font-size: 40px;
}

.text {
position: absolute;
width: 450px;
left: 50%;
margin-left: -225px;
height: 40px;
top: 50%;
margin-top: -20px;
}

p {
display: inline-block;
vertical-align: top;
margin: 0;
}

.word {
position: absolute;
width: 220px;
opacity: 0;
}

.letter {
display: inline-block;
position: relative;
float: left;
transform: translateZ(25px);
transform-origin: 50% 50% 25px;
}

.letter.out {
transform: rotateX(90deg);
transition: transform 0.32s cubic-bezier(0.55, 0.055, 0.675, 0.19);
}

.letter.behind {
transform: rotateX(-90deg);
}

.letter.in {
transform: rotateX(0deg);
transition: transform 0.38s cubic-bezier(0.175, 0.885, 0.32, 1.275);
}

.wisteria {
color: #8e44ad;
}

.belize {
color: #2980b9;
}

.pomegranate {
color: #c0392b;
}

.green {
color: #16a085;
}

.midnight {
color: #2c3e50;
}

var words = document.getElementsByClassName('word');
var wordArray = [];
var currentWord = 0;

words[currentWord].style.opacity = 1;
for (var i = 0; i < words.length; i++) {
splitLetters(words[i]);
}

function changeWord() {
var cw = wordArray[currentWord];
var nw = currentWord == words.length-1 ? wordArray[0] : wordArray[currentWord+1];
for (var i = 0; i < cw.length; i++) {
animateLetterOut(cw, i);
}

for (var i = 0; i < nw.length; i++) {
nw[i].className = 'letter behind';
nw[0].parentElement.style.opacity = 1;
animateLetterIn(nw, i);
}

currentWord = (currentWord == wordArray.length-1) ? 0 : currentWord+1;
}

function animateLetterOut(cw, i) {
setTimeout(function() {
cw[i].className = 'letter out';
}, i*80);
}

function animateLetterIn(nw, i) {
setTimeout(function() {
nw[i].className = 'letter in';
}, 340+(i*80));
}

function splitLetters(word) {
var content = word.innerHTML;
word.innerHTML = '';
var letters = [];
for (var i = 0; i < content.length; i++) {
var letter = document.createElement('span');
letter.className = 'letter';
letter.innerHTML = content.charAt(i);
word.appendChild(letter);
letters.push(letter);
}

wordArray.push(letters);
}

changeWord();
setInterval(changeWord, 4000);

最佳答案

是的。基本上是可以的!
但确实,计算每个该死的字母的动画真的很困难……但我不太确定是否还有一些动画工作室在玩这种东西……
所以,如果你想正好喜欢这样做(= 带有字母动画)我更喜欢使用非常好的 JS 代码段(感谢在这里展示)。
但是如果你不能/愿意在你的元素中使用 JS 并且你想做 东西 像那样,您可以使用 @keyframes 在 CSS 中轻松实现完整的单词更改动画。
请参阅下面的解释的快速而肮脏的示例。
注意:动画真的很简单!当然,您可以使用旋转、背景等来制作更酷的效果。如果您喜欢根据自己的想象力调整示例:-)

* {
-webkit-box-sizing: border-box;
box-sizing: border-box;
}

html {
font-family: sans-serif;
}

.text {
position: relative;
margin: 20px;
font-size: 1.5rem;
line-height: 1.2em;
}

.wordChange__wrapper {
display: relative;
}

.wordChange__changer {
display: inline-block;
position: absolute;
top: -100%;
width: 220px;
opacity: 0;
-webkit-animation: wordchanger;
animation: wordchanger;
-webkit-animation-duration: 10s;
animation-duration: 10s;
-webkit-animation-iteration-count: infinite;
animation-iteration-count: infinite;
}

.wordChange__element_1 {
-webkit-animation-delay: 0s;
animation-delay: 0s;
}

.wordChange__element_2 {
-webkit-animation-delay: 2s;
animation-delay: 2s;
}

.wordChange__element_3 {
-webkit-animation-delay: 4s;
animation-delay: 4s;
}

.wordChange__element_4 {
-webkit-animation-delay: 6s;
animation-delay: 6s;
}

.wordChange__element_5 {
-webkit-animation-delay: 8s;
animation-delay: 8s;
}

@-webkit-keyframes wordchanger {
0% {
top: -100%;
opacity: 0;
}
4.5% {
opacity: 0;
}
9% {
top: 0;
opacity: 1;
}
20% {
top: 0;
opacity: 1;
-webkit-transform: rotate(0deg);
transform: rotate(0deg);
}
24.5% {
opacity: 0;
}
29% {
top: 100%;
opacity: 0;
}
}

@keyframes wordchanger {
0% {
top: -100%;
opacity: 0;
}
4.5% {
opacity: 0;
}
9% {
top: 0;
opacity: 1;
}
20% {
top: 0;
opacity: 1;
-webkit-transform: rotate(0deg);
transform: rotate(0deg);
}
24.5% {
opacity: 0;
}
29% {
top: 100%;
opacity: 0;
}
}
.wisteria {
color: #8e44ad;
}

.belize {
color: #2980b9;
}

.pomegranate {
color: #c0392b;
}

.green {
color: #16a085;
}

.midnight {
color: #2c3e50;
}
<div class="text">
<p class="wordChange__wrapper">Nachos are&nbsp;
<span class="wordChange__changer wordChange__element_1 wisteria">tasty.</span>
<span class="wordChange__changer wordChange__element_2 belize">wonderful.</span>
<span class="wordChange__changer wordChange__element_3 pomegranate">fancy.</span>
<span class="wordChange__changer wordChange__element_4 green">beautiful.</span>
<span class="wordChange__changer wordChange__element_5 midnight">cheap.</span>
</p>
</div>

关于html - 纯 CSS 响应式文字效果,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/66728589/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com