效果圖
實現思路
把屏幕當成一個固定的盒子,然後把盒子分成兩邊,並讓盒子的每一邊都能夠滾動。
源碼
index.wxml
<!--主盒子-->
<view class="container">
<!--左側欄-->
<view class="nav_left">
<block wx:for="{{title}}" wx:for-item="item" wx:key="{{index}}">
<!-- {{curNav == item.id ? 'active' : ''}} -->
<!-- 三目運算符是用來給當前選中的目錄修改樣式用的 -->
<!-- switchRightTab 函數是用來實現數據的渲染 -->
<!-- 當用戶點擊道不同的側邊欄目錄時候,根據 data-id 去從數據庫獲取新的數據,渲染到左側,並且修改 curNav 的值,使新樣式添加到點擊的目錄上,具體 js 自己實現 -->
<view class="nav_left_items {{curNav == item.id ? 'active' : ''}}" bindtap="switchRightTab" data-id="{{item.id}}">{{item.name}}</view>
</block>
</view>
<!--右側欄-->
<view class="nav_right">
<view class="nav_right_items">
<block wx:for="{{content}}" wx:for-item="item" wx:key="{{index}}">
<view>
<text>{{item.name}}</text>
</view>
</block>
</view>
</view>
</view>
index.wxss
page {
background: #f5f5f5;
}
/*總體主盒子*/
.container {
display: flex;
direction: row;
}
/*左側欄主盒子*/
.nav_left {
/*設置行內塊級元素(沒使用定位)*/
position: absolute;
display: inline-block;
width: 35%;
height: 100%;
/*主盒子設置背景色爲灰色*/
background: #f5f5f5;
text-align: center;
overflow: scroll;
}
/*左側欄list的item*/
.nav_left .nav_left_items {
/*每個高30px*/
height: 30px;
/*垂直居中*/
line-height: 30px;
/*再設上下padding增加高度,總高42px*/
padding: 6px 0;
/*只設下邊線*/
border-bottom: 1px solid #dedede;
/*文字14px*/
font-size: 20px;
}
/*左側欄list的item被選中時*/
.nav_left .nav_left_items.active {
/*背景色變成白色*/
background: #fff;
color: red;
}
/*右側欄主盒子*/
.nav_right {
/*右側盒子使用了絕對定位*/
position: absolute;
top: 0;
right: 0;
flex: 1;
/*寬度75%,高度佔滿,並使用百分比佈局*/
width: 65%;
height: 100%;
padding: 10px;
box-sizing: border-box;
background: #fff;
overflow: scroll;
}
.nav_right .nav_right_items {
}
.nav_right .nav_right_items text {
/*給text設成塊級元素*/
display: block;
margin-bottom: 25px;
font-size: 20px;
/*設置文字居中*/
text-align: left;
/*設置文字溢出部分爲...*/
overflow: hidden;
white-space: nowrap;
text-overflow: ellipsis;
}
index.js
Page({
data: {
curNav: 0,
title:[
{
"id":0,
"name":"技術"
},
{
"id": 1,
"name": "技術"
},
{
"id": 1,
"name": "技術"
},
{
"id": 1,
"name": "技術"
},
{
"id": 1,
"name": "技術"
},
{
"id": 1,
"name": "技術"
},
{
"id": 1,
"name": "技術"
},
{
"id": 1,
"name": "技術"
},
{
"id": 1,
"name": "技術"
},
{
"id": 1,
"name": "技術"
},
{
"id": 1,
"name": "技術"
},
{
"id": 1,
"name": "技術"
},
{
"id": 1,
"name": "技術"
},
{
"id": 1,
"name": "技術"
}
],
content:[
{
id:2,
"name":"軟件工程師"
},
{
id: 2,
"name": "軟件工程師"
},
{
id: 2,
"name": "軟件工程師"
},
{
id: 2,
"name": "軟件工程師"
},
{
id: 2,
"name": "軟件工程師"
},
{
id: 2,
"name": "軟件工程師"
},
{
id: 2,
"name": "軟件工程師"
},
{
id: 2,
"name": "軟件工程師"
},
{
id: 2,
"name": "軟件工程師"
},
{
id: 2,
"name": "軟件工程師"
},
{
id: 2,
"name": "軟件工程師"
},
{
id: 2,
"name": "軟件工程師"
}
]
}
})