u-row-notice.vue 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311
  1. <template>
  2. <view class="u-notice" @tap="clickHandler">
  3. <slot name="icon">
  4. <view class="u-notice__left-icon" v-if="icon">
  5. <u-icon :name="icon" :color="color" size="19"></u-icon>
  6. </view>
  7. </slot>
  8. <view class="u-notice__content" ref="u-notice__content">
  9. <view ref="u-notice__content__text" class="u-notice__content__text" :style="[animationStyle]">
  10. <text
  11. v-for="(item, index) in text"
  12. :key="index"
  13. :style="[textStyle]"
  14. @click="(e) => location(item.link)"
  15. class="u-notice__content__text__item"
  16. >{{item.info}}</text>
  17. </view>
  18. </view>
  19. <view class="u-notice__right-icon" v-if="['link', 'closable'].includes(mode)">
  20. <u-icon v-if="mode === 'link'" name="arrow-right" :size="17" :color="color"></u-icon>
  21. <u-icon v-if="mode === 'closable'" @click="close" name="close" :size="16" :color="color"></u-icon>
  22. </view>
  23. </view>
  24. </template>
  25. <script>
  26. import props from './props.js';
  27. import mixin from '../../libs/mixin/mixin'
  28. import mpMixin from '../../libs/mixin/mpMixin';
  29. // #ifdef APP-NVUE
  30. const animation = uni.requireNativePlugin('animation')
  31. const dom = uni.requireNativePlugin('dom')
  32. // #endif
  33. /**
  34. * RowNotice 滚动通知中的水平滚动模式
  35. * @description 水平滚动
  36. * @tutorial https://uview.d3u.cn/components/noticeBar.html
  37. * @property {String | Number} text 显示的内容,字符串
  38. * @property {String} icon 是否显示左侧的音量图标 (默认 'volume' )
  39. * @property {String} mode 通告模式,link-显示右箭头,closable-显示右侧关闭图标
  40. * @property {String} color 文字颜色,各图标也会使用文字颜色 (默认 '#f9ae3d' )
  41. * @property {String} bgColor 背景颜色 (默认 ''#fdf6ec' )
  42. * @property {String | Number} fontSize 字体大小,单位px (默认 14 )
  43. * @property {String | Number} speed 水平滚动时的滚动速度,即每秒滚动多少px(rpx),这有利于控制文字无论多少时,都能有一个恒定的速度 (默认 80 )
  44. *
  45. * @event {Function} click 点击通告文字触发
  46. * @event {Function} close 点击右侧关闭图标触发
  47. * @example
  48. */
  49. export default {
  50. name: 'u-row-notice',
  51. mixins: [mpMixin, mixin, props],
  52. data() {
  53. return {
  54. animationDuration: '0', // 动画执行时间
  55. animationPlayState: 'paused', // 动画的开始和结束执行
  56. // nvue下,内容发生变化,导致滚动宽度也变化,需要标志为是否需要重新计算宽度
  57. // 不能在内容变化时直接重新计算,因为nvue的animation模块上一次的滚动不是刚好结束,会有影响
  58. nvueInit: true,
  59. show: true
  60. };
  61. },
  62. watch: {
  63. text: {
  64. immediate: true,
  65. handler(newValue, oldValue) {
  66. // #ifdef APP-NVUE
  67. this.nvueInit = true
  68. // #endif
  69. // #ifndef APP-NVUE
  70. this.vue()
  71. // #endif
  72. /*
  73. if (!uni.$u.test.string(newValue)) {
  74. uni.$u.error('noticebar组件direction为row时,要求text参数为字符串形式')
  75. }*/
  76. }
  77. },
  78. fontSize() {
  79. // #ifdef APP-NVUE
  80. this.nvueInit = true
  81. // #endif
  82. // #ifndef APP-NVUE
  83. this.vue()
  84. // #endif
  85. },
  86. speed() {
  87. // #ifdef APP-NVUE
  88. this.nvueInit = true
  89. // #endif
  90. // #ifndef APP-NVUE
  91. this.vue()
  92. // #endif
  93. }
  94. },
  95. computed: {
  96. // 文字内容的样式
  97. textStyle() {
  98. let style = {}
  99. style.color = this.color
  100. style.fontSize = uni.$u.addUnit(this.fontSize)
  101. return style
  102. },
  103. animationStyle() {
  104. let style = {}
  105. style.animationDuration = this.animationDuration
  106. style.animationPlayState = this.animationPlayState
  107. return style
  108. },
  109. // 内部对用户传入的数据进一步分割,放到多个text标签循环,否则如果用户传入的字符串很长(100个字符以上)
  110. // 放在一个text标签中进行滚动,在低端安卓机上,动画可能会出现抖动现象,需要分割到多个text中可解决此问题
  111. innerText() {
  112. let result = [],
  113. // 每组text标签的字符长度
  114. len = 20
  115. const textArr = this.text.split('')
  116. for (let i = 0; i < textArr.length; i += len) {
  117. // 对拆分的后的text进行slice分割,得到的为数组再进行join拼接为字符串
  118. result.push(textArr.slice(i, i + len).join(''))
  119. }
  120. return result
  121. }
  122. },
  123. mounted() {
  124. // #ifdef APP-PLUS
  125. // 在APP上(含nvue),监听当前webview是否处于隐藏状态(进入下一页时即为hide状态)
  126. // 如果webivew隐藏了,为了节省性能的损耗,应停止动画的执行,同时也是为了保持进入下一页返回后,滚动位置保持不变
  127. var pages = getCurrentPages()
  128. var page = pages[pages.length - 1]
  129. var currentWebview = page.$getAppWebview()
  130. currentWebview.addEventListener('hide', () => {
  131. this.webviewHide = true
  132. })
  133. currentWebview.addEventListener('show', () => {
  134. this.webviewHide = false
  135. })
  136. // #endif
  137. this.init()
  138. },
  139. // #ifdef VUE3
  140. emits: ["click", "close"],
  141. // #endif
  142. methods: {
  143. init() {
  144. // #ifdef APP-NVUE
  145. this.nvue()
  146. // #endif
  147. // #ifndef APP-NVUE
  148. this.vue()
  149. // #endif
  150. /*
  151. if (!uni.$u.test.string(this.text)) {
  152. uni.$u.error('noticebar组件direction为row时,要求text参数为字符串形式')
  153. }*/
  154. },
  155. // vue版处理
  156. async vue() {
  157. // #ifndef APP-NVUE
  158. let boxWidth = 0,
  159. textWidth = 0
  160. // 进行一定的延时
  161. await uni.$u.sleep()
  162. // 查询盒子和文字的宽度
  163. textWidth = (await this.$uGetRect('.u-notice__content__text')).width
  164. boxWidth = (await this.$uGetRect('.u-notice__content')).width
  165. // 根据t=s/v(时间=路程/速度),这里为何不需要加上#u-notice-box的宽度,因为中设置了.u-notice-content样式中设置了padding-left: 100%
  166. // 恰巧计算出来的结果中已经包含了#u-notice-box的宽度
  167. this.animationDuration = `${textWidth / uni.$u.getPx(this.speed)}s`
  168. // 这里必须这样开始动画,否则在APP上动画速度不会改变
  169. this.animationPlayState = 'paused'
  170. setTimeout(() => {
  171. this.animationPlayState = 'running'
  172. }, 10)
  173. // #endif
  174. },
  175. // nvue版处理
  176. async nvue() {
  177. // #ifdef APP-NVUE
  178. this.nvueInit = false
  179. let boxWidth = 0,
  180. textWidth = 0
  181. // 进行一定的延时
  182. await uni.$u.sleep()
  183. // 查询盒子和文字的宽度
  184. textWidth = (await this.getNvueRect('u-notice__content__text')).width
  185. boxWidth = (await this.getNvueRect('u-notice__content')).width
  186. // 将文字移动到盒子的右边沿,之所以需要这么做,是因为nvue不支持100%单位,否则可以通过css设置
  187. animation.transition(this.$refs['u-notice__content__text'], {
  188. styles: {
  189. transform: `translateX(${boxWidth}px)`
  190. },
  191. }, () => {
  192. // 如果非禁止动画,则开始滚动
  193. !this.stopAnimation && this.loopAnimation(textWidth, boxWidth)
  194. });
  195. // #endif
  196. },
  197. loopAnimation(textWidth, boxWidth) {
  198. // #ifdef APP-NVUE
  199. animation.transition(this.$refs['u-notice__content__text'], {
  200. styles: {
  201. // 目标移动终点为-textWidth,也即当文字的最右边贴到盒子的左边框的位置
  202. transform: `translateX(-${textWidth}px)`
  203. },
  204. // 滚动时间的计算为,时间 = 路程(boxWidth + textWidth) / 速度,最后转为毫秒
  205. duration: (boxWidth + textWidth) / uni.$u.getPx(this.speed) * 1000,
  206. delay: 10
  207. }, () => {
  208. animation.transition(this.$refs['u-notice__content__text'], {
  209. styles: {
  210. // 重新将文字移动到盒子的右边沿
  211. transform: `translateX(${this.stopAnimation ? 0 : boxWidth}px)`
  212. },
  213. }, () => {
  214. // 如果非禁止动画,则继续下一轮滚动
  215. if (!this.stopAnimation) {
  216. // 判断是否需要初始化计算尺寸
  217. if (this.nvueInit) {
  218. this.nvue()
  219. } else {
  220. this.loopAnimation(textWidth, boxWidth)
  221. }
  222. }
  223. });
  224. })
  225. // #endif
  226. },
  227. getNvueRect(el) {
  228. // #ifdef APP-NVUE
  229. // 返回一个promise
  230. return new Promise(resolve => {
  231. dom.getComponentRect(this.$refs[el], (res) => {
  232. resolve(res.size)
  233. })
  234. })
  235. // #endif
  236. },
  237. // 点击通告栏
  238. clickHandler(index) {
  239. this.$emit('click')
  240. },
  241. // 点击右侧按钮,需要判断点击的是关闭图标还是箭头图标
  242. close() {
  243. this.$emit('close')
  244. }
  245. },
  246. // #ifdef APP-NVUE && VUE2
  247. beforeDestroy() {
  248. this.stopAnimation = true
  249. },
  250. // #endif
  251. };
  252. </script>
  253. <style lang="scss" scoped>
  254. @import "../../libs/css/components.scss";
  255. .u-notice {
  256. @include flex;
  257. align-items: center;
  258. justify-content: space-between;
  259. &__left-icon {
  260. align-items: center;
  261. margin-right: 5px;
  262. }
  263. &__right-icon {
  264. margin-left: 5px;
  265. align-items: center;
  266. }
  267. &__content {
  268. text-align: right;
  269. flex: 1;
  270. @include flex;
  271. flex-wrap: nowrap;
  272. overflow: hidden;
  273. &__text {
  274. font-size: 14px;
  275. color: $u-warning;
  276. /* #ifndef APP-NVUE */
  277. // 这一句很重要,为了能让滚动左右连接起来
  278. padding-left: 100%;
  279. word-break: keep-all;
  280. white-space: nowrap;
  281. animation: u-loop-animation 10s linear infinite both;
  282. /* #endif */
  283. @include flex(row);
  284. }
  285. }
  286. .u-notice__content__text__item {
  287. margin-right:200rpx;
  288. }
  289. }
  290. /* #ifndef APP-NVUE */
  291. @keyframes u-loop-animation {
  292. 0% {
  293. transform: translate3d(0, 0, 0);
  294. }
  295. 100% {
  296. transform: translate3d(-100%, 0, 0);
  297. }
  298. }
  299. /* #endif */
  300. </style>