u-row-notice.vue 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279
  1. <template>
  2. <view class="u-notice-bar-wrap" :style="{
  3. borderRadius: borderRadius + 'rpx',
  4. }">
  5. <view
  6. v-if="show"
  7. class="u-notice-bar"
  8. :style="{
  9. background: computeBgColor,
  10. padding: padding
  11. }"
  12. :class="[
  13. type ? `u-type-${type}-light-bg` : ''
  14. ]"
  15. >
  16. <view class="u-direction-row">
  17. <view class="u-icon-wrap">
  18. <u-icon class="u-left-icon" v-if="volumeIcon" name="volume-fill" :size="volumeSize" :color="computeColor"></u-icon>
  19. </view>
  20. <view class="u-notice-box" id="u-notice-box">
  21. <view
  22. class="u-notice-content"
  23. id="u-notice-content"
  24. :style="{
  25. animationDuration: animationDuration,
  26. animationPlayState: animationPlayState,
  27. }"
  28. >
  29. <text class="u-notice-text" @tap="click" :style="[textStyle]"
  30. :class="['u-type-' + type]">{{showText}}</text>
  31. </view>
  32. </view>
  33. <view class="u-icon-wrap">
  34. <u-icon @click="getMore" class="u-down-icon" v-if="moreIcon" name="arrow-down" :size="26" :color="computeColor"></u-icon>
  35. <u-icon @click="close" class="u-right-icon" v-if="closeIcon" name="close" :size="24" :color="computeColor"></u-icon>
  36. </view>
  37. </view>
  38. </view>
  39. </view>
  40. </template>
  41. <script>
  42. export default {
  43. props: {
  44. // 滚动通知设置圆角
  45. borderRadius: {
  46. type: [Number, String],
  47. default: 0
  48. },
  49. // 显示的内容,数组
  50. list: {
  51. type: Array,
  52. default() {
  53. return [];
  54. }
  55. },
  56. // 显示的主题,success|error|primary|info|warning|none
  57. // none主题默认为透明背景,黑色(contentColor)字体
  58. type: {
  59. type: String,
  60. default: 'warning'
  61. },
  62. // 是否显示左侧的音量图标
  63. volumeIcon: {
  64. type: Boolean,
  65. default: true
  66. },
  67. // 是否显示右侧的右箭头图标
  68. moreIcon: {
  69. type: Boolean,
  70. default: false
  71. },
  72. // 是否显示右侧的关闭图标
  73. closeIcon: {
  74. type: Boolean,
  75. default: false
  76. },
  77. // 是否自动播放
  78. autoplay: {
  79. type: Boolean,
  80. default: true
  81. },
  82. // 文字颜色,各图标也会使用文字颜色
  83. color: {
  84. type: String,
  85. default: ''
  86. },
  87. // 背景颜色
  88. bgColor: {
  89. type: String,
  90. default: ''
  91. },
  92. // 是否显示
  93. show: {
  94. type: Boolean,
  95. default: true
  96. },
  97. // 字体大小,单位rpx
  98. fontSize: {
  99. type: [Number, String],
  100. default: 26
  101. },
  102. // 音量喇叭的大小
  103. volumeSize: {
  104. type: [Number, String],
  105. default: 34
  106. },
  107. // 水平滚动时的滚动速度,即每秒滚动多少rpx,这有利于控制文字无论多少时,都能有一个恒定的速度
  108. speed: {
  109. type: [Number, String],
  110. default: 160
  111. },
  112. // 播放状态,play-播放,paused-暂停
  113. playState: {
  114. type: String,
  115. default: 'play'
  116. },
  117. // 通知的边距
  118. padding: {
  119. type: [Number, String],
  120. default: '18rpx 24rpx'
  121. }
  122. },
  123. data() {
  124. return {
  125. textWidth: 0, // 滚动的文字宽度
  126. boxWidth: 0, // 供文字滚动的父盒子的宽度,和前者一起为了计算滚动速度
  127. animationDuration: '10s', // 动画执行时间
  128. animationPlayState: 'paused', // 动画的开始和结束执行
  129. showText: '' // 显示的文本
  130. };
  131. },
  132. watch: {
  133. list: {
  134. immediate: true,
  135. handler(val) {
  136. this.showText = val.join(',');
  137. this.$nextTick(() => {
  138. this.initSize();
  139. });
  140. }
  141. },
  142. playState(val) {
  143. if(val == 'play') this.animationPlayState = 'running';
  144. else this.animationPlayState = 'paused';
  145. },
  146. speed(val) {
  147. this.initSize();
  148. }
  149. },
  150. computed: {
  151. // 计算字体颜色,如果没有自定义的,就用uview主题颜色
  152. computeColor() {
  153. if (this.color) return this.color;
  154. // 如果是无主题,就默认使用content-color
  155. else if(this.type == 'none') return '#606266';
  156. else return this.type;
  157. },
  158. // 文字内容的样式
  159. textStyle() {
  160. let style = {};
  161. if (this.color) style.color = this.color;
  162. else if(this.type == 'none') style.color = '#606266';
  163. style.fontSize = this.fontSize + 'rpx';
  164. return style;
  165. },
  166. // 计算背景颜色
  167. computeBgColor() {
  168. if (this.bgColor) return this.bgColor;
  169. else if(this.type == 'none') return 'transparent';
  170. }
  171. },
  172. mounted() {
  173. this.$nextTick(() => {
  174. this.initSize();
  175. });
  176. },
  177. methods: {
  178. initSize() {
  179. let query = [],
  180. boxWidth = 0,
  181. textWidth = 0;
  182. let textQuery = new Promise((resolve, reject) => {
  183. uni.createSelectorQuery()
  184. .in(this)
  185. .select(`#u-notice-content`)
  186. .boundingClientRect()
  187. .exec(ret => {
  188. this.textWidth = ret[0].width;
  189. resolve();
  190. });
  191. });
  192. query.push(textQuery);
  193. Promise.all(query).then(() => {
  194. // 根据t=s/v(时间=路程/速度),这里为何不需要加上#u-notice-box的宽度,因为中设置了.u-notice-content样式中设置了padding-left: 100%
  195. // 恰巧计算出来的结果中已经包含了#u-notice-box的宽度
  196. this.animationDuration = `${this.textWidth / uni.upx2px(this.speed)}s`;
  197. // 这里必须这样开始动画,否则在APP上动画速度不会改变(HX版本2.4.6,IOS13)
  198. this.animationPlayState = 'paused';
  199. setTimeout(() => {
  200. if(this.playState == 'play' && this.autoplay) this.animationPlayState = 'running';
  201. }, 10);
  202. });
  203. },
  204. // 点击通告栏
  205. click(index) {
  206. this.$emit('click');
  207. },
  208. // 点击关闭按钮
  209. close() {
  210. this.$emit('close');
  211. },
  212. // 点击更多箭头按钮
  213. getMore() {
  214. this.$emit('getMore');
  215. }
  216. }
  217. };
  218. </script>
  219. <style lang="scss" scoped>
  220. .u-notice-bar {
  221. padding: 18rpx 24rpx;
  222. overflow: hidden;
  223. }
  224. .u-direction-row {
  225. display: flex;
  226. align-items: center;
  227. justify-content: space-between;
  228. }
  229. .u-left-icon {
  230. /* #ifndef APP-NVUE */
  231. display: inline-flex;
  232. /* #endif */
  233. align-items: center;
  234. }
  235. .u-notice-box {
  236. flex: 1;
  237. display: flex;
  238. overflow: hidden;
  239. margin-left: 12rpx;
  240. }
  241. .u-right-icon {
  242. margin-left: 12rpx;
  243. display: inline-flex;
  244. align-items: center;
  245. }
  246. .u-notice-content {
  247. animation: u-loop-animation 10s linear infinite both;
  248. text-align: right;
  249. // 这一句很重要,为了能让滚动左右连接起来
  250. padding-left: 100%;
  251. display: flex;
  252. flex-wrap: nowrap;
  253. }
  254. .u-notice-text {
  255. font-size: 26rpx;
  256. word-break: keep-all;
  257. white-space: nowrap
  258. }
  259. @keyframes u-loop-animation {
  260. 0% {
  261. transform: translate3d(0, 0, 0);
  262. }
  263. 100% {
  264. transform: translate3d(-100%, 0, 0);
  265. }
  266. }
  267. </style>