u-row-notice.vue 9.8 KB

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