Skip to content

Commit bc58720

Browse files
authored
Merge pull request #2 from viewweiwu/master
🏇 clean index page
2 parents f3875e2 + e49a1f3 commit bc58720

File tree

3 files changed

+68
-55
lines changed

3 files changed

+68
-55
lines changed

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515
},
1616
"dependencies": {
1717
"axios": "^0.18.0",
18-
"nuxt": "^1.0.0"
18+
"nuxt": "^1.4.0"
1919
},
2020
"devDependencies": {
2121
"@nuxtjs/axios": "^5.1.1",

pages/index.vue

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,10 @@
7070
.to-top-btn.show {
7171
display block
7272
}
73+
.loading-more {
74+
text-align center
75+
margin: 10px
76+
}
7377
7478
@media (max-width 600px) {
7579
.category-nav {
@@ -99,6 +103,9 @@
99103
<v-icon name="android-arrow-dropup" size="16"></v-icon>
100104
</button>
101105
<nuxt />
106+
<div class="loading-more">
107+
<el-button icon="el-icon-loading">正在加载</el-button>
108+
</div>
102109
</div>
103110
</div>
104111
</template>

pages/index/index.vue

Lines changed: 60 additions & 54 deletions
Original file line numberDiff line numberDiff line change
@@ -159,10 +159,36 @@
159159
</template>
160160

161161
<script>
162+
const getData = (store, self) => {
163+
let params = {
164+
suid: 'aemu3ZqVijiqQj2QEFiB',
165+
ab: 'welcome_3',
166+
src: 'web'
167+
}
168+
return new Promise(resolve => {
169+
store.dispatch('recommend', params).then(res => {
170+
if (self) {
171+
const { d, m, s } = res
172+
if (s === 1) {
173+
self.recommends.push(...d)
174+
} else {
175+
self.$message({
176+
message: m,
177+
type: 'warning'
178+
})
179+
}
180+
self.scrollStatus = true
181+
}
182+
resolve(res)
183+
})
184+
})
185+
}
186+
162187
export default {
163188
data () {
164189
return {
165-
scrollStatus: true
190+
scrollStatus: true,
191+
recommends: []
166192
}
167193
},
168194
filters: {
@@ -184,66 +210,46 @@ export default {
184210
}
185211
},
186212
async asyncData ({ store, error }) {
187-
let params = {
188-
suid: 'aemu3ZqVijiqQj2QEFiB',
189-
ab: 'welcome_3',
190-
src: 'web'
191-
}
192-
let [ res ] = await Promise.all([
193-
store.dispatch('recommend', params)
194-
]).catch((e) => {
195-
error({ statusCode: 404, message: 'Post not found' })
196-
})
213+
let res = await getData(store)
197214
return {
198215
recommends: res.d
199216
}
200217
},
201218
mounted () {
202-
const self = this
203-
const $el = document.documentElement
204-
const $entry = self.$refs.entry
205-
let clienHeight = $el.clientHeight
206-
let containerHeight = ~~(window.getComputedStyle($entry, null).getPropertyValue('height').split('px')[0]) + 140
207-
208-
const params = {
209-
suid: 'aemu3ZqVijiqQj2QEFiB',
210-
ab: 'welcome_3',
211-
src: 'web'
212-
}
213-
window.onscroll = () => {
214-
if ($el.scrollTop > 120) {
215-
document.getElementsByClassName('to-top-btn')[0].classList.add('show')
216-
} else {
217-
document.getElementsByClassName('to-top-btn')[0].classList.remove('show')
218-
}
219-
// console.log(containerHeight, $el.scrollTop + clienHeight, clienHeight)
220-
if ($el.scrollTop + clienHeight > containerHeight - 10 && self.scrollStatus) {
221-
self.scrollStatus = false
222-
self.$store.dispatch('recommend', params).then(
223-
res => {
224-
const { d, m, s } = res
225-
if (s === 1) {
226-
self.recommends = [...self.recommends, ...d]
227-
containerHeight = ~~(window.getComputedStyle($entry, null).getPropertyValue('height').split('px')[0]) + 140
228-
// console.log('containerHeight', containerHeight)
229-
} else {
230-
self.$message({
231-
message: m,
232-
type: 'warning'
233-
})
234-
}
235-
}
236-
).then(() => {
237-
setTimeout(() => {
238-
self.scrollStatus = true
239-
}, 300)
219+
this.getFullPageData()
220+
window.addEventListener('scroll', this.handleScroll)
221+
},
222+
destroyed () {
223+
window.removeEventListener('scroll', this.handleScroll)
224+
},
225+
methods: {
226+
getFullPageData () {
227+
if (document.body.offsetHeight < window.innerHeight) {
228+
this.loadMoreData().then(() => {
229+
this.getFullPageData()
240230
})
241231
}
242-
}
243-
window.onresize = () => {
244-
clienHeight = $el.clientHeight
245-
containerHeight = ~~(window.getComputedStyle($entry, null).getPropertyValue('height').split('px')[0]) + 140
246-
// console.log('clienHeight', clienHeight)
232+
},
233+
handleScroll () {
234+
this.timer && clearTimeout(this.timer)
235+
this.timer = setTimeout(this.loadMoreData, 300)
236+
},
237+
loadMoreData () {
238+
return new Promise((resolve) => {
239+
let $el = document.documentElement
240+
let $entry = this.$refs.entry
241+
let clienHeight = $el.clientHeight
242+
let containerHeight = ~~(window.getComputedStyle($entry, null).getPropertyValue('height').split('px')[0]) + 140
243+
// 设置【返回顶部】显示隐藏
244+
document.querySelector('.to-top-btn').classList[$el.scrollTop > 120 ? 'add' : 'remove']('show')
245+
// console.log(containerHeight, $el.scrollTop + clienHeight, clienHeight)
246+
// 滚动到一定高度,重新加载数据
247+
if ($el.scrollTop + clienHeight > containerHeight - 10 && this.scrollStatus) {
248+
getData(this.$store, this).then(res => {
249+
resolve(res)
250+
})
251+
}
252+
})
247253
}
248254
}
249255
}

0 commit comments

Comments
 (0)