u-row-notice.vue 6.0 KB

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