[TOC]
#### BScroll 制作左右聯動導航
1. 圖例

2. 需求:
* [ ] 點擊左側菜單 右側滾動到對應位置
* [ ] 滾動右側菜單 左側跟隨聯動
3. 實現步驟
* [ ] 監聽右側商品列表滾動Y坐標 scrollY = 0
* [ ] 求出右邊商品列表累加的所有高度,存放在一個數組中 goodsList = []
* [ ] 利用計算屬性,計算出 scrollY 定位在 goodsList 數組中的區間值
>[danger] 每當監聽到scrollY 值改變,計算屬性會重新計算,返回最新區間值
* [ ] 公式:
~~~
computed: {
...mapState({
goodsData: state => state.goodsData
}),
currentIndex() {
return this.listHeight.findIndex((item, index) => {
return !this.listHeight[index+1] || (this.scrollY >= item && this.scrollY < this.listHeight[index+1])
})
}
},
~~~
* [ ] 點擊左側按鈕,右側商品列表滾動到對應位置
核心算法:
~~~
scrollTo(index) {
const goodsListEl = Array.from(this.$refs.goodsList.querySelectorAll('li'))
this.listScroll.scrollToElement(goodsListEl[index], 300)
}
~~~
* [ ] 右側滾動,左側聯動
思考: 要觸發左側菜單導航聯動,需要在滾動左側菜單時,拿到對應索引,然后用scrollToElement 跳轉到對應的位置
核心算法:
~~~
// 監聽滾動事件
this.listScroll.on('scroll', (pos) => {
const posY = Math.round( Math.abs(pos.y) )
this.scrollY = posY
this.scrollMenu(this.currentIndex);
})
~~~
~~~
scrollMenu(index) {
const menuList = Array.from(this.$refs.goodsMenu.querySelectorAll('li'));
this.menuScroll.scrollToElement(menuList[index], 300, false, -320)
}
~~~
>[danger] 注意: this.scrollY 滾動時改變數據,會觸發計算屬性重新計算,然后根據計算屬性返回的索引值來跳轉對應的菜單導航
- 起步
- 環境搭建
- mock數據
- 基礎
- 生命周期
- 過濾器
- 過渡動畫
- keyframes動畫
- 動畫JS鉤子
- 路由
- 導航守衛
- 全局守衛
- 監聽器
- 自定義組件
- 獲取焦點
- mixins
- mixins抽離vuex
- 國際化
- 動態組件
- Dom
- 擴展
- 安裝devTools
- scss
- Nuxt引用多個UI庫
- vuex
- vuex命名空間
- vuex定義
- cli
- 安裝與卸載
- 環境變量
- 雜項
- Mock數據
- FeHelper
- git
- 反向代理
- 本地存儲
- stylus
- 常用mixins
- jsonp
- 配置
- mock配置
- 跨域配置
- 自定義路徑
- px2rem
- 代理后端請求
- 常用算法
- 字母排序城市數據
- 倒計時
- 通訊錄數據結構
- 請求
- axios防止多次請求
- 封裝axios請求
- axios使用
- 封裝axios
- 插件
- BetterScroll
- 高德定位
- polyfill
- fastClick
- LazyLoad
- storageCache
- moment
- keyFrameAnimation
- vueSwiper
- 組件
- Loading組件
- header組件
- 仿有道App導航
- SupportIcon
- 仿餓了么購物車跳動
- 購物車小球緩動
- 小球飛入購物車
- 仿音樂歌手列表
- 唱片飛入效果
- 搜索組件
- 仿美團PC搜索框
- 頁面布局
- stickyFooter
- 背景色漸變
- 背景虛化
- Ui組件
- CubeUi
- CreateApi
- tab滑屏切換
- 索引列表
- BScroll
- BScroll左右聯動導航
- vant
- 函數庫
- 常用Dom函數庫
- axios封裝
- 格式化音樂播放時長
- 搜索節流
- time格式化
- JS基礎
- window對象中的高度
- JS中的寬高
- 常用正則
- nuxt
- nuxtVuex
- 監聽頁面滾動
- 監聽body滾動
- 監聽局部滾動