微信小程序?qū)崿F(xiàn)菜單左右聯(lián)動效果
本文實(shí)例為大家分享了微信小程序?qū)崿F(xiàn)菜單左右聯(lián)動效果的具體代碼,供大家參考,具體內(nèi)容如下
原理
首先是獲取數(shù)據(jù),并且獲取數(shù)據(jù)的長度(需要根據(jù)長度來計算元素的高度),通過遍歷數(shù)據(jù)的內(nèi)容通過題目和元素個數(shù)的相加得到高度,當(dāng)消失高度小于等于某個元素的高度就設(shè)定索引值給左邊的菜單目錄實(shí)現(xiàn)右邊滑動左邊聯(lián)動,左邊的菜單點(diǎn)擊事件聯(lián)動比較簡單就不說
代碼實(shí)現(xiàn)
wxml
<view class="menu"> ? ? ? ? <view class="left-box"> ? ? ? ? ? ? <scroll-view class="left_menu" scroll-with-animation ?scroll-y="true" scroll-into-view="{{leftViewId}}"> ? ? ? ? ? ? ? ? <text class="menu-item {{index==currentIndex?'menu-active':''}}" wx:for="{{navList}}" wx:key="this" data-id="menu{{index}}" data-i="{{index}}" bindtap="changeMenu">{{item.c_name}}</text> ? ? ? ? ? ? </scroll-view> ? ? ? ? </view> ? ? ? ? <view class="right-box"> ? ? ? ? ? ? <scroll-view class="right_menu" scroll-y='true' ?scroll-with-animation scroll-into-view="{{rightViewId}}" ?bindscroll="getScroll" enable-flex> ? ? ? ? ? ? ? ? <view wx:for="{{navList}}" wx:key="this" class='pro-item' id="menu{{index}}"> ? ? ? ? ? ? ? ? ? ? <view class="right_menu_head">{{item.c_name}}</view> ? ? ? ? ? ? ? ? ? ? <view class="list-box"> ? ? ? ? ? ? ? ? ? ? ? ? <view class="menu_list" wx:for-item='items' wx:for="{{item.list}}" wx:key="this"> ? ? ? ? ? ? ? ? ? ? ? ? <image src="{{items.url}}"></image> ? ? ? ? ? ? ? ? ? ? ? ? <view>{{items.goodsName}}</view> ? ? ? ? ? ? ? ? ? ? </view> ? ? ? ? ? ? ? ? </view> ? ? ? ? ? ? ? ? </view> ? ? ? ? ? ? </scroll-view> ? ? ? ? </view> </view>
wxss,這里使用的是less語法,vscode插件可編譯
.menu{ ? ? .left-box{ ? ? ? ? width: 180rpx; ? ? ? ? border-right: 1px solid #dfdfdf; ? ? ? ? position: fixed; ? ? ? ? left: 0; ? ? ? ? z-index: 10; ? ? ? ? top: 370rpx; ? ? ? ? box-sizing: border-box; ? ? ? ? height: 90%; ? ? ? ? background: #f0f0f0; ? ? ? ? .menu-item{ ? ? ? ? ? ? display: inline-block; ? ? ? ? ? ? width: 180rpx; ? ? ? ? ? ? height: 88rpx; ? ? ? ? ? ? font-size: 26rpx; ? ? ? ? ? ? line-height: 88rpx; ? ? ? ? ? ? background: #fff; ? ? ? ? ? ? text-align: center; ? ? ? ? ? ? border-bottom: 1px solid #dfdfdf; ? ? ? ? } ? ? ? ? .menu-active{ ? ? ? ? ? ? background: #f0f0f0; ? ? ? ? ? ? border-left: 10rpx ?solid #333999; ? ? ? ? } ? ? } ? ?? ? ? .right-box{ ? ? ? ? width: 74%; ? ? ? ? position: fixed; ? ? ? ? top: 360rpx; ? ? ? ? height: 77%; ? ? ? ? right: 0; ? ? ? ? border-left: 20rpx; ? ? ? ? box-sizing: border-box; ? ? ? ? margin-top: 20rpx; ? ? ? ? .right_menu{ ? ? ? ? ? ? height: 100%; ? ? ? ? ? ? box-sizing: border-box; ? ? ? ? ? ? .pro-item{ ? ? ? ? ? ? ? ? .right_menu_head{ ? ? ? ? ? ? ? ? ? ? height: 80rpx; ? ? ? ? ? ? ? ? ? ? line-height: 80rpx; ? ? ? ? ? ? ? ? ? ? font-size: 26rpx; ? ? ? ? ? ? ? ? ? ? font-weight: 600; ? ? ? ? ? ? ? ? } ? ? ? ? ? ? ? ? &:last-child{ ? ? ? ? ? ? ? ? ? ? margin-bottom: 156rpx; ? ? ? ? ? ? ? ? } ? ? ? ? ? ? ? ? .list-box{ ? ? ? ? ? ? ? ? ? ? width: 100%; ? ? ? ? ? ? ? ? ? ? // min-height: 380rpx; ? ? ? ? ? ? ? ? ? ? display: flex; ? ? ? ? ? ? ? ? ? ? flex-wrap: wrap; ? ? ? ? ? ? ? ? ? ? background: #fff; ? ? ? ? ? ? ? ? ? ? box-sizing: border-box; ? ? ? ? ? ? ? ? ? ? border-radius: 10rpx; ? ? ? ? ? ? ? ? ? ? font-size: 20rpx; ? ? ? ? ? ? ? ? ? ? .menu_list{ ? ? ? ? ? ? ? ? ? ? ? ? width: 33.3%; ? ? ? ? ? ? ? ? ? ? ? ? font-size: 20rpx; ? ? ? ? ? ? ? ? ? ? ? ? padding: 20rpx 0; ? ? ? ? ? ? ? ? ? ? ? ? text-align: center; ? ? ? ? ? ? ? ? ? ? ? ? font-style: '微軟雅黑'; ? ? ? ? ? ? ? ? ? ? ? ? image{ ? ? ? ? ? ? ? ? ? ? ? ? ? ? width: 100rpx; ? ? ? ? ? ? ? ? ? ? ? ? ? ? height: 100rpx; ? ? ? ? ? ? ? ? ? ? ? ? } ? ? ? ? ? ? ? ? ? ? ? ? view{ ? ? ? ? ? ? ? ? ? ? ? ? ? ? color: #333; ? ? ? ? ? ? ? ? ? ? ? ? ? ? text-overflow: ellipsis; ? ? ? ? ? ? ? ? ? ? ? ? ? ? white-space: nowrap; ? ? ? ? ? ? ? ? ? ? ? ? ? ? overflow: hidden; ? ? ? ? ? ? ? ? ? ? ? ? ? } ? ? ? ? ? ? ? ? ? ? } ? ? ? ? ? ? ? ? } ? ? ? ? ? ? } ? ? ? ? } ? ? } }
js文件
data:{ ?? ?navList:[..],//數(shù)據(jù) ?? ?currentIndex: 0,//左邊對應(yīng)的索引 ? ? rightViewId: '',//點(diǎn)擊事件右邊的索引 } getScroll(e) {//微信小程序中綁定滑動函數(shù),每滑動一下都會觸發(fā) ? ? ? ? let top = e.detail.scrollTop, ? ? ? ? ? ? h = 0, ? ? ? ? ? ? _this = this; ? ? ? ? for (let i = 0; i < this.data.navList.length; i++) { ? ? ? ? ? ? let dishSize = this.data.navList[i].list.length;//獲取數(shù)據(jù)對應(yīng)展示商品的json ? ? ? ? ? ? h += 38 + parseInt(dishSize / 3 * 80);//獲取當(dāng)前元素的高度,38是標(biāo)題高度,80是元素高度 ? ? ? ? ? ? if (top <= h) {//滿足條件立刻停止循環(huán),就會一直停留再當(dāng)前索引,不滿足當(dāng)前就會自動到下一個菜單 ? ? ? ? ? ? ? ? _this.setData({ ? ? ? ? ? ? ? ? ? ? currentIndex: i ? ? ? ? ? ? ? ? }) ? ? ? ? ? ? ? ? break; ? ? ? ? ? ? } ? ? ? ? } ? ? }, ? ? changeMenu(e) { ? ? ? ? console.log(this.data.heightArr, this.data.topArr); ? ? ? ? this.setData({ ? ? ? ? ? ? currentIndex: e.currentTarget.dataset.i, ? ? ? ? ? ? rightViewId: e.currentTarget.dataset.id ? ? ? ? }) ? ? }
展示圖片
以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
Javascript 中創(chuàng)建自定義對象的方法匯總
這篇文章主要匯總介紹了Javascript 中創(chuàng)建自定義對象的方法,需要的朋友可以參考下2014-12-12深入解析JS實(shí)現(xiàn)3D標(biāo)簽云的原理與方法
這篇文章主要介紹了深入解析JS實(shí)現(xiàn)3D標(biāo)簽云的原理與方法,結(jié)合實(shí)例形式詳細(xì)分析了3D標(biāo)簽云原理、實(shí)現(xiàn)技巧與相關(guān)操作注意事項(xiàng),需要的朋友可以參考下2019-08-08JavaScript高階API數(shù)組reduce函數(shù)使用示例
這篇文章主要為大家介紹了JavaScript數(shù)組高階API?reduce函數(shù)使用示例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2022-11-11js實(shí)現(xiàn)簡單的日歷顯示效果函數(shù)示例
這篇文章主要介紹了js實(shí)現(xiàn)簡單的日歷顯示效果函數(shù),結(jié)合完整實(shí)例形式分析了JavaScript實(shí)現(xiàn)的日歷功能相關(guān)原理與具體操作技巧,需要的朋友可以參考下2019-11-11