index.vue 8.0 KB

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