初始化项目:
npm init webpack mytravel
//接下来根据提示内容选择就行了
一、网站首页Header开发
安装依赖:
npm install stylus --save
npm install stylus-loader --save
1rem = html font-size = 50px
首页header样式:
<style lang="stylus" scoped>
.header
display: flex
line-height: .86rem
background: #00bcd4
color: #fff
.header-left
width: .64rem
float: left
.header-input
flex: 1
height: .64rem
line-height: .64rem
margin-top: .12rem
margin-left: .2rem
background: #fff
border-radius: .1rem
color: #ccc
.header-right
width: 1.24
float: right
text-align: center
</style>
二、iconfont的使用
iconfont官网:https://www.iconfont.cn
header部分代码实现:
<style lang="stylus" scoped>
.header
display: flex
line-height: .86rem
background: #00bcd4
color: #fff
.header-left
width: .64rem
float: left
.back-icon
text-align: center
font-size: .4rem
.header-input
flex: 1
height: .64rem
line-height: .64rem
margin-top: .12rem
margin-left: .2rem
padding-left: .2rem
background: #fff
border-radius: .1rem
color: #ccc
.header-right
width: 1.24rem
float: right
text-align: center
.arrow-icon
margin-left: -.04rem
font-size: .24rem
</style>
效果图:
修改完webpack.base.conf.js的内容后,需要重新启动环境。
三、首页轮播图
安装vue-awesome-swiper插件:
地址:https://github.com/surmon-china/vue-awesome-swiper
可以快速构建轮播图
使用问题:
我安装的是最新版本,但是轮播图的小圆圈一直不显示,解决办法:
在2.6.7版本:
swiperOption: {
pagination: '.swiper-pagination',
loop: true
},
新的版本:
swiperOption: {
pagination: {
el: '.swiper-pagination'
},
loop: true
},
四、首页桌面图标
<template>
<div class="icons">
<div class="icon">
<div class="icon-img">
<img class="icon-img-content" src="http://img1.qunarzz.com/piao/fusion/1803/95/f3dd6c383aeb3b02.png" />
</div>
<p class="icon-desc">热门景点</p>
</div>
<div class="icon">
<div class="icon-img">
<img class="icon-img-content" src="http://img1.qunarzz.com/piao/fusion/1803/95/f3dd6c383aeb3b02.png" />
</div>
<p class="icon-desc">热门景点</p>
</div>
<div class="icon">
<div class="icon-img">
<img class="icon-img-content" src="http://img1.qunarzz.com/piao/fusion/1803/95/f3dd6c383aeb3b02.png" />
</div>
<p class="icon-desc">热门景点</p>
</div>
<div class="icon">
<div class="icon-img">
<img class="icon-img-content" src="http://img1.qunarzz.com/piao/fusion/1803/95/f3dd6c383aeb3b02.png" />
</div>
<p class="icon-desc">热门景点</p>
</div>
<div class="icon">
<div class="icon-img">
<img class="icon-img-content" src="http://img1.qunarzz.com/piao/fusion/1803/95/f3dd6c383aeb3b02.png" />
</div>
<p class="icon-desc">热门景点</p>
</div>
<div class="icon">
<div class="icon-img">
<img class="icon-img-content" src="http://img1.qunarzz.com/piao/fusion/1803/95/f3dd6c383aeb3b02.png" />
</div>
<p class="icon-desc">热门景点</p>
</div>
<div class="icon">
<div class="icon-img">
<img class="icon-img-content" src="http://img1.qunarzz.com/piao/fusion/1803/95/f3dd6c383aeb3b02.png" />
</div>
<p class="icon-desc">热门景点</p>
</div>
<div class="icon">
<div class="icon-img">
<img class="icon-img-content" src="http://img1.qunarzz.com/piao/fusion/1803/95/f3dd6c383aeb3b02.png" />
</div>
<p class="icon-desc">热门景点</p>
</div>
</div>
</template>
<script>
export default {
name: 'HomeIcons'
}
</script>
<style lang="stylus" scoped>
@import '~styles/varibles.styl'
.icons
overflow: hidden
height: 0
padding-bottom: 50%
.icon
position: relative
overflow: hidden
float: left
width: 25%
height: 0
padding-bottom: 25%
.icon-img
position: absolute
top: 0
left: 0
right: 0
bottom: .44rem
box-sizing: border-box
padding: .1rem
.icon-img-content
display: block
margin: 0 auto
height: 100%
.icon-desc
position: absolute
left: 0
right: 0
bottom: 0
height: .44rem
line-height: .44rem
color: $darkTextColor
text-align: center
</style>
五、图标区域逻辑实现
<template>
<div class="icons">
<swiper>
<swiper-slide v-for="(page, index) of pages" :key="index">
<div
class="icon"
v-for="item of page"
:key="item.id"
>
<div class="icon-img">
<img class="icon-img-content" :src="item.imgUrl" />
</div>
<p class="icon-desc">{{item.desc}}</p>
</div>
</swiper-slide>
</swiper>
</div>
</template>
<script>
export default {
name: 'HomeIcons',
data () {
return {
iconList: [{
id: '0001',
imgUrl: 'http://img1.qunarzz.com/piao/fusion/1803/95/f3dd6c383aeb3b02.png',
desc: '景点门票'
}, {
id: '0002',
imgUrl: 'http://img1.qunarzz.com/piao/fusion/1803/fc/b10a6b2e4f0fe102.png',
desc: '万龙滑雪'
}, {
id: '0003',
imgUrl: 'http://img1.qunarzz.com/piao/fusion/1803/fc/b10a6b2e4f0fe102.png',
desc: '陶然亭冰雪'
}, {
id: '0004',
imgUrl: 'http://img1.qunarzz.com/piao/fusion/1803/6c/9e54a8540fee0102.png',
desc: '故宫'
}, {
id: '0005',
imgUrl: 'http://img1.qunarzz.com/piao/fusion/1803/fc/b10a6b2e4f0fe102.png',
desc: '北京滑雪'
}, {
id: '0006',
imgUrl: 'http://img1.qunarzz.com/piao/fusion/1803/fc/b10a6b2e4f0fe102.png',
desc: '静之湖滑雪'
}, {
id: '0007',
imgUrl: 'http://img1.qunarzz.com/piao/fusion/1804/5a/13ceb38dcf262f02.png',
desc: '一日游'
}, {
id: '0008',
imgUrl: 'http://img1.qunarzz.com/piao/fusion/1803/67/9a1678221b8e0e02.png',
desc: '古北水镇'
}, {
id: '0009',
imgUrl: 'http://img1.qunarzz.com/piao/fusion/1803/ab/6f7d6e44963c9302.png',
desc: '泡温泉'
}]
}
},
computed: {
pages () {
const pages = []
this.iconList.forEach((item, index) => {
const page = Math.floor(index / 8)
// console.log(page)
if (!pages[page]) {
pages[page] = []
}
pages[page].push(item)
})
return pages
}
}
}
</script>
<style lang="stylus" scoped>
@import '~styles/varibles.styl'
@import '~styles/mixins.styl'
.icons >>> .swiper-container
height: 0
padding-bottom: 50%
.icon
position: relative
overflow: hidden
float: left
width: 25%
height: 0
padding-bottom: 25%
.icon-img
position: absolute
top: 0
left: 0
right: 0
bottom: .44rem
box-sizing: border-box
padding: .1rem
.icon-img-content
display: block
margin: 0 auto
height: 100%
.icon-desc
position: absolute
left: 0
right: 0
bottom: 0
height: .44rem
line-height: .44rem
color: $darkTextColor
text-align: center
ellipsis()
</style>
六、首页推荐组件开发
<template>
<div>
<div class="title">热销推荐</div>
<ul>
<li class="item border-bottom" v-for="item of recommendList" :key="item.id">
<img class="item-img" :src="item.imgUrl" />
<div class="item-info">
<p class="item-title">{{item.titel}}</p>
<p class="item-desc">{{item.desc}}</p>
<button class="item-button">查看详情</button>
</div>
</li>
</ul>
</div>
</template>
<script>
export default {
name: 'HomeRecommend',
data () {
return {
recommendList: [{
id: '0001',
imgUrl: 'http://img1.qunarzz.com/sight/p0/1602/45/459bd3d00321a44790.water.jpg_200x200_6cd905a1.jpg',
titel: '景山公园',
desc: '这是景山公园'
}, {
id: '0002',
imgUrl: 'http://img1.qunarzz.com/sight/p0/1602/45/459bd3d00321a44790.water.jpg_200x200_6cd905a1.jpg',
titel: '景山公园',
desc: '这是景山公园'
}, {
id: '0003',
imgUrl: 'http://img1.qunarzz.com/sight/p0/1602/45/459bd3d00321a44790.water.jpg_200x200_6cd905a1.jpg',
titel: '景山公园',
desc: '这是景山公园'
}, {
id: '0004',
imgUrl: 'http://img1.qunarzz.com/sight/p0/1602/45/459bd3d00321a44790.water.jpg_200x200_6cd905a1.jpg',
titel: '景山公园',
desc: '这是景山公园'
}]
}
}
}
</script>
<style lang="stylus" scoped>
@import '~styles/mixins.styl'
.title
line-height: .8rem
background: #EEEEEE
text-indent: .2rem
margin-top: .2rem
.item
overflow: hidden
display: flex
height: 1.9rem
.item-img
width: 1.7rem
height: 1.7rem
padding: .1rem
.item-info
flex: 1
padding: .1rem
min-width: 0
.item-title
line-height: .54rem
font-size: .32rem
ellipsis()
.item-desc
line-height: .4rem
color: #ccc
ellipsis()
.item-button
line-height: .44rem
margin-top: .16rem
background: #ff9300
padding: 0 .2rem
border-radius: 0.6rem
color: #fff
</style>
七、周末游组件
<template>
<div>
<div class="title">周末去哪</div>
<ul>
<li class="item border-bottom" v-for="item of recommendList" :key="item.id">
<div class="item-img-wrapper">
<img class="item-img" :src="item.imgUrl" />
</div>
<div class="item-info">
<p class="item-title">{{item.titel}}</p>
<p class="item-desc">{{item.desc}}</p>
</div>
</li>
</ul>
</div>
</template>
<script>
export default {
name: 'HomeWeekend',
data () {
return {
recommendList: [{
id: '0001',
imgUrl: 'http://img1.qunarzz.com/sight/source/1811/f3/86173f863bef61.jpg_r_640x214_52b003ac.jpg',
titel: '景山公园',
desc: '这是景山公园'
}, {
id: '0002',
imgUrl: 'http://img1.qunarzz.com/sight/source/1811/f3/86173f863bef61.jpg_r_640x214_52b003ac.jpg',
titel: '景山公园',
desc: '这是景山公园'
}, {
id: '0003',
imgUrl: 'http://img1.qunarzz.com/sight/source/1811/f3/86173f863bef61.jpg_r_640x214_52b003ac.jpg',
titel: '景山公园',
desc: '这是景山公园'
}, {
id: '0004',
imgUrl: 'http://img1.qunarzz.com/sight/source/1811/f3/86173f863bef61.jpg_r_640x214_52b003ac.jpg',
titel: '景山公园',
desc: '这是景山公园'
}]
}
}
}
</script>
<style lang="stylus" scoped>
@import '~styles/mixins.styl'
.title
line-height: .8rem
background: #EEEEEE
text-indent: .2rem
margin-top: .2rem
.item-img-wrapper
overflow: hidden
height: 0
padding-bottom: 36%
.item-img
width: 100%
.item-info
padding: .1rem
.item-title
line-height: .54rem
font-size: .32rem
ellipsis()
.item-desc
line-height: .4rem
color: #ccc
ellipsis()
</style>
八、使用Ajax获取首页数据
安装axios插件:
npm install axios --save
引入:
import axios from 'axios'
在Home.vue中写入:
methods: {
getHomeInfo () {
axios.get('/api/index.json')
.then(this.getHomeInfoSucc)
},
getHomeInfoSucc (res) {
console.log(res)
}
},
mounted () {
this.getHomeInfo()
}