roll-text.vue 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  1. <template>
  2. <view class="u-notice">
  3. <view class="u-notice__content">
  4. <view class="u-notice__content__text" ref="u-notice__content__text" :style="[animationStyle]">
  5. <view
  6. v-for="(item, index) in innerText"
  7. :key="index"
  8. :style="[textStyle]"
  9. >{{item}}</view>
  10. </view>
  11. </view>
  12. </view>
  13. </template>
  14. <script>
  15. import props from './props.js';
  16. export default {
  17. mixins: [props],
  18. computed: {
  19. animationStyle() {
  20. let style = {}
  21. style.animationDuration = this.animationDuration
  22. style.animationPlayState = this.animationPlayState
  23. return style
  24. },
  25. // 文字内容的样式
  26. textStyle() {
  27. let style = {}
  28. style.color = this.color
  29. style.fontSize = this.fontSize + "px"
  30. return style
  31. },
  32. innerText() {
  33. let result = [],
  34. // 每组text标签的字符长度
  35. len = 20
  36. const textArr = this.text.split('')
  37. for (let i = 0; i < textArr.length; i += len) {
  38. // 对拆分的后的text进行slice分割,得到的为数组再进行join拼接为字符串
  39. result.push(textArr.slice(i, i + len).join(''))
  40. }
  41. return result
  42. }
  43. },
  44. mounted() {
  45. // this.vue()
  46. },
  47. methods: {
  48. async vue() {
  49. let boxWidth = 0,
  50. textWidth = 0
  51. console.log("this", this);
  52. // 进行一定的延时
  53. await new Promise((resolve) => {
  54. setTimeout(() => {
  55. resolve()
  56. }, 30)
  57. })
  58. // 查询盒子和文字的宽度
  59. textWidth = (await this.$uGetRect('.u-notice__content__text')).width
  60. boxWidth = (await this.$uGetRect('.u-notice__content')).width
  61. console.log("textWidth", textWidth, "boxWidth", boxWidth);
  62. // 根据t=s/v(时间=路程/速度),这里为何不需要加上#u-notice-box的宽度,因为中设置了.u-notice-content样式中设置了padding-left: 100%
  63. // 恰巧计算出来的结果中已经包含了#u-notice-box的宽度
  64. this.animationDuration = `${textWidth / this.speed}s`
  65. console.log("this.$uGetRect('.u-notice__content__text')", await this.$uGetRect('.u-notice__content__text', true));
  66. // 这里必须这样开始动画,否则在APP上动画速度不会改变
  67. this.animationPlayState = 'paused'
  68. setTimeout(() => {
  69. this.animationPlayState = 'running'
  70. }, 10)
  71. }
  72. }
  73. }
  74. </script>
  75. <style lang="scss" scoped>
  76. .u-notice {
  77. display: flex;
  78. width: 460rpx;
  79. align-items: center;
  80. &__content {
  81. text-align: right;
  82. flex: 1;
  83. display: flex;
  84. flex-wrap: nowrap;
  85. overflow: hidden;
  86. &__text {
  87. font-size: 14px;
  88. padding-left: 100%;
  89. word-break: keep-all;
  90. white-space: nowrap;
  91. animation: u-loop-animation 5s linear infinite both;
  92. display: flex;
  93. flex-direction: row;
  94. }
  95. }
  96. }
  97. @keyframes u-loop-animation {
  98. 0% {
  99. transform: translate3d(-440rpx, 0, 0);
  100. }
  101. 100% {
  102. transform: translate3d(-60%, 0, 0);
  103. }
  104. }
  105. </style>