Skip to content

Commit 25d2fe3

Browse files
author
kai.zhang
committed
# add index page , plugin of timeago
1 parent ebf884c commit 25d2fe3

File tree

4 files changed

+245
-4
lines changed

4 files changed

+245
-4
lines changed

src/components/LoadMore/index.vue

+69
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
<template>
2+
<div class="scroll-box" v-scroll="scrollParams">
3+
<slot></slot>
4+
{{isLoading}}
5+
</div>
6+
</template>
7+
8+
<script>
9+
export default {
10+
props: {
11+
loadMore: {
12+
type: Function,
13+
required: true
14+
},
15+
offset: {
16+
type: Number,
17+
default: 50
18+
}
19+
},
20+
data() {
21+
return {
22+
isLoading: false,
23+
isLoadAll: false
24+
};
25+
},
26+
computed: {
27+
scrollParams() {
28+
return {
29+
offset: this.offset,
30+
loadMoreFunc: this.loadMore,
31+
isLoading: this.isLoading,
32+
isLoadAll: this.isLoadAll
33+
};
34+
}
35+
},
36+
directives: {
37+
scroll: {
38+
bind: (el, binding) => {
39+
let val = binding.value;
40+
el.addEventListener("scroll", () => {
41+
if (val.isLoading || val.isLoadAll) {
42+
return;
43+
}
44+
if (el.scrollTop + el.offsetHeight + val.offset > el.scrollHeight) {
45+
val.isLoading = true;
46+
val.loadMoreFunc({
47+
loaded() {
48+
val.isLoading = false;
49+
},
50+
loadAll() {
51+
val.isLoadAll = true;
52+
}
53+
});
54+
}
55+
});
56+
}
57+
}
58+
}
59+
};
60+
</script>
61+
62+
<style lang="less" scoped>
63+
.scroll-box {
64+
height: 100%;
65+
overflow: scroll;
66+
-webkit-overflow-scrolling: touch;
67+
}
68+
</style>
69+

src/filters/index.js

+7
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
import timeAgo from '@/utils/timeago';
2+
13
// 常用数字精度换算
24
export function toFixed(val, num = 2) {
35
if (isNaN(val)) {
@@ -23,3 +25,8 @@ export function wordLimit(val, limitCount = 9999) {
2325
}
2426
return `${tmpStr}${realLen > limitCount ? '...' : ''}`;
2527
}
28+
29+
30+
export function setTimeAgo(val) {
31+
return timeAgo(val);
32+
}

src/pages/index.vue

+135-4
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,156 @@
11
<template>
2-
<div>
3-
123
2+
<div class="app-box">
3+
<div class="app-flex app-flex-full">
4+
<div class="app-tag">
5+
<ul class="tag-box">
6+
<li class="app-ellipsis">
7+
<a href="javascript:;" class="active">
8+
<i class="coverImg" :style="{'backgroundImage':'url(https://sfault-avatar.b0.upaiyun.com/399/739/3997397795-5a6edc1c3167f_small)'}"></i>
9+
标签0
10+
</a>
11+
</li>
12+
<li class="app-ellipsis" v-for="i in 5" :key="i">
13+
<a href="javascript:;">
14+
<i class="coverImg" :style="{'backgroundImage':'url(https://sfault-avatar.b0.upaiyun.com/399/739/3997397795-5a6edc1c3167f_small)'}"></i>
15+
标签{{i+1}}
16+
</a>
17+
</li>
18+
</ul>
19+
</div>
20+
<div class="col-1">
21+
<load-more :load-more="loadMore">
22+
<ul class="news">
23+
<li class="item-card" v-for="(item, $idx) in dataList" :key="$idx">
24+
<div class="app-flex app-flex-full">
25+
<div class="item-cover">
26+
<div class="coverImg" :style="{'backgroundImage':`url(${item.coverUrl})`}"></div>
27+
</div>
28+
<div class="col-1 item-section">
29+
<header class="text-cut2">{{item.title}}</header>
30+
<section class="text-cut2">{{item.describ}}</section>
31+
<footer>
32+
<i class="icon-eye"></i>
33+
{{item.viewCount}}
34+
{{item.createTime | setTimeAgo}}
35+
</footer>
36+
</div>
37+
</div>
38+
</li>
39+
</ul>
40+
</load-more>
41+
</div>
42+
<div>
43+
44+
</div>
45+
</div>
446
</div>
547
</template>
648

749
<script>
50+
const LoadMore = () => import("@/components/LoadMore/index.vue");
851
952
export default {
1053
data() {
1154
return {
12-
55+
count: 100,
56+
dataList: []
1357
};
1458
},
59+
components: {
60+
LoadMore
61+
},
1562
methods: {
63+
getDataList() {
64+
let data = {
65+
title: "测试标题",
66+
describ:
67+
"以前iOS版本降级使用 Helvetica。 中文字体设置为华文黑体STHeiTi, iOS 9+ 就开始支持 -apple-system 参数了, Chrome 使用 BlinkMacSystemFont ,兼容 iOS/MacOS。现在很多设计师的字体都是PingFang,所以这里...",
68+
viewCount: 100,
69+
createTime: "1410501101602",
70+
coverUrl: "https://segmentfault.com/img/bVbcXWQ?w=500&amp;h=500"
71+
};
72+
let res = new Array(10).fill(data);
1673
74+
this.dataList = [...this.dataList, ...res];
75+
},
76+
loadMore(action) {
77+
console.log("loadMore");
78+
setTimeout(() => {
79+
this.getDataList();
80+
action.loaded();
81+
}, 2000);
82+
}
1783
},
1884
mounted() {
85+
this.getDataList();
1986
}
2087
};
2188
</script>
2289

2390
<style lang="less" scoped>
24-
91+
.app-box {
92+
width: 970px;
93+
height: 100%;
94+
margin: auto;
95+
position: relative;
96+
}
97+
.app-tag {
98+
width: 120px;
99+
padding-right: 20px;
100+
.tag-box {
101+
a {
102+
i {
103+
width: 16px;
104+
height: 16px;
105+
position: absolute;
106+
left: 8px;
107+
top: 50%;
108+
transform: translate(0, -50%);
109+
}
110+
display: block;
111+
position: relative;
112+
padding: 8px 0;
113+
border-radius: 2px;
114+
text-align: center;
115+
text-decoration: none;
116+
color: #666;
117+
transition: all 0.3s ease;
118+
&:hover {
119+
background-color: #a9c9e2;
120+
transition: all 0.3s ease;
121+
}
122+
}
123+
}
124+
}
125+
.item-card {
126+
padding: 10px 0;
127+
border-bottom: 1px solid #eee;
128+
.item-cover div {
129+
width: 100px;
130+
height: 100px;
131+
margin-right: 10px;
132+
}
133+
.item-section {
134+
> header {
135+
font-size: 18px;
136+
color: #222;
137+
}
138+
> section {
139+
font-size: 13px;
140+
color: #888;
141+
line-height: 1.5;
142+
}
143+
> footer {
144+
color: #666;
145+
i {
146+
width: 20px;
147+
height: 20px;
148+
display: inline-block;
149+
border-radius: 50%;
150+
background-color: #eee;
151+
color: #fff;
152+
}
153+
}
154+
}
155+
}
25156
</style>

src/utils/timeago.js

+34
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
const getTimeAgo = (dateTimeStamp) => {
2+
let now = new Date().getTime();
3+
let diffValue = now - dateTimeStamp;
4+
if (diffValue < 0) {
5+
return '未知时间';
6+
}
7+
let minute = 1000 * 60;
8+
let hour = minute * 60;
9+
let day = hour * 24;
10+
let month = day * 30;
11+
let year = month * 12;
12+
let yearC = diffValue / year;
13+
let monthC = diffValue / month;
14+
let weekC = diffValue / (7 * day);
15+
let dayC = diffValue / day;
16+
let hourC = diffValue / hour;
17+
let minC = diffValue / minute;
18+
if (yearC >= 1) {
19+
return `${~~yearC}年前`;
20+
} else if (monthC >= 1) {
21+
return `${~~monthC}月前`;
22+
} else if (weekC >= 1) {
23+
return `${~~weekC}周前`;
24+
} else if (dayC >= 1) {
25+
return `${~~dayC}天前`;
26+
} else if (hourC >= 1) {
27+
return `${~~hourC}小时前`;
28+
} else if (minC >= 1) {
29+
return `${~~minC}分钟前`;
30+
}
31+
return `刚刚`;
32+
};
33+
34+
export default getTimeAgo;

0 commit comments

Comments
 (0)