Blame view

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