u-row-notice.vue 6.6 KB

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