Skip to content

Commit 48342a8

Browse files
committed
feat(Album): 新增自定义图片显示数量及状态
close #925,#962
1 parent 88b9d2c commit 48342a8

File tree

6 files changed

+176
-42
lines changed

6 files changed

+176
-42
lines changed

examples/pages/components/layout/pages/album/index.wxml

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@
1818
<l-avatar class="avatar" size='80' shape="square" src="{{avatarUrl}}" />
1919
<view class="right">
2020
<view class="name">LinUI@27315</view>
21-
<l-album urls='{{urls5}}'></l-album>
21+
<l-album max-number='{{4}}' urls='{{urls5}}' custom-row-number='{{false}}' preview-full-image='{{true}}'></l-album>
2222
</view>
2323
</view>
2424
</view>
@@ -33,7 +33,7 @@
3333
</view>
3434
</view>
3535
</view>
36-
<view>
36+
<view style="margin-top:20rpx">
3737
<view class="album">
3838
<l-avatar class="avatar" size='80' shape="square" src="{{avatarUrl}}" />
3939
<view class="right">
@@ -60,7 +60,7 @@
6060
<l-avatar class="avatar" size='80' shape="square" src="{{avatarUrl}}" />
6161
<view class="right">
6262
<view class="name">LinUI@27315</view>
63-
<l-album urls='{{urls4}}' key='newurl' gapRow='{{25}}' gapColumn='{{25}}'></l-album>
63+
<l-album urls='{{urls4}}' key='newurl' gap-row='{{25}}' gap-column='{{25}}' every-row-number="{{3}}"></l-album>
6464
</view>
6565
</view>
6666
</view>
Lines changed: 34 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -1,49 +1,57 @@
11
/* pages/components/layout/pages/album/index.wxss */
22
.num {
3-
color: #fc6517;
4-
font-size: 42rpx;
5-
font-style: italic;
3+
color: #fc6517;
4+
font-size: 42rpx;
5+
font-style: italic;
66
}
77

88
.text {
9-
height: 30rpx;
10-
font-size: 22rpx;
11-
font-family: PingFangSC-Regular;
12-
font-weight: 400;
13-
color: rgba(102, 102, 102, 1);
14-
line-height: 30rpx;
15-
margin-top: 9rpx;
9+
height: 30rpx;
10+
font-size: 22rpx;
11+
font-family: PingFangSC-Regular;
12+
font-weight: 400;
13+
color: rgba(102, 102, 102, 1);
14+
line-height: 30rpx;
15+
margin-top: 9rpx;
1616
}
1717

1818
.album {
19-
display: flex;
20-
flex-direction: row;
19+
display: flex;
20+
flex-direction: row;
2121
}
2222

2323
.avatar {
24-
margin-left: 20rpx;
24+
margin-left: 20rpx;
2525
}
2626

2727
.right {
28-
margin-left: 20rpx;
28+
margin-left: 20rpx;
2929
}
3030

3131
.name {
32-
color: rgb(91, 108, 160);
33-
font-weight: 450;
32+
color: rgb(91, 108, 160);
33+
font-weight: 450;
34+
margin-bottom: 20rpx;
3435
}
3536

3637
.content {
37-
margin-top: 7rpx;
38-
margin-right: 10rpx;
39-
color: black;
40-
font-weight: 445;
41-
margin-bottom: 15rpx;
38+
margin-top: 7rpx;
39+
margin-right: 10rpx;
40+
color: black;
41+
font-weight: 445;
42+
margin-bottom: 15rpx;
4243
}
4344

4445
.line {
45-
height: 1px;
46-
border-top: solid Silver 1px;
47-
/* margin-bottom: 40rpx; */
48-
margin-top: 30rpx;
49-
}
46+
height: 1px;
47+
border-top: solid Silver 1px;
48+
/* margin-bottom: 40rpx; */
49+
margin-top: 30rpx;
50+
}
51+
52+
.parent {
53+
display: inline-grid;
54+
grid-template-columns: repeat(2, 158rpx);
55+
grid-row-gap: 20px;
56+
grid-column-gap: 20px;
57+
}

src/album/index.js

Lines changed: 48 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,22 @@ Component({
4444
key: {
4545
type: String,
4646
value: 'url'
47+
},
48+
maxNumber: {
49+
type: Number,
50+
value: 9
51+
},
52+
customRowNumber: {
53+
type: Boolean,
54+
value: false
55+
},
56+
everyRowNumber: {
57+
type: Number,
58+
value: 3
59+
},
60+
previewFullImage: {
61+
type: Boolean,
62+
value: true,
4763
}
4864
},
4965

@@ -59,6 +75,10 @@ Component({
5975
row: 0,
6076
// 图片排列几列
6177
colum: 0,
78+
// 用于显示的图片列表
79+
showUrls: [],
80+
// 传入的url长度是否大于maxNumber指定的数量
81+
isLong: false,
6282
},
6383

6484
/**
@@ -69,12 +89,29 @@ Component({
6989
// 在组件实例进入页面节点树时执行
7090

7191
//判断传入urls长度
72-
if (this.data.urls.length > 9) {
73-
const urls = this.data.urls.slice(0, 9);
92+
let urls = [];
93+
if (this.data.urls.length > this.data.maxNumber) {
94+
urls = this.data.urls.slice(0, this.data.maxNumber);
7495
this.setData({
75-
urls
96+
isLong: true,
7697
});
77-
console.warn('超过9张图片!');
98+
console.warn('图片数量超过maxNumber指定数量');
99+
} else {
100+
urls = this.data.urls;
101+
}
102+
this.setData({
103+
showUrls: urls
104+
});
105+
106+
if (!this.data.customRowNumber) {
107+
let urlLength = this.data.showUrls.length;
108+
if (urlLength > 1 && urlLength < 5) {
109+
this.setData({
110+
everyRowNumber: 2
111+
});
112+
} else(this.setData({
113+
everyRowNumber: 3
114+
}));
78115
}
79116

80117
this.preview();
@@ -137,7 +174,13 @@ Component({
137174

138175
onPreviewTap(e) {
139176
const index = e.currentTarget.id;
140-
const urls = this.data.urls;
177+
let urls;
178+
if (this.data.previewFullImage) {
179+
urls = this.data.urls;
180+
} else {
181+
urls = this.data.showUrls;
182+
}
183+
141184
let tempFilePath = '';
142185
let previewImageList = [];
143186
const newType = this.data.newType;

src/album/index.less

Lines changed: 43 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,48 @@
11
/* miniprogram_npm/lin-ui/picture-album/index.wxss */
2-
.container{
2+
.container {
33
display: grid;
4-
54
}
5+
66
.vertical {
77
height: 360rpx;
8-
}
8+
}
9+
10+
.parent {
11+
display: inline-grid;
12+
grid-template-columns: repeat(auto-fill);
13+
}
14+
15+
.child {
16+
box-sizing: border-box;
17+
background-color: white;
18+
flex: 0 0 44%;
19+
height: 100px;
20+
border: 1px solid red;
21+
margin-top: 4%;
22+
margin-left: 2%;
23+
margin-right: 2%;
24+
}
25+
26+
.dimback {
27+
background: #000;
28+
}
29+
30+
.dim {
31+
opacity: 0.6;
32+
filter: alpha(opacity=60);
33+
}
34+
35+
36+
.imageContainer {
37+
position: relative;
38+
}
39+
40+
.imageContainer:last-child>.text {
41+
color: white;
42+
font-weight: bold;
43+
position: absolute;
44+
top: 50%;
45+
left: 50%;
46+
transform: translate(-50%, -50%);
47+
font-size: larger;
48+
}

src/album/index.wxml

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,9 @@
11
<wxs src="index.wxs" module="album"></wxs>
2-
<view class="container l-class" style="{{album.containerStyle(urls, multipleSize, gapRow, gapColumn)}}">
3-
<block wx:for="{{urls}}" wx:key="index">
4-
<image id='{{index}}' mut-bind:tap="onPreviewTap" class="{{album.blockClass(urls, horizontalScreen)}}" style="{{album.blockStyle(urls, horizontalScreen, shortSideValue, singleSize, multipleSize)}}" src="{{newType?item[key]:item}}" mode="{{urls.length === 1?singleMode:multipleMode}}" />
2+
<view style="{{album.gridStyle(gapRow, gapColumn, multipleSize, everyRowNumber)}}">
3+
<block wx:for="{{showUrls}}" wx:key="index" class="child">
4+
<view class="imageContainer {{album.dimBack(isLong, index, maxNumber, previewFullImage)}}" style="{{album.blockStyle(urls, horizontalScreen, shortSideValue, singleSize, multipleSize)}}">
5+
<image class="{{album.dim(isLong, index, maxNumber, previewFullImage)}}}" id='{{index}}' bind:tap="onPreviewTap" style="{{album.blockStyle(urls, horizontalScreen, shortSideValue, singleSize, multipleSize)}}" src="{{newType?item[key]:item}}" mode="{{urls.length === 1?singleMode:multipleMode}}" />
6+
<view wx:if="{{album.image(isLong, index, maxNumber, previewFullImage)}}" class="text">+{{urls.length-maxNumber}}</view>
7+
</view>
58
</block>
69
</view>

src/album/index.wxs

Lines changed: 42 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
var containerStyle = function (urls, multipleSize, gapRow, gapColumn) {
2-
urls.length === 2 || urls.length === 4 ? 'width:' + (2 * multipleSize + gapRow) + 'rpx;' : 'width:' + (3 * multipleSize + 2 * gapRow) + 'rpx;'
32
if (urls.length === 2 || urls.length === 4) {
43
return 'width:' + (2 * multipleSize + gapRow) + 'rpx; grid-row-gap:' + gapColumn + 'rpx; grid-column-gap:' + gapRow + 'rpx;grid-template-columns:repeat(auto-fit, ' + multipleSize + 'rpx);'
54
} else {
@@ -32,8 +31,49 @@ var blockStyle = function (urls, horizontalScreen, shortSideValue, singleSize, m
3231
}
3332
}
3433

34+
var gridStyle = function (gapRow, gapColumn, multipleSize, everyRowNumber) {
35+
return 'display:inline-grid;grid-template-columns: repeat(' + everyRowNumber + ',' + multipleSize + 'rpx);grid-row-gap:' + gapRow + 'rpx;grid-column-gap:' + gapColumn + 'rpx;'
36+
}
37+
38+
var dimBack = function (isLong, index, maxNumber, previewFullImage) {
39+
if (previewFullImage) {
40+
if (isLong) {
41+
if (index == maxNumber - 1) {
42+
return 'dimback'
43+
}
44+
}
45+
}
46+
return ''
47+
}
48+
49+
var dim = function (isLong, index, maxNumber, previewFullImage) {
50+
if (previewFullImage) {
51+
if (isLong) {
52+
if (index == maxNumber - 1) {
53+
return 'dim'
54+
}
55+
}
56+
}
57+
return ''
58+
}
59+
60+
var image = function (isLong, index, maxNumber, previewFullImage) {
61+
if (previewFullImage) {
62+
if (isLong) {
63+
if (index == maxNumber - 1) {
64+
return true
65+
}
66+
}
67+
}
68+
return false
69+
}
70+
3571
module.exports = {
3672
containerStyle: containerStyle,
3773
blockClass: blockClass,
38-
blockStyle: blockStyle
74+
blockStyle: blockStyle,
75+
gridStyle: gridStyle,
76+
dimBack: dimBack,
77+
dim: dim,
78+
image: image,
3979
}

0 commit comments

Comments
 (0)