You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

344 lines
9.4 KiB

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