先前看到网上不少大神写的demo,其菜单栏主要以 A,B,C,D等字母为主,即A,B,C,D等字母为对应该项携带的 id(id不能为汉字或纯数字)。而笔者现在写的项目菜单栏为汉字,所以需要改变数据格式,进而需要改变 wxml 中的循环嵌套和获取。以下为成型后效果,希望对读者有帮助。
实现该功能的思路:通过点击左侧滑栏的某一项,获取到该元素携带的 id ,然后动态传给右侧滑栏的 scroll-into-view ,从而实现右侧滑栏对应的该元素运动置顶。
以下为完整数据
数据格式:
var list = {
"List": [
{
'A': [
{ name: '白酒' },
{ 'picture': '../../img/55ac9689Ncc876cf1.jpg', 'desc': '葡萄酒' },
{ 'picture': '../../img/56668c04N5cb325b7.jpg', 'desc': '洋酒' },
{ 'picture': '../../img/586e055eNf678fd52.png', 'desc': '汾酒' },
{ 'picture': '../../img/596d6f4eNb62c24c1.jpg', 'desc': '汾酒' }
],
'B': [
{ name: '白酒1' },
{ 'picture': '../../img/55ac9689Ncc876cf1.jpg', 'desc': '葡萄酒' },
{ 'picture': '../../img/56668c04N5cb325b7.jpg', 'desc': '洋酒' },
{ 'picture': '../../img/586e055eNf678fd52.png', 'desc': '汾酒' },
{ 'picture': '../../img/596d6f4eNb62c24c1.jpg', 'desc': '汾酒' }
],
'C': [
{ name: '白酒2' },
{ 'picture': '../../img/55ac9689Ncc876cf1.jpg', 'desc': '葡萄酒' },
{ 'picture': '../../img/56668c04N5cb325b7.jpg', 'desc': '洋酒' },
{ 'picture': '../../img/586e055eNf678fd52.png', 'desc': '汾酒' },
{ 'picture': '../../img/596d6f4eNb62c24c1.jpg', 'desc': '汾酒' }
],
'D': [
{ name: '白酒3' },
{ 'picture': '../../img/55ac9689Ncc876cf1.jpg', 'desc': '葡萄酒' },
{ 'picture': '../../img/56668c04N5cb325b7.jpg', 'desc': '洋酒' },
{ 'picture': '../../img/586e055eNf678fd52.png', 'desc': '汾酒' },
{ 'picture': '../../img/596d6f4eNb62c24c1.jpg', 'desc': '汾酒' }
],
'E': [
{ name: '白酒4' },
{ 'picture': '../../img/55ac9689Ncc876cf1.jpg', 'desc': '葡萄酒' },
{ 'picture': '../../img/56668c04N5cb325b7.jpg', 'desc': '洋酒' },
{ 'picture': '../../img/586e055eNf678fd52.png', 'desc': '汾酒' },
{ 'picture': '../../img/596d6f4eNb62c24c1.jpg', 'desc': '汾酒' }
],
'F': [
{ name: '白酒5' },
{ 'picture': '../../img/55ac9689Ncc876cf1.jpg', 'desc': '葡萄酒' },
{ 'picture': '../../img/56668c04N5cb325b7.jpg', 'desc': '洋酒' },
{ 'picture': '../../img/586e055eNf678fd52.png', 'desc': '汾酒' },
{ 'picture': '../../img/596d6f4eNb62c24c1.jpg', 'desc': '汾酒' }
],
'G': [
{ name: '白酒6' },
{ 'picture': '../../img/55ac9689Ncc876cf1.jpg', 'desc': '葡萄酒' },
{ 'picture': '../../img/56668c04N5cb325b7.jpg', 'desc': '洋酒' },
{ 'picture': '../../img/586e055eNf678fd52.png', 'desc': '汾酒' },
{ 'picture': '../../img/596d6f4eNb62c24c1.jpg', 'desc': '汾酒' }
],
'H': [
{ name: '白酒7' },
{ 'picture': '../../img/55ac9689Ncc876cf1.jpg', 'desc': '葡萄酒' },
{ 'picture': '../../img/56668c04N5cb325b7.jpg', 'desc': '洋酒' },
{ 'picture': '../../img/586e055eNf678fd52.png', 'desc': '汾酒' },
{ 'picture': '../../img/596d6f4eNb62c24c1.jpg', 'desc': '汾酒' }
],
'I': [
{ name: '白酒8' },
{ 'picture': '../../img/55ac9689Ncc876cf1.jpg', 'desc': '葡萄酒' },
{ 'picture': '../../img/56668c04N5cb325b7.jpg', 'desc': '洋酒' },
{ 'picture': '../../img/586e055eNf678fd52.png', 'desc': '汾酒' },
{ 'picture': '../../img/596d6f4eNb62c24c1.jpg', 'desc': '汾酒' }
],
'J': [
{ name: '白酒9' },
{ 'picture': '../../img/55ac9689Ncc876cf1.jpg', 'desc': '葡萄酒' },
{ 'picture': '../../img/56668c04N5cb325b7.jpg', 'desc': '洋酒' },
{ 'picture': '../../img/586e055eNf678fd52.png', 'desc': '汾酒' },
{ 'picture': '../../img/596d6f4eNb62c24c1.jpg', 'desc': '汾酒' }
],
'K': [
{ name: '白酒10' },
{ 'picture': '../../img/55ac9689Ncc876cf1.jpg', 'desc': '葡萄酒' },
{ 'picture': '../../img/56668c04N5cb325b7.jpg', 'desc': '洋酒' },
{ 'picture': '../../img/586e055eNf678fd52.png', 'desc': '汾酒' },
{ 'picture': '../../img/596d6f4eNb62c24c1.jpg', 'desc': '汾酒' }
],
'L': [
{ name: '白酒11' },
{ 'picture': '../../img/55ac9689Ncc876cf1.jpg', 'desc': '葡萄酒' },
{ 'picture': '../../img/56668c04N5cb325b7.jpg', 'desc': '洋酒' },
{ 'picture': '../../img/586e055eNf678fd52.png', 'desc': '汾酒' },
{ 'picture': '../../img/596d6f4eNb62c24c1.jpg', 'desc': '汾酒' }
],
'M': [
{ name: '白酒12' },
{ 'picture': '../../img/55ac9689Ncc876cf1.jpg', 'desc': '葡萄酒' },
{ 'picture': '../../img/56668c04N5cb325b7.jpg', 'desc': '洋酒' },
{ 'picture': '../../img/586e055eNf678fd52.png', 'desc': '汾酒' },
{ 'picture': '../../img/596d6f4eNb62c24c1.jpg', 'desc': '汾酒' }
]
}
],
}
module.exports = list;wxml内容:
{{itemName[0].name}} {{itemName[0].name}} 0}}"> {{item.desc}}
js内容:
// pages/list-1/list-1.js
var list = require('../../utils/list.js')
Page({
data: {
// 左侧点击类样式
curNav: 'A',
},
onReady: function () {
// 生命周期函数--监听页面初次渲染完成
var listChild1 = list.List[0];
var that = this;
// 获取可视区高度
wx.getSystemInfo({
success: function (res) {
that.setData({
list: listChild1,
winHeight: res.windowHeight,
})
}
})
},
//点击左侧 tab ,右侧列表相应位置联动 置顶
switchRightTab: function (e) {
var id = e.target.id;
console.log(typeof id)
this.setData({
// 动态把获取到的 id 传给 scrollTopId
scrollTopId: id,
// 左侧点击类样式
curNav:id
})
}
})wxss代码 (样式可能会不全,需要引入weui.wxss文件)
.container {
position: relative;
width: 100%;
height: 1220rpx;
background-color: #f0f4f7;
color: #939393;
}
.nav_left{
display: inline-block;
width: 100%;
height: 100%;
background: #fff;
text-align: center;
left: 0;
top: 0;
border-top: 1rpx solid #dedede;
}
.nav_left .nav_left_items{
background: #fff;
height: 80rpx;
line-height: 80rpx;
padding: 15rpx 0;
border-bottom: 1px solid #dedede;
font-size: 29rpx;
color: #101010;
font-weight:
}
.nav_left .nav_left_items.active{
background: #f0f4f7;
color: #ed1000;
}
.scroll_right{
position: fixed;
top: 0;
right: 0;
overflow: auto;
flex: 1;
width: 75%;
height: 100%;
padding: 20rpx;
box-sizing: border-box;
background-color: #f0f4f7;
border-top: 1rpx solid #dedede;
}
.mink::after{
display:block;content:'';clear:both;
}
.jiul,.jiul image{
width: 100%;
height: 170rpx;
}
.minl{
font-size: 29rpx;
color: #777;
text-align: left;
line-height: 60rpx;
float: left;
background: #f0f4f7;
width: 100%;
}
.mink{
width: 100%;
background: #fff;
height: 100%;
}
.nav_right_items{
float: left;
width: 50%;
text-align: center;
color: #4a4a4a;
background: #fff;
}
.nav_right_items image{
width: 60px;
height: 50px;
margin-top: 15rpx;
}
.nav_right_items text{
display: block;
margin-top: 5rpx;
margin-bottom: 10rpx;
font-size: 26rpx;
overflow: hidden;
white-space: nowrap;
text-overflow: ellipsis;
}
.other-navigator-hover{
background:#fff;
}
.scroll_left{
width:25%;
height:100%;
background:#fff;
text-align:center;
position: fixed;
left: 0;top: 0
}下节更新《微信小程序-商品列表右=>左联动(二)》
感谢翻阅,如有疑问或想交流技术,请留言!



