利用绝对定位、3D 变换和 JS 实现翻转文字

CSS Animation is

awesome.beautiful.creative.fabulous.interesting.

实现代码

<template>
  <div class="rotating-text">
    <p>CSS Animation is</p>
    <p>
      <span
        v-for="(item, i) in list"
        :ref="
          (el) => {
            if (el) words[i] = el;
          }
        "
        :key="i"
        :class="['word', item[0]]"
        >{{ item[1] }}.</span
      >
    </p>
  </div>
</template>

<script setup>
import { ref, onMounted } from 'vue';

const words = ref([]);

const list = [
  ['alizarin', 'awesome'],
  ['wisteria', 'beautiful'],
  ['peter-river', 'creative'],
  ['emerald', 'fabulous'],
  ['sun-flower', 'interesting'],
];

onMounted(() => {
  words.value.forEach((word) => {
    let letters = word.textContent.split('');
    word.textContent = '';
    letters.forEach((letter) => {
      let span = document.createElement('span');
      span.textContent = letter;
      span.className = 'letter';
      word.append(span);
    });
  });

  let currentWordIndex = 0;
  let maxWordIndex = words.value.length - 1;
  words.value[currentWordIndex].style.opacity = '1';

  let rotateText = () => {
    let currentWord = words.value[currentWordIndex];
    let nextWord = currentWordIndex === maxWordIndex ? words.value[0] : words.value[currentWordIndex + 1];
    // rotate out letters of current word
    Array.from(currentWord.children).forEach((letter, i) => {
      setTimeout(() => {
        letter.className = 'letter out';
      }, i * 80);
    });
    // reveal and rotate in letters of next word
    nextWord.style.opacity = '1';
    Array.from(nextWord.children).forEach((letter, i) => {
      letter.className = 'letter behind';
      setTimeout(() => {
        letter.className = 'letter in';
      }, 340 + i * 80);
    });
    currentWordIndex = currentWordIndex === maxWordIndex ? 0 : currentWordIndex + 1;
  };

  rotateText();
  setInterval(rotateText, 4000);
});
</script>

<style scoped lang="scss">
.rotating-text {
  font-weight: 600;
  font-size: 36px;
  color: #444;

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

    &:first-child {
      margin-right: 10px;
    }

    .word {
      position: absolute;
      display: flex;
      opacity: 0;
    }
  }
}

// palette: https://flatuicolors.com/palette/defo
.alizarin {
  color: #e74c3c;
}

.wisteria {
  color: #8e44ad;
}

.peter-river {
  color: #3498db;
}

.emerald {
  color: #2ecc71;
}

.sun-flower {
  color: #f1c40f;
}
</style>

<style lang="scss">
.dark {
  .rotating-text {
    color: #ccc;
  }
}
.letter {
  transform-origin: center center 25px;

  &.out {
    transform: rotateX(90deg);
    transition: 0.32s cubic-bezier(0.6, 0, 0.7, 0.2);
  }
  &.in {
    transition: 0.38s ease;
  }
  &.behind {
    transform: rotateX(-90deg);
  }
}
</style>
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
Last Updated:
Contributors: leevare