Skip to content

Commit 7538c97

Browse files
committed
popup组件新增width和height参数,如果内容超出容易,自动垂直滚动
1 parent d5dae6c commit 7538c97

File tree

2 files changed

+36
-12
lines changed

2 files changed

+36
-12
lines changed

pages.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
// "current": 0, //当前激活的模式(list 的索引项)
77
// "list": [{
88
// "name": "test", //模式名称
9-
// "path": "pages/componentsA/select/index", //启动页面,必选
9+
// "path": "pages/componentsC/popup/index", //启动页面,必选
1010
// "query": "id=1&name=2" //启动参数,在页面的onLoad函数里面得到
1111
// }]
1212
// },

uview-ui/components/u-popup/u-popup.vue

Lines changed: 35 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -24,9 +24,13 @@
2424
:color="closeIconColor"
2525
:size="closeIconSize"
2626
></u-icon>
27-
<slot />
27+
<scroll-view class="u-drawer__scroll-view" scroll-y="true">
28+
<slot />
29+
</scroll-view>
2830
</view>
29-
<block v-else><slot /></block>
31+
<scroll-view class="u-drawer__scroll-view" scroll-y="true" v-else>
32+
<slot />
33+
</scroll-view>
3034
<view class="u-close" :class="['u-close--' + closeIconPos]">
3135
<u-icon
3236
v-if="mode != 'center' && closeable"
@@ -158,6 +162,18 @@ export default {
158162
closeIconSize: {
159163
type: [String, Number],
160164
default: '30'
165+
},
166+
// 宽度,只对左,右,中部弹出时起作用,单位rpx,或者"auto"
167+
// 或者百分比"50%",表示由内容撑开高度或者宽度,优先级高于length参数
168+
width: {
169+
type: String,
170+
default: ''
171+
},
172+
// 高度,只对上,下,中部弹出时起作用,单位rpx,或者"auto"
173+
// 或者百分比"50%",表示由内容撑开高度或者宽度,优先级高于length参数
174+
height: {
175+
type: String,
176+
default: ''
161177
}
162178
},
163179
data() {
@@ -172,20 +188,17 @@ export default {
172188
// 根据mode的位置,设定其弹窗的宽度(mode = left|right),或者高度(mode = top|bottom)
173189
style() {
174190
let style = {};
175-
let translate = '100%';
176-
// 判断是否是否百分比或者auto值,是的话,直接使用该值,否则默认为rpx单位的数值
177-
let length = /%$/.test(this.length) || this.length == 'auto' ? this.length : uni.upx2px(this.length) + 'px';
178191
// 如果是左边或者上边弹出时,需要给translate设置为负值,用于隐藏
179192
if (this.mode == 'left' || this.mode == 'right') {
180193
style = {
181-
width: length,
194+
width: this.width ? this.getUnitValue(this.width) : this.getUnitValue(this.length),
182195
height: '100%',
183196
transform: `translate3D(${this.mode == 'left' ? '-100%' : '100%'},0px,0px)`
184197
};
185198
} else if (this.mode == 'top' || this.mode == 'bottom') {
186199
style = {
187200
width: '100%',
188-
height: length,
201+
height: this.height ? this.getUnitValue(this.height) : this.getUnitValue(this.length),
189202
transform: `translate3D(0px,${this.mode == 'top' ? '-100%' : '100%'},0px)`
190203
};
191204
}
@@ -215,16 +228,17 @@ export default {
215228
// 中部弹窗的特有样式
216229
centerStyle() {
217230
let style = {};
218-
let length = /%$/.test(this.length) || this.length == 'auto' ? this.length : uni.upx2px(this.length) + 'px';
219-
style.width = length;
231+
style.width = this.width ? this.getUnitValue(this.width) : this.getUnitValue(this.length);
232+
// 中部弹出的模式,如果没有设置高度,就用auto值,由内容撑开高度
233+
style.height = this.height ? this.getUnitValue(this.height) : 'auto';
220234
style.zIndex = this.zIndex ? this.zIndex : this.$u.zIndex.popup;
221235
if (this.borderRadius) {
222236
style.borderRadius = `${this.borderRadius}rpx`;
223237
// 不加可能圆角无效
224238
style.overflow = 'hidden';
225239
}
226240
return style;
227-
}
241+
},
228242
},
229243
watch: {
230244
value(val) {
@@ -236,6 +250,11 @@ export default {
236250
}
237251
},
238252
methods: {
253+
// 判断传入的值,是否带有单位,如果没有,就默认用rpx单位
254+
getUnitValue(val) {
255+
if(/(%|px|rpx|auto)$/.test(val)) return val;
256+
else return val + 'rpx'
257+
},
239258
// 遮罩被点击
240259
maskClick() {
241260
this.close();
@@ -307,6 +326,11 @@ export default {
307326
transition: all 0.3s linear;
308327
}
309328
329+
.u-drawer__scroll-view {
330+
width: 100%;
331+
height: 100%;
332+
}
333+
310334
.u-drawer-left {
311335
top: 0;
312336
bottom: 0;
@@ -375,7 +399,7 @@ export default {
375399
376400
.u-close {
377401
position: absolute;
378-
z-index: 1;
402+
z-index: 3;
379403
}
380404
381405
.u-close--top-left {

0 commit comments

Comments
 (0)