u-row-notice.vue 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334
  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.js';
  56. import mpMixin from '../../libs/mixin/mpMixin.js';
  57. import mixin from '../../libs/mixin/mixin.js';
  58. // #ifdef APP-NVUE
  59. const animation = uni.requireNativePlugin('animation')
  60. const dom = uni.requireNativePlugin('dom')
  61. // #endif
  62. /**
  63. * RowNotice 滚动通知中的水平滚动模式
  64. * @description 水平滚动
  65. * @tutorial https://ijry.github.io/uview-plus/components/noticeBar.html
  66. * @property {String | Number} text 显示的内容,字符串
  67. * @property {String} icon 是否显示左侧的音量图标 (默认 'volume' )
  68. * @property {String} mode 通告模式,link-显示右箭头,closable-显示右侧关闭图标
  69. * @property {String} color 文字颜色,各图标也会使用文字颜色 (默认 '#f9ae3d' )
  70. * @property {String} bgColor 背景颜色 (默认 ''#fdf6ec' )
  71. * @property {String | Number} fontSize 字体大小,单位px (默认 14 )
  72. * @property {String | Number} speed 水平滚动时的滚动速度,即每秒滚动多少px(rpx),这有利于控制文字无论多少时,都能有一个恒定的速度 (默认 80 )
  73. *
  74. * @event {Function} click 点击通告文字触发
  75. * @event {Function} close 点击右侧关闭图标触发
  76. * @example
  77. */
  78. export default {
  79. name: 'u-row-notice',
  80. mixins: [mpMixin, mixin, props],
  81. data() {
  82. return {
  83. animationDuration: '0', // 动画执行时间
  84. animationPlayState: 'paused', // 动画的开始和结束执行
  85. // nvue下,内容发生变化,导致滚动宽度也变化,需要标志为是否需要重新计算宽度
  86. // 不能在内容变化时直接重新计算,因为nvue的animation模块上一次的滚动不是刚好结束,会有影响
  87. nvueInit: true,
  88. show: true
  89. };
  90. },
  91. watch: {
  92. text: {
  93. immediate: true,
  94. handler(newValue, oldValue) {
  95. // #ifdef APP-NVUE
  96. this.nvueInit = true
  97. // #endif
  98. // #ifndef APP-NVUE
  99. this.vue()
  100. // #endif
  101. if(!uni.$u.test.string(newValue)) {
  102. uni.$u.error('noticebar组件direction为row时,要求text参数为字符串形式')
  103. }
  104. }
  105. },
  106. fontSize() {
  107. // #ifdef APP-NVUE
  108. this.nvueInit = true
  109. // #endif
  110. // #ifndef APP-NVUE
  111. this.vue()
  112. // #endif
  113. },
  114. speed() {
  115. // #ifdef APP-NVUE
  116. this.nvueInit = true
  117. // #endif
  118. // #ifndef APP-NVUE
  119. this.vue()
  120. // #endif
  121. }
  122. },
  123. computed: {
  124. // 文字内容的样式
  125. textStyle() {
  126. let style = {}
  127. style.color = this.color
  128. style.fontSize = uni.$u.addUnit(this.fontSize)
  129. return style
  130. },
  131. animationStyle() {
  132. let style = {}
  133. style.animationDuration = this.animationDuration
  134. style.animationPlayState = this.animationPlayState
  135. return style
  136. },
  137. // 内部对用户传入的数据进一步分割,放到多个text标签循环,否则如果用户传入的字符串很长(100个字符以上)
  138. // 放在一个text标签中进行滚动,在低端安卓机上,动画可能会出现抖动现象,需要分割到多个text中可解决此问题
  139. innerText() {
  140. let result = [],
  141. // 每组text标签的字符长度
  142. len = 20
  143. const textArr = this.text.split('')
  144. for (let i = 0; i < textArr.length; i += len) {
  145. // 对拆分的后的text进行slice分割,得到的为数组再进行join拼接为字符串
  146. result.push(textArr.slice(i, i + len).join(''))
  147. }
  148. return result
  149. }
  150. },
  151. mounted() {
  152. // #ifdef APP-PLUS
  153. // 在APP上(含nvue),监听当前webview是否处于隐藏状态(进入下一页时即为hide状态)
  154. // 如果webivew隐藏了,为了节省性能的损耗,应停止动画的执行,同时也是为了保持进入下一页返回后,滚动位置保持不变
  155. var pages = getCurrentPages()
  156. var page = pages[pages.length - 1]
  157. var currentWebview = page.$getAppWebview()
  158. currentWebview.addEventListener('hide', () => {
  159. this.webviewHide = true
  160. })
  161. currentWebview.addEventListener('show', () => {
  162. this.webviewHide = false
  163. })
  164. // #endif
  165. this.init()
  166. },
  167. methods: {
  168. init() {
  169. // #ifdef APP-NVUE
  170. this.nvue()
  171. // #endif
  172. // #ifndef APP-NVUE
  173. this.vue()
  174. // #endif
  175. if(!uni.$u.test.string(this.text)) {
  176. uni.$u.error('noticebar组件direction为row时,要求text参数为字符串形式')
  177. }
  178. },
  179. // vue版处理
  180. async vue() {
  181. // #ifndef APP-NVUE
  182. let boxWidth = 0,
  183. textWidth = 0
  184. // 进行一定的延时
  185. await uni.$u.sleep()
  186. // 查询盒子和文字的宽度
  187. textWidth = (await this.$uGetRect('.u-notice__content__text')).width
  188. boxWidth = (await this.$uGetRect('.u-notice__content')).width
  189. // 根据t=s/v(时间=路程/速度),这里为何不需要加上#u-notice-box的宽度,因为中设置了.u-notice-content样式中设置了padding-left: 100%
  190. // 恰巧计算出来的结果中已经包含了#u-notice-box的宽度
  191. this.animationDuration = `${textWidth / uni.$u.getPx(this.speed)}s`
  192. // 这里必须这样开始动画,否则在APP上动画速度不会改变
  193. this.animationPlayState = 'paused'
  194. setTimeout(() => {
  195. this.animationPlayState = 'running'
  196. }, 10)
  197. // #endif
  198. },
  199. // nvue版处理
  200. async nvue() {
  201. // #ifdef APP-NVUE
  202. this.nvueInit = false
  203. let boxWidth = 0,
  204. textWidth = 0
  205. // 进行一定的延时
  206. await uni.$u.sleep()
  207. // 查询盒子和文字的宽度
  208. textWidth = (await this.getNvueRect('u-notice__content__text')).width
  209. boxWidth = (await this.getNvueRect('u-notice__content')).width
  210. // 将文字移动到盒子的右边沿,之所以需要这么做,是因为nvue不支持100%单位,否则可以通过css设置
  211. animation.transition(this.$refs['u-notice__content__text'], {
  212. styles: {
  213. transform: `translateX(${boxWidth}px)`
  214. },
  215. }, () => {
  216. // 如果非禁止动画,则开始滚动
  217. !this.stopAnimation && this.loopAnimation(textWidth, boxWidth)
  218. });
  219. // #endif
  220. },
  221. loopAnimation(textWidth, boxWidth) {
  222. // #ifdef APP-NVUE
  223. animation.transition(this.$refs['u-notice__content__text'], {
  224. styles: {
  225. // 目标移动终点为-textWidth,也即当文字的最右边贴到盒子的左边框的位置
  226. transform: `translateX(-${textWidth}px)`
  227. },
  228. // 滚动时间的计算为,时间 = 路程(boxWidth + textWidth) / 速度,最后转为毫秒
  229. duration: (boxWidth + textWidth) / uni.$u.getPx(this.speed) * 1000,
  230. delay: 10
  231. }, () => {
  232. animation.transition(this.$refs['u-notice__content__text'], {
  233. styles: {
  234. // 重新将文字移动到盒子的右边沿
  235. transform: `translateX(${this.stopAnimation ? 0 : boxWidth}px)`
  236. },
  237. }, () => {
  238. // 如果非禁止动画,则继续下一轮滚动
  239. if (!this.stopAnimation) {
  240. // 判断是否需要初始化计算尺寸
  241. if (this.nvueInit) {
  242. this.nvue()
  243. } else {
  244. this.loopAnimation(textWidth, boxWidth)
  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: 14px;
  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. /* #ifndef APP-NVUE */
  312. @keyframes u-loop-animation {
  313. 0% {
  314. transform: translate3d(0, 0, 0);
  315. }
  316. 100% {
  317. transform: translate3d(-100%, 0, 0);
  318. }
  319. }
  320. /* #endif */
  321. </style>