u-row-notice.vue 6.2 KB

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