tn-row-notice.vue 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301
  1. <template>
  2. <view
  3. v-if="show"
  4. class="tn-row-notice-class tn-row-notice"
  5. :class="[backgroundColorClass]"
  6. :style="[noticeStyle]"
  7. >
  8. <view class="tn-row-notice__wrap">
  9. <!-- 左图标 -->
  10. <view class="tn-row-notice__icon">
  11. <view
  12. v-if="leftIcon"
  13. class="tn-row-notice__icon--left"
  14. :class="[`tn-icon-${leftIconName}`,fontColorClass]"
  15. :style="[fontStyle('leftIcon')]"
  16. @tap="clickLeftIcon"></view>
  17. </view>
  18. <!-- 消息体 -->
  19. <view class="tn-row-notice__content-box" id="tn-row-notice__content-box">
  20. <view
  21. class="tn-row-notice__content"
  22. id="tn-row-notice__content"
  23. :style="{
  24. animationDuration: animationDuration,
  25. animationPlayState: animationPlayState
  26. }"
  27. >
  28. <text
  29. class="tn-row-notice__content--text"
  30. :class="[fontColorClass]"
  31. :style="[fontStyle()]"
  32. @tap="click"
  33. >{{ showText }}</text>
  34. </view>
  35. </view>
  36. <!-- 右图标 -->
  37. <view class="tn-row-notice__icon">
  38. <view
  39. v-if="rightIcon"
  40. class="tn-row-notice__icon--right"
  41. :class="[`tn-icon-${rightIconName}`,fontColorClass]"
  42. :style="[fontStyle('rightIcon')]"
  43. @tap="clickRightIcon"></view>
  44. <view
  45. v-if="closeBtn"
  46. class="tn-row-notice__icon--right"
  47. :class="[`tn-icon-close`,fontColorClass]"
  48. :style="[fontStyle('close')]"
  49. @tap="close"></view>
  50. </view>
  51. </view>
  52. </view>
  53. </template>
  54. <script>
  55. import componentsColorMixin from '../../libs/mixin/components_color.js'
  56. export default {
  57. name: 'tn-row-notice',
  58. mixins: [componentsColorMixin],
  59. props: {
  60. // 显示的内容
  61. list: {
  62. type: Array,
  63. default() {
  64. return []
  65. }
  66. },
  67. // 是否显示
  68. show: {
  69. type: Boolean,
  70. default: true
  71. },
  72. // 播放状态
  73. // play -> 播放 paused -> 暂停
  74. playStatus: {
  75. type: String,
  76. default: 'play'
  77. },
  78. // 是否显示左边图标
  79. leftIcon: {
  80. type: Boolean,
  81. default: true
  82. },
  83. // 左边图标的名称
  84. leftIconName: {
  85. type: String,
  86. default: 'sound'
  87. },
  88. // 左边图标的大小
  89. leftIconSize: {
  90. type: Number,
  91. default: 34
  92. },
  93. // 是否显示右边的图标
  94. rightIcon: {
  95. type: Boolean,
  96. default: false
  97. },
  98. // 右边图标的名称
  99. rightIconName: {
  100. type: String,
  101. default: 'right'
  102. },
  103. // 右边图标的大小
  104. rightIconSize: {
  105. type: Number,
  106. default: 26
  107. },
  108. // 是否显示关闭按钮
  109. closeBtn: {
  110. type: Boolean,
  111. default: false
  112. },
  113. // 圆角
  114. radius: {
  115. type: Number,
  116. default: 0
  117. },
  118. // 内边距
  119. padding: {
  120. type: String,
  121. default: '18rpx 24rpx'
  122. },
  123. // 自动播放
  124. autoplay: {
  125. type: Boolean,
  126. default: true
  127. },
  128. // 水平滚动时的速度,即每秒滚动多少rpx
  129. speed: {
  130. type: Number,
  131. default: 160
  132. }
  133. },
  134. computed: {
  135. fontStyle() {
  136. return (type) => {
  137. let style = {}
  138. style.color = this.fontColorStyle ? this.fontColorStyle : ''
  139. style.fontSize = this.fontSizeStyle ? this.fontSizeStyle : ''
  140. if (type === 'leftIcon' && this.leftIconSize) {
  141. style.fontSize = this.leftIconSize + 'rpx'
  142. }
  143. if (type === 'rightIcon' && this.rightIconSize) {
  144. style.fontSize = this.rightIconSize + 'rpx'
  145. }
  146. if (type === 'close') {
  147. style.fontSize = '24rpx'
  148. }
  149. return style
  150. }
  151. },
  152. noticeStyle() {
  153. let style = {}
  154. style.backgroundColor = this.backgroundColorStyle ? this.backgroundColorStyle : 'transparent'
  155. if (this.padding) style.padding = this.padding
  156. return style
  157. }
  158. },
  159. data() {
  160. return {
  161. // 滚动文字的宽度
  162. textWidth: 0,
  163. // 存放滚动文字的盒子的宽度
  164. textBoxWidth: 0,
  165. // 动画执行的时间
  166. animationDuration: '10s',
  167. // 动画执行状态
  168. animationPlayState: 'paused',
  169. // 当前显示的文本
  170. showText: ''
  171. }
  172. },
  173. watch: {
  174. list: {
  175. handler(value) {
  176. this.showText = value.join(',')
  177. this.$nextTick(() => {
  178. this.initNotice()
  179. })
  180. },
  181. immediate: true
  182. },
  183. playStatus(value) {
  184. if (value === 'play') this.animationPlayState = 'running'
  185. else this.animationPlayState = 'paused'
  186. },
  187. speed(value) {
  188. this.initNotice()
  189. }
  190. },
  191. methods: {
  192. // 初始化通知栏
  193. initNotice() {
  194. let query = [],
  195. textBoxWidth = 0,
  196. textWidth = 0;
  197. let textQuery = new Promise((resolve, reject) => {
  198. uni.createSelectorQuery()
  199. .in(this)
  200. .select(`#tn-row-notice__content`)
  201. .boundingClientRect()
  202. .exec(ret => {
  203. this.textWidth = ret[0].width
  204. resolve()
  205. })
  206. })
  207. query.push(textQuery)
  208. Promise.all(query).then(() => {
  209. // 根据t=s/v(时间=路程/速度),这里为何不需要加上#tn-row-notice__content-box的宽度,因为设置了.tn-row-notice__content样式中设置了padding-left: 100%
  210. this.animationDuration = `${this.textWidth / uni.upx2px(this.speed)}s`
  211. // 这里必须这样开始动画,否则在APP上动画速度不会改变(HX版本2.4.6,IOS13)
  212. this.animationPlayState = 'paused'
  213. setTimeout(() => {
  214. if (this.autoplay && this.playStatus === 'play') this.animationPlayState = 'running'
  215. }, 10)
  216. })
  217. },
  218. // 点击了通知栏
  219. click() {
  220. this.$emit('click')
  221. },
  222. // 点击了关闭按钮
  223. close() {
  224. this.$emit('close')
  225. },
  226. // 点击了左边图标
  227. clickLeftIcon() {
  228. this.$emit('clickLeft')
  229. },
  230. // 点击了右边图标
  231. clickRightIcon() {
  232. this.$emit('clickRight')
  233. }
  234. }
  235. }
  236. </script>
  237. <style lang="scss" scoped>
  238. .tn-row-notice {
  239. width: 100%;
  240. overflow: hidden;
  241. &__wrap {
  242. display: flex;
  243. flex-direction: row;
  244. align-items: center;
  245. justify-content: space-between;
  246. }
  247. &__content {
  248. &-box {
  249. flex: 1;
  250. display: flex;
  251. flex-direction: row;
  252. overflow: hidden;
  253. margin-left: 12rpx;
  254. }
  255. display: flex;
  256. flex-direction: row;
  257. flex-wrap: nowrap;
  258. text-align: right;
  259. // 为了能滚动起来
  260. padding-left: 100%;
  261. animation: tn-notice-loop-animation 10s linear infinite both;
  262. &--text {
  263. word-break: keep-all;
  264. white-space: nowrap;
  265. }
  266. }
  267. &__icon {
  268. &--left {
  269. display: inline-flex;
  270. align-items: center;
  271. }
  272. &--right {
  273. margin-left: 12rpx;
  274. display: inline-flex;
  275. align-items: center;
  276. }
  277. }
  278. }
  279. @keyframes tn-notice-loop-animation {
  280. 0% {
  281. transform: translateX(0);
  282. }
  283. 100% {
  284. transform: translateX(-100%);
  285. }
  286. }
  287. </style>