VciTextBounce.vue 1.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  1. <template>
  2. <div class="VciTextBounce">
  3. <span
  4. v-for="(t, index) in textArray"
  5. :key="t + index"
  6. :class="{'tb-up': indexCurrentBounce === index, 'tb-space': /\s/.test(t)}"
  7. class="tb"
  8. >
  9. {{ t }}
  10. </span>
  11. </div>
  12. </template>
  13. <script>
  14. import MixinClearScheduleTasks from "../../mixins/MixinClearScheduledTasks";
  15. export default {
  16. name: "VciTextBounce",
  17. mixins: [MixinClearScheduleTasks],
  18. props: {
  19. text: {
  20. type: String,
  21. default: "bounce"
  22. }
  23. },
  24. data() {
  25. return {
  26. indexCurrentBounce: -1,
  27. inter: -1
  28. };
  29. },
  30. computed: {
  31. textArray() {
  32. return (this.text + "...").split("");
  33. }
  34. },
  35. watch: {
  36. text: {
  37. handler() {
  38. this.startBounce();
  39. },
  40. immediate: true
  41. }
  42. },
  43. methods: {
  44. startBounce() {
  45. this.stopBounce();
  46. if(this.textArray.length > 2) {
  47. this.indexCurrentBounce = -1;
  48. this.inter = setInterval(() => {
  49. this.indexCurrentBounce++;
  50. if(this.indexCurrentBounce >= this.textArray.length) this.indexCurrentBounce = -1;
  51. }, 220);
  52. }
  53. },
  54. stopBounce() {
  55. clearInterval(this.inter);
  56. }
  57. }
  58. };
  59. </script>
  60. <style lang="scss" scoped>
  61. @use "@vci/style/src/var" as va;
  62. .VciTextBounce {
  63. white-space: nowrap;
  64. .tb {
  65. display: inline-block;
  66. transition: va.$transition;
  67. letter-spacing: .1em;
  68. &.tb-up {
  69. transform: translateY(-20%);
  70. }
  71. &.tb-space {
  72. padding: 0 2px;
  73. }
  74. }
  75. }
  76. </style>