textScroll.vue 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  1. <template>
  2. <div class="tip">
  3. <div class="inner" :class="{'move': scroll}" :style="styleName">
  4. <text class="tip-inder">{{text}} {{scroll ? text : '' }}</text>
  5. </div>
  6. </div>
  7. </template>
  8. <script>
  9. export default {
  10. props: {
  11. text: {
  12. type: String,
  13. defualt: ''
  14. },
  15. },
  16. data() {
  17. return {
  18. styleName: "animation-duration: 6s",
  19. scroll: false,
  20. tipWidth: 0
  21. };
  22. },
  23. watch: {
  24. text: {
  25. handler(val) {
  26. this.textScroll()
  27. },
  28. immediate: false
  29. }
  30. },
  31. methods: {
  32. textScroll() {
  33. // 等待节点挂载后再执行,热门线路tip滚动实现
  34. this.$nextTick(() => {
  35. console.log('滚动')
  36. let query = uni.createSelectorQuery().in(this)
  37. query.select('.tip').boundingClientRect(data => {
  38. this.tipWidth = data.width
  39. console.log('tip', this.tipWidth)
  40. }).exec();
  41. query.select('.tip-inder').boundingClientRect(data => {
  42. console.log('tip-inder', data.width)
  43. if(data.width > this.tipWidth) {
  44. let w = Number(data.width)
  45. let time = Math.round(w / 40)
  46. this.styleName = `animation-duration: ${time}s`
  47. this.scroll = true
  48. }
  49. }).exec();
  50. })
  51. }
  52. }
  53. };
  54. </script>
  55. <style lang="less">
  56. .tip {
  57. width: 100%;
  58. background: transparent;
  59. color: #4d82ff;
  60. font-size: 28rpx;
  61. height: 80rpx;
  62. line-height: 80rpx;
  63. overflow: hidden;
  64. box-sizing: border-box;
  65. white-space: nowrap;
  66. }
  67. .tip .inner {
  68. overflow: hidden;
  69. display: inline-block;
  70. }
  71. .tip .inner .tip-inder {
  72. white-space: nowrap;
  73. }
  74. .tip .inner.move {
  75. // animation: move 6s infinite linear;
  76. animation-name: scroll;
  77. animation-timing-function: linear;
  78. animation-iteration-count: infinite;
  79. }
  80. @keyframes scroll {
  81. 0% {
  82. transform: translateX(0);
  83. }
  84. 100% {
  85. transform: translateX(-50%);
  86. }
  87. }
  88. </style>