微信小程序?qū)崿F(xiàn)跟隨菜單效果和循環(huán)嵌套加載數(shù)據(jù)
本文實(shí)例為大家分享了微信小程序?qū)崿F(xiàn)跟隨菜單效果、微信小程序循環(huán)嵌套加載數(shù)據(jù),供大家參考,具體內(nèi)容如下
效果如圖:
代碼如下:
wxml
//使用循環(huán)嵌套data數(shù)據(jù)格式寫對(duì)即可 <scroll-view class="left" scroll-y> <view wx:for="{{left}}" class="leftlist {{index==_click?'yes':''}}" data-i="{{index}}" bindtap="tap"> {{item.txt}} </view> </scroll-view> <scroll-view class="right" scroll-y bindscroll="scroll" scroll-into-view="{{toView}}"> <view id="{{item.id}}" wx:for="{{right}}"> <view class="title"> <text class="line"></text> {{item.txt}} <text class="line"></text> </view> <view class="li" wx:for="{{item.li}}"> <image src="{{item.src}}"></image> <text class="name">{{item.name}}</text> </view> </view> </scroll-view>
js
Page({ data: { toView: 'red1', _click:0, left: [{ txt: '新品', id: 'new' }, { txt: '手機(jī)', id: 'phone' }, { txt: '電視', id: 'mv' }, { txt: '電腦', id: 'computer' }], right: [ { txt: '新品', id: 'new',li: [{ src: '../../assets/images/max1.jpg', name: '小米noto' }, { src: '../../assets/images/max1.jpg', name: '小米mix' }, { src: '../../assets/images/max3.jpg', name: '小米5c' }, { src: '../../assets/images/max2.jpg', name: '小米notp' }, { src: '../../assets/images/max2.jpg', name: '小米note5' }, { src: '../../assets/images/max2.jpg', name: '小米6' }]}, { txt: '手機(jī)', id: 'phone',li: [{ src: '../../assets/images/max2.jpg', name: '小米6s' }, { src: '../../assets/images/max3.jpg', name: '小米max' }, { src: '../../assets/images/max2.jpg', name: '小米5s' }, { src: '../../assets/images/max1.jpg', name: '小米li' }, { src: '../../assets/images/max3.jpg', name: '小米4' }, { src: '../../assets/images/max1.jpg', name: '小米max' }]}, { txt: '電視', id: 'mv', li: [{ src: '../../assets/images/max3.jpg', name: '小米6' }, { src: '../../assets/images/max2.jpg', name: '小米mix' }, { src: '../../assets/images/max1.jpg', name: '小米7s' }, { src: '../../assets/images/max3.jpg', name: '小米2' }, { src: '../../assets/images/max1.jpg', name: '小米note7' }, { src: '../../assets/images/max3.jpg', name: '小米8' }] }, { txt: '電腦', id: 'computer', li: [{ src: '../../assets/images/max1.jpg', name: '小米2' }, { src: '../../assets/images/max1.jpg', name: '小米mix' }, { src: '../../assets/images/max2.jpg', name: '小米max' }, { src: '../../assets/images/max1.jpg', name: '小米6' }, { src: '../../assets/images/max3.jpg', name: '小米note' }, { src: '../../assets/images/max1.jpg', name: '小米max' }] }] }, scroll: function (e) { console.log(e)//右側(cè)列表滑動(dòng)-左側(cè)列表名稱樣式跟著改變,然而我不會(huì)寫,擱置中,誰(shuí)會(huì)告訴我,謝謝! }, tap: function (e) { var j = parseInt(e.currentTarget.dataset.i); this.setData({ toView: this.data.left[j].id,//控制視圖滾動(dòng)到為此id的<view> _click:j //控制左側(cè)點(diǎn)擊后樣式 }) }, })
wxss
page{border-top:1px solid #f6f6f6;} .left{ height:100%; width: 19%; display: inline-block; background:#fff; text-align:center; border-right:1px solid #eee; } .leftlist{ font-size:12px; padding:10px; } .right{ height:100%; width: 80%; display: inline-block; background:#fff; text-align:center; } .line{ width:15px; height:1px; background:#ddd; display:inline-block; vertical-align:super; margin:0 15px; } .li{ height:10%; width:30%; display:inline-block; text-align:center; } .li image{width:60px;height:60px;} .li .name{ font-size:12px; display:block; color:#888; overflow:hidden; white-space:nowrap; text-overflow:ellipsis; } .title{padding:20px 0;} .yes{color: #f99;font-size: 14px;}
友情提示:
1、左側(cè)點(diǎn)擊樣式改變:
利用自身index與點(diǎn)擊的元素的index比較。
data-i=“{{獲取當(dāng)前index傳給_click保存}}”,
class=”leftlist {{index==_click?'yes':”}}”,
此處index是自身的,如果自身和點(diǎn)擊的一致就添加,yes類名,否側(cè)滯空清除yes樣式。
2、點(diǎn)擊左側(cè),右側(cè)跟隨:
利用scroll-into-view=”{{id}}”, 視圖會(huì)滾動(dòng)到id為此id的view標(biāo)簽。我是直接從data數(shù)據(jù)里取得id,也可以直接獲取點(diǎn)擊元素id。
3、循環(huán)嵌套:data數(shù)據(jù)格式寫對(duì),按照官方文檔就行。
相關(guān)文章
javascript中的有名函數(shù)和無(wú)名函數(shù)
javascript中的有名函數(shù)和無(wú)名函數(shù)...2007-10-10如何實(shí)現(xiàn)iframe(嵌入式幀)的自適應(yīng)高度
[紅色]如何實(shí)現(xiàn)iframe(嵌入式幀)的自適應(yīng)高度...2006-07-07webpack5?import動(dòng)態(tài)導(dǎo)入實(shí)現(xiàn)按需加載并給文件統(tǒng)一命名的配置方法
這篇文章主要介紹了webpack5?import動(dòng)態(tài)導(dǎo)入實(shí)現(xiàn)按需加載并給文件統(tǒng)一命名的配置,本文給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2022-11-11js基于setTimeout與setInterval實(shí)現(xiàn)多線程
這篇文章主要介紹了js基于setTimeout與setInterval實(shí)現(xiàn)多線程的方法,分析了多線程的原理與javascript模擬實(shí)現(xiàn)多線程的相關(guān)技巧,需要的朋友可以參考下2016-06-06Bootstrap框架結(jié)合jQuery仿百度換膚功能實(shí)例解析
這篇文章主要為大家詳細(xì)介紹了Bootstrap框架結(jié)合jQuery仿百度換膚功能實(shí)現(xiàn)代碼解析,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2016-09-09JS實(shí)現(xiàn)注冊(cè)界面表單校驗(yàn)
這篇文章主要為大家詳細(xì)介紹了JS實(shí)現(xiàn)注冊(cè)界面表單校驗(yàn),文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2022-08-08javascript權(quán)威指南 學(xué)習(xí)筆記之變量作用域分享
最近一直在看《javascript權(quán)威指南 第五版》,變量作用域這一章,看得真的有點(diǎn)累。不過,收獲還是多多。2011-09-09