u-row-notice.vue 6.7 KB

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