您好,登錄后才能下訂單哦!
今天小編給大家分享一下微信小程序如何實(shí)現(xiàn)菜單左右聯(lián)動(dòng)效果的相關(guān)知識(shí)點(diǎn),內(nèi)容詳細(xì),邏輯清晰,相信大部分人都還太了解這方面的知識(shí),所以分享這篇文章給大家參考一下,希望大家閱讀完這篇文章后有所收獲,下面我們一起來了解一下吧。
首先是獲取數(shù)據(jù),并且獲取數(shù)據(jù)的長(zhǎng)度(需要根據(jù)長(zhǎng)度來計(jì)算元素的高度),通過遍歷數(shù)據(jù)的內(nèi)容通過題目和元素個(gè)數(shù)的相加得到高度,當(dāng)消失高度小于等于某個(gè)元素的高度就設(shè)定索引值給左邊的菜單目錄實(shí)現(xiàn)右邊滑動(dòng)左邊聯(lián)動(dòng),左邊的菜單點(diǎn)擊事件聯(lián)動(dòng)比較簡(jiǎ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,//左邊對(duì)應(yīng)的索引 rightViewId: '',//點(diǎn)擊事件右邊的索引 } getScroll(e) {//微信小程序中綁定滑動(dòng)函數(shù),每滑動(dòng)一下都會(huì)觸發(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ù)對(duì)應(yīng)展示商品的json h += 38 + parseInt(dishSize / 3 * 80);//獲取當(dāng)前元素的高度,38是標(biāo)題高度,80是元素高度 if (top <= h) {//滿足條件立刻停止循環(huán),就會(huì)一直停留再當(dāng)前索引,不滿足當(dāng)前就會(huì)自動(dòng)到下一個(gè)菜單 _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 }) }
展示圖片
以上就是“微信小程序如何實(shí)現(xiàn)菜單左右聯(lián)動(dòng)效果”這篇文章的所有內(nèi)容,感謝各位的閱讀!相信大家閱讀完這篇文章都有很大的收獲,小編每天都會(huì)為大家更新不同的知識(shí),如果還想學(xué)習(xí)更多的知識(shí),請(qǐng)關(guān)注億速云行業(yè)資訊頻道。
免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。