标题和内容的联动效果
一. 点击标题滚动到对应位咋位置
-
在中监听标题的点击,获取index
-
滚动到对应的主题:
- 获取所有主题的offsetTop
- 问题:在哪里才能获取正确的offsetTop
- 1.created肯定不行,压根不能获取元素
- 2.mounted也不行,数据还没有获取到
- 3.获取到数据的回调中也不行,DOM还没渲染完
- 4.$nextTick也不行,因为图片的高度没有被计算在内
- 5.在图片加载完成后,得到的offsetTop才是正确的
二. 内容滚动,显示正确标题
普通做法:
for (let i = 0; i < length; i++) {
if ( (this.currentIndex !== i && i < length - 1 && positionY >= this.themeTopYs[i] && positionY < this.themeTopYs[i + 1]) || ( i === length - 1 && positionY >= this.themeTopYs[i]) ) {
this.currentIndex = i;
this.$refs.nav.currentIndex = this.currentIndex;
}
}
条件成立&