u-row-notice.vue 9.4 KB

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