u-row-notice.vue 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342
  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.color = this.color
  130. style.fontSize = addUnit(this.fontSize)
  131. return style
  132. },
  133. animationStyle() {
  134. let style = {}
  135. style.animationDuration = this.animationDuration
  136. style.animationPlayState = this.animationPlayState
  137. return style
  138. },
  139. // 内部对用户传入的数据进一步分割,放到多个text标签循环,否则如果用户传入的字符串很长(100个字符以上)
  140. // 放在一个text标签中进行滚动,在低端安卓机上,动画可能会出现抖动现象,需要分割到多个text中可解决此问题
  141. innerText() {
  142. let result = [],
  143. // 每组text标签的字符长度
  144. len = 20
  145. const textArr = this.text.split('')
  146. for (let i = 0; i < textArr.length; i += len) {
  147. // 对拆分的后的text进行slice分割,得到的为数组再进行join拼接为字符串
  148. result.push(textArr.slice(i, i + len).join(''))
  149. }
  150. return result
  151. }
  152. },
  153. mounted() {
  154. // #ifdef APP-PLUS
  155. // 在APP上(含nvue),监听当前webview是否处于隐藏状态(进入下一页时即为hide状态)
  156. // 如果webivew隐藏了,为了节省性能的损耗,应停止动画的执行,同时也是为了保持进入下一页返回后,滚动位置保持不变
  157. var pages = getCurrentPages()
  158. var page = pages[pages.length - 1]
  159. var currentWebview = page.$getAppWebview()
  160. currentWebview.addEventListener('hide', () => {
  161. this.webviewHide = true
  162. })
  163. currentWebview.addEventListener('show', () => {
  164. this.webviewHide = false
  165. })
  166. // #endif
  167. this.init()
  168. },
  169. emits: ["click", "close"],
  170. methods: {
  171. init() {
  172. // #ifdef APP-NVUE
  173. this.nvue()
  174. // #endif
  175. // #ifndef APP-NVUE
  176. this.vue()
  177. // #endif
  178. if(!test.string(this.text)) {
  179. error('noticebar组件direction为row时,要求text参数为字符串形式')
  180. }
  181. },
  182. // vue版处理
  183. async vue() {
  184. // #ifndef APP-NVUE
  185. let boxWidth = 0,
  186. textWidth = 0
  187. // 进行一定的延时
  188. await sleep()
  189. // 查询盒子和文字的宽度
  190. textWidth = (await this.$uGetRect('.u-notice__content__text')).width
  191. boxWidth = (await this.$uGetRect('.u-notice__content')).width
  192. // 根据t=s/v(时间=路程/速度),这里为何不需要加上#u-notice-box的宽度,因为中设置了.u-notice-content样式中设置了padding-left: 100%
  193. // 恰巧计算出来的结果中已经包含了#u-notice-box的宽度
  194. this.animationDuration = `${textWidth / getPx(this.speed)}s`
  195. // 这里必须这样开始动画,否则在APP上动画速度不会改变
  196. this.animationPlayState = 'paused'
  197. setTimeout(() => {
  198. this.animationPlayState = 'running'
  199. }, 10)
  200. // #endif
  201. },
  202. // nvue版处理
  203. async nvue() {
  204. // #ifdef APP-NVUE
  205. this.nvueInit = false
  206. let boxWidth = 0,
  207. textWidth = 0
  208. // 进行一定的延时
  209. await sleep()
  210. // 查询盒子和文字的宽度
  211. textWidth = (await this.getNvueRect('u-notice__content__text')).width
  212. boxWidth = (await this.getNvueRect('u-notice__content')).width
  213. // 将文字移动到盒子的右边沿,之所以需要这么做,是因为nvue不支持100%单位,否则可以通过css设置
  214. animation.transition(this.$refs['u-notice__content__text'], {
  215. styles: {
  216. transform: `translateX(${boxWidth}px)`
  217. },
  218. }, () => {
  219. // 如果非禁止动画,则开始滚动
  220. !this.stopAnimation && this.loopAnimation(textWidth, boxWidth)
  221. });
  222. // #endif
  223. },
  224. loopAnimation(textWidth, boxWidth) {
  225. // #ifdef APP-NVUE
  226. animation.transition(this.$refs['u-notice__content__text'], {
  227. styles: {
  228. // 目标移动终点为-textWidth,也即当文字的最右边贴到盒子的左边框的位置
  229. transform: `translateX(-${textWidth}px)`
  230. },
  231. // 滚动时间的计算为,时间 = 路程(boxWidth + textWidth) / 速度,最后转为毫秒
  232. duration: (boxWidth + textWidth) / getPx(this.speed) * 1000,
  233. delay: 10
  234. }, () => {
  235. animation.transition(this.$refs['u-notice__content__text'], {
  236. styles: {
  237. // 重新将文字移动到盒子的右边沿
  238. transform: `translateX(${this.stopAnimation ? 0 : boxWidth}px)`
  239. },
  240. }, () => {
  241. // 如果非禁止动画,则继续下一轮滚动
  242. if (!this.stopAnimation) {
  243. // 判断是否需要初始化计算尺寸
  244. if (this.nvueInit) {
  245. this.nvue()
  246. } else {
  247. this.loopAnimation(textWidth, boxWidth)
  248. }
  249. }
  250. });
  251. })
  252. // #endif
  253. },
  254. getNvueRect(el) {
  255. // #ifdef APP-NVUE
  256. // 返回一个promise
  257. return new Promise(resolve => {
  258. dom.getComponentRect(this.$refs[el], (res) => {
  259. resolve(res.size)
  260. })
  261. })
  262. // #endif
  263. },
  264. // 点击通告栏
  265. clickHandler(index) {
  266. this.$emit('click')
  267. },
  268. // 点击右侧按钮,需要判断点击的是关闭图标还是箭头图标
  269. close() {
  270. this.$emit('close')
  271. }
  272. },
  273. // #ifdef APP-NVUE
  274. // #ifdef VUE2
  275. beforeDestroy() {
  276. // #endif
  277. // #ifdef VUE3
  278. beforeUnmount() {
  279. // #endif
  280. this.stopAnimation = true
  281. },
  282. // #endif
  283. };
  284. </script>
  285. <style lang="scss" scoped>
  286. @import "../../libs/css/components.scss";
  287. .u-notice {
  288. @include flex;
  289. align-items: center;
  290. justify-content: space-between;
  291. &__left-icon {
  292. align-items: center;
  293. margin-right: 5px;
  294. }
  295. &__right-icon {
  296. margin-left: 5px;
  297. align-items: center;
  298. }
  299. &__content {
  300. text-align: right;
  301. flex: 1;
  302. @include flex;
  303. flex-wrap: nowrap;
  304. overflow: hidden;
  305. &__text {
  306. font-size: 14px;
  307. color: $u-warning;
  308. /* #ifndef APP-NVUE */
  309. // 这一句很重要,为了能让滚动左右连接起来
  310. padding-left: 100%;
  311. word-break: keep-all;
  312. white-space: nowrap;
  313. animation: u-loop-animation 10s linear infinite both;
  314. /* #endif */
  315. @include flex(row);
  316. }
  317. }
  318. }
  319. /* #ifndef APP-NVUE */
  320. @keyframes u-loop-animation {
  321. 0% {
  322. transform: translate3d(0, 0, 0);
  323. }
  324. 100% {
  325. transform: translate3d(-100%, 0, 0);
  326. }
  327. }
  328. /* #endif */
  329. </style>