u-row-notice.vue 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307
  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. <text
  23. ref="u-notice__content__text"
  24. class="u-notice__content__text"
  25. :style="[textStyle]"
  26. >{{ text }}
  27. </text>
  28. </view>
  29. <view
  30. class="u-notice__right-icon"
  31. v-if="['link', 'closable'].includes(mode)"
  32. >
  33. <u-icon
  34. v-if="mode === 'link'"
  35. name="arrow-right"
  36. :size="17"
  37. :color="color"
  38. ></u-icon>
  39. <u-icon
  40. v-if="mode === 'closable'"
  41. @click="close"
  42. name="close"
  43. :size="16"
  44. :color="color"
  45. ></u-icon>
  46. </view>
  47. </view>
  48. </template>
  49. <script>
  50. import props from './props.js';
  51. // #ifdef APP-NVUE
  52. const animation = uni.requireNativePlugin('animation')
  53. const dom = uni.requireNativePlugin('dom')
  54. // #endif
  55. /**
  56. * RowNotice 滚动通知中的水平滚动模式
  57. * @description 水平滚动
  58. * @tutorial https://www.uviewui.com/components/noticeBar.html
  59. * @property {String | Number} text 显示的内容,字符串
  60. * @property {String} icon 是否显示左侧的音量图标 (默认 'volume' )
  61. * @property {String} mode 通告模式,link-显示右箭头,closable-显示右侧关闭图标
  62. * @property {String} color 文字颜色,各图标也会使用文字颜色 (默认 '#f9ae3d' )
  63. * @property {String} bgColor 背景颜色 (默认 ''#fdf6ec' )
  64. * @property {String | Number} fontSize 字体大小,单位px (默认 14 )
  65. * @property {String | Number} speed 水平滚动时的滚动速度,即每秒滚动多少px(rpx),这有利于控制文字无论多少时,都能有一个恒定的速度 (默认 80 )
  66. *
  67. * @event {Function} click 点击通告文字触发
  68. * @event {Function} close 点击右侧关闭图标触发
  69. * @example
  70. */
  71. export default {
  72. name: 'u-row-notice',
  73. mixins: [uni.$u.mpMixin, uni.$u.mixin, props],
  74. data() {
  75. return {
  76. animationDuration: '0', // 动画执行时间
  77. animationPlayState: 'paused', // 动画的开始和结束执行
  78. // nvue下,内容发生变化,导致滚动宽度也变化,需要标志为是否需要重新计算宽度
  79. // 不能在内容变化时直接重新计算,因为nvue的animation模块上一次的滚动不是刚好结束,会有影响
  80. nvueInit: true,
  81. show: true
  82. };
  83. },
  84. watch: {
  85. text: {
  86. immediate: true,
  87. handler(newValue, oldValue) {
  88. // #ifdef APP-NVUE
  89. this.nvueInit = true
  90. // #endif
  91. // #ifndef APP-NVUE
  92. this.vue()
  93. // #endif
  94. if (!uni.$u.test.string(newValue)) {
  95. uni.$u.error('noticebar组件direction为row时,要求text参数为字符串形式')
  96. }
  97. }
  98. },
  99. fontSize() {
  100. t // #ifdef APP-NVUE
  101. this.nvueInit = true
  102. // #endif
  103. // #ifndef APP-NVUE
  104. this.vue()
  105. // #endif
  106. },
  107. speed() {
  108. // #ifdef APP-NVUE
  109. this.nvueInit = true
  110. // #endif
  111. // #ifndef APP-NVUE
  112. this.vue()
  113. // #endif
  114. }
  115. },
  116. computed: {
  117. // 文字内容的样式
  118. textStyle() {
  119. let style = {}
  120. style.color = this.color
  121. style.animationDuration = this.animationDuration
  122. style.animationPlayState = this.animationPlayState
  123. style.fontSize = uni.$u.addUnit(this.fontSize)
  124. return style
  125. },
  126. },
  127. mounted() {
  128. // #ifdef APP-PLUS
  129. // 在APP上(含nvue),监听当前webview是否处于隐藏状态(进入下一页时即为hide状态)
  130. // 如果webivew隐藏了,为了节省性能的损耗,应停止动画的执行,同时也是为了保持进入下一页返回后,滚动位置保持不变
  131. var pages = getCurrentPages()
  132. var page = pages[pages.length - 1]
  133. var currentWebview = page.$getAppWebview()
  134. currentWebview.addEventListener('hide', () => {
  135. this.webviewHide = true
  136. })
  137. currentWebview.addEventListener('show', () => {
  138. this.webviewHide = false
  139. })
  140. // #endif
  141. this.init()
  142. },
  143. methods: {
  144. init() {
  145. // #ifdef APP-NVUE
  146. this.nvue()
  147. // #endif
  148. // #ifndef APP-NVUE
  149. this.vue()
  150. // #endif
  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
  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. }
  284. }
  285. }
  286. @keyframes u-loop-animation {
  287. 0% {
  288. transform: translate3d(0, 0, 0);
  289. }
  290. 100% {
  291. transform: translate3d(-100%, 0, 0);
  292. }
  293. }
  294. </style>