1. Detail.vue
- data 增加
currentIndex: 0
- detail-nav-bar 增加属性
ref="nav"
- methods 增加
contentScroll(position)
- scroll 增加属性
:probe-type="2" 和事件 @scroll="contentScroll"
<template>
<div id="detail">
<detail-nav-bar @titleClick="titleClick" class="detail-nav" ref="nav"/>
<scroll
ref="scroll"
class="detail-scroll-content"
:probe-type="2"
@scroll="contentScroll">
<detail-swiper ref="swiper" :top-images="topImages"/>
<detail-base-info :goods="goods"/>
<detail-shop-info :shop="shop"/>
<detail-goods-info :detail-info="detailInfo" @imageLoad="imageLoad"/>
<detail-param-info ref="param" :param-info="paramInfo"/>
<detail-comment-info ref="comment" :comment-info="commentInfo"/>
<goods-list ref="recommond" :goods="recommends"/>
</scroll>
</div>
</template>
<script>
import DetailNavBar from './childComps/DetailNavBar'
import DetailSwiper from './childComps/DetailSwiper'
import DetailBaseInfo from './childComps/DetailBaseInfo'
import DetailShopInfo from './childComps/DetailShopInfo'
import DetailGoodsInfo from './childComps/DetailGoodsInfo'
import DetailParamInfo from './childComps/DetailParamInfo'
import DetailCommentInfo from './childComps/DetailCommentInfo'
import Scroll from 'components/common/scroll/Scroll'
import GoodsList from 'components/content/goods/GoodsList'
import {getDetail, getRecommend, Goods, Shop, GoodsParam} from 'network/detail'
import { itemListenerMixin } from 'common/mixin'
export default {
name: 'Detail',
components: {
DetailNavBar,
DetailSwiper,
DetailBaseInfo,
DetailShopInfo,
DetailGoodsInfo,
DetailParamInfo,
DetailCommentInfo,
Scroll,
GoodsList
},
mixins: [itemListenerMixin],
data() {
return {
iid: null,
topImages: [],
goods: {},
shop: {},
detailInfo: {},
paramInfo: {},
commentInfo: {},
recommends: [],
themeTopYs: [],
currentIndex: 0
}
},
methods: {
getThemeTopYs() {
this.themeTopYs = [
this.$refs.swiper.$el.offsetTop,
this.$refs.param.$el.offsetTop - 44,
this.$refs.comment.$el.offsetTop - 44,
this.$refs.recommond.$el.offsetTop - 44
]
},
imageLoad() {
this.$refs.scroll.refresh()
this.getThemeTopYs()
},
titleClick(index) {
this.$refs.scroll.scrollTo(0, -this.themeTopYs[index], 200)
},
contentScroll(position) {
// 1. 获取y值
const positionY = -position.y
// 2. positionY和主题中的值进行对比
const len = this.themeTopYs.length
for (let i in this.themeTopYs) {
const intI = parseInt(i)// for ... in ... 遍历的索引是字符串
const themeTopY = this.themeTopYs[intI]
if (this.currentIndex !== intI
&& ((intI < len - 1 && positionY >= themeTopY && positionY < this.themeTopYs[intI + 1])
|| (intI === len - 1 && positionY >= themeTopY))) {
this.currentIndex = intI
this.$refs.nav.currentIndex = this.currentIndex
}
}
}
},
created() {
// 1. 保存传入的iid
this.iid = this.$route.params.iid
// 2. 根据iid请求详情数据
getDetail(this.iid).then(res => {
const data = res.result
// 1. 获取顶部的图片轮播数据
this.topImages = data.itemInfo.topImages
// 2. 获取商品信息
this.goods = new Goods(data.itemInfo, data.columns, data.shopInfo.services)
// 3. 创建店铺信息的对象
this.shop = new Shop(data.shopInfo)
// 4. 保存商品的详情数据
this.detailInfo = data.detailInfo
// 5. 获取参数的信息
this.paramInfo = new GoodsParam(data.itemParams.info, data.itemParams.rule)
// 6. 取出评论的信息
if (data.rate.cRate !== 0) {
this.commentInfo = data.rate.list[0]
}
// 初始化scroll
this.$nextTick(() => {
this.$refs.scroll.createScroll()
})
})
// 3. 请求推荐数据
getRecommend().then(res => {
this.recommends = res.data.list
})
}
}
</script>
<style scoped>
#detail {
position: relative;
z-index: 9;
background-color: #fff;
height: 100vh;
}
.detail-nav {
position: relative;
z-index: 9;
background-color: #fff;
}
.detail-scroll-content {
height: calc(100% - 44px);
}
</style>