uniapp(vue)实现点击左侧菜单,右侧显示对应的内容

<template>
<view class="container">
<view class="fication-search">
<input type="text" value="" placeholder="请输入您要搜索的内容"/><image class="search-icon" src="../../static/images/search.png" mode=""></image>
</view>
<!-- 滚动区域 -->
<view class="scroll-panel" id="scroll-panel">
<view class="list-box">
<view class="left">
<scroll-view scroll-y="true" :style="{ height: scrollHeight + 'px' }" :scroll-into-view="leftIntoView">
<view
class="item"
v-for="(item, index) in leftArray"
:key="index"
:class="{ active: index == leftIndex }"
:id="'left-' + index"
:data-index="index"
@tap="leftTap"
>
<view class="activelink"></view>
<text class="item-name">{{ item }}</text>
</view>
</scroll-view>
</view>
<view class="main">
<scroll-view scroll-y="true" :style="{ height: scrollHeight + 'px' }" @scroll="mainScroll" :scroll-into-view="scrollInto" scroll-with-animation="true">
<view class="item main-item" v-for="(item, index) in mainArray" :key="index" :id="'item-' + index">
<view class="title">
<view>{{ item.title }}</view>
</view>
<view class="goods" v-for="(item2, index2) in item.list" :key="index2">
<view class="orderlist-list">
<view class="list-left">
<image src="../../static/images/store_bg.png" mode=""></image>
</view>
<view class="list-right">
<view class="list-name">香辣火锅</view>
<view class="list-ping">
<u-rate :count="count" active-color="#FFB800" inactive-color="#E0E0E0" size='32' v-model="value"></u-rate>
<text>{{4.8}}分</text>
</view>
<view class="list-meuns"><text>销量{{99}}+</text><text>配送费¥{{3}}</text><text>距离{{1.2}}km</text></view>
</view>
</view>
</view>
</view>
<view class="fill-last" :style="{ height: fillHeight + 'px' }"></view>
</scroll-view>
</view>
</view>
</view>
</view> </template>
<script>
export default{
data() {
return {
scrollHeight: 400,
scrollTopSize: 0,
fillHeight: 0, // 填充高度,用于最后一项低于滚动区域时使用
leftArray: [],
mainArray: [],
topArr: [],
leftIndex: 0,
scrollInto: '',
count: 5,
value: 4
};
},
computed: {
/* 计算左侧滚动位置定位 */
leftIntoView() {
return `left-${this.leftIndex > 3 ? this.leftIndex - 3 : 0}`;
}
},
mounted() {
/* 等待DOM挂载完成 */
this.$nextTick(() => {
/* 在非H5平台,nextTick回调后有概率获取到错误的元素高度,则添加200ms的延迟来减少BUG的产生 */
setTimeout(() => {
/* 等待滚动区域初始化完成 */
this.initScrollView().then(() => {
/* 获取列表数据,你的代码从此处开始 */
this.getListData();
});
}, 200);
});
},
methods: {
/* 初始化滚动区域 */
initScrollView() {
return new Promise((resolve, reject) => {
let view = uni.createSelectorQuery().select('#scroll-panel');
view.boundingClientRect(res => {
this.scrollTopSize = res.top;
this.scrollHeight = res.height;
this.$nextTick(() => {
resolve();
});
}).exec();
});
},
/* 获取列表数据 */
getListData() {
// Promise 为 ES6 新增的API ,有疑问的请自行学习该方法的使用。
new Promise((resolve, reject) => {
/* 因无真实数据,当前方法模拟数据。正式项目中将此处替换为 数据请求即可 */
uni.showLoading();
setTimeout(() => {
let [left, main] = [[], []]; for (let i = 0; i < 12; i++) {
left.push(`${i + 1}类商品`); let list = [];
let r = Math.floor(Math.random() * 10);
r = r < 1 ? 3 : r;
for (let j = 0; j < r; j++) {
list.push(j);
}
main.push({
title: `第${i + 1}类商品标题`,
list
});
} // 将请求接口返回的数据传递给 Promise 对象的 then 函数。
resolve({ left, main });
}, 1000);
}).then(res => {
console.log('-----------请求接口返回数据示例-------------');
console.log(res); uni.hideLoading();
this.leftArray = res.left;
this.mainArray = res.main; // DOM 挂载后 再调用 getElementTop 获取高度的方法。
this.$nextTick(() => {
this.getElementTop();
});
});
},
/* 获取元素顶部信息 */
getElementTop() {
new Promise((resolve, reject) => {
let view = uni.createSelectorQuery().selectAll('.main-item');
view.boundingClientRect(data => {
resolve(data);
}).exec();
}).then(res => {
let topArr = res.map(item => {
return item.top - this.scrollTopSize; /* 减去滚动容器距离顶部的距离 */
});
this.topArr = topArr; /* 获取最后一项的高度,设置填充高度。判断和填充时做了 +-20 的操作,是为了滚动时更好的定位 */
let last = res[res.length - 1].height;
if (last - 20 < this.scrollHeight) {
this.fillHeight = this.scrollHeight - last + 20;
}
});
},
/* 主区域滚动监听 */
mainScroll(e) {
let top = e.detail.scrollTop;
let index = 0;
/* 查找当前滚动距离 */
for (let i = this.topArr.length - 1; i >= 0; i--) {
/* 在部分安卓设备上,因手机逻辑分辨率与rpx单位计算不是整数,滚动距离与有误差,增加2px来完善该问题 */
if (top + 2 >= this.topArr[i]) {
index = i;
break;
}
}
this.leftIndex = index < 0 ? 0 : index;
},
/* 左侧导航点击 */
leftTap(e) {
let index = e.currentTarget.dataset.index;
this.scrollInto = `item-${index}`;
}
}
};
</script>
<style lang="scss"> page,
.container {
width: 100vw;
height: 100%;
}
/* 容器 */
.container {
display: flex;
flex-direction: column;
flex-wrap: nowrap;
justify-content: flex-start;
align-items: flex-start;
align-content: flex-start; // & > view {
// width: 100%;
// } .scroll-panel {
flex-grow: 1;
height: 0;
overflow: hidden;
} }
.fication-search{
width: 686rpx;
height: 64rpx;
margin: 12rpx auto;
background: #F5F6F7;
border-radius: 32rpx 32rpx 32rpx 32rpx;
display: flex;
flex-direction: row;
align-items: center;
justify-content: space-between;
}
.fication-search input{
padding-left: 24rpx;
font-size: 12px;
font-family: PingFang SC-Regular, PingFang SC;
font-weight: 400;
color: #B5B5B5;
}
.fication-search input::-webkit-input-placeholder{
color:red!important;
} .search-icon{
width: 24rpx;
height: 24rpx;
margin-right: 30rpx;
}
.list-box {
display: flex;
flex-direction: row;
flex-wrap: nowrap;
justify-content: flex-start;
align-items: flex-start;
align-content: flex-start;
font-size: 28rpx; .left {
width: 196rpx;
background: #F5F6F7;
line-height: 112rpx;
box-sizing: border-box;
font-size: 28rpx;
font-family: PingFang SC-Medium, PingFang SC; .item { position: relative;
display: flex; &:not(:first-child) {
margin-top: 1px; &::after {
content: '';
display: block;
height: 0;
// border-top: #d6d6d6 solid 1px;
width: 620upx;
position: absolute;
top: 0px;
right: 0;
transform: scaleY(0.5); /* 1px像素 */
}
}
.item-name{
padding-left: 32rpx;
}
&.active {
color: #F7433D;
background-color: #fff;
}
&.active .activelink{
width: 8rpx;
height: 48rpx;
margin-top: 32rpx;
padding-left: 0;
background-color: #F7433D;
border-radius: 2px 2px 2px 2px;
}
}
.fill-last {
height: 0;
width: 100%;
background: none;
}
} .title {
line-height: 64rpx;
font-size: 16px;
font-family: PingFang SC-Bold, PingFang SC;
font-weight: bold;
color: #000000;
padding: 8rpx 0;
background-color: #fff;
position: sticky;
top: 0;
z-index: 19;
} }
.orderlist-list{
width: 526rpx;
height: 200rpx;
background: #FFFFFF;
margin: 0 auto;
display: flex;
border-bottom: 2rpx solid #F5F6F7;
// border-radius: 8px 8px 8px 8px;
}
.list-left{
margin: 24rpx;
width: 152rpx;
height: 152rpx;
border-radius: 4px 4px 4px 4px;
overflow: hidden;
}
.list-left image{
width: 100%;
height: 100%;
}
.list-right{
flex: 1;
display: flex;
flex-direction: column;
}
.list-name{
margin-top: 24rpx;
font-size: 16px;
font-family: PingFang SC-Medium, PingFang SC;
font-weight: 500;
color: #000000;
line-height: 19px;
}
.list-ping{
margin: 20rpx 0;
display: flex;
font-size: 12px;
font-family: PingFang SC-Regular, PingFang SC;
font-weight: 400;
color: #FFB800; }
.list-ping text{
margin-left: 6rpx;
}
.list-meuns{
margin-bottom: 28rpx;
font-size: 12px;
font-family: PingFang SC-Medium, PingFang SC;
font-weight: 500;
color: #999999;
}
</style>
uniapp(vue)实现点击左侧菜单,右侧显示对应的内容的更多相关文章
- uniapp 微信小程序 实现左侧菜单右侧列表,双向联动的效果
<template> <view class="u-wrap"> <view class="u-search-box"> & ...
- C# 窗体 类似framest 左侧点击右侧显示 左侧菜单右侧显示
首先托一个splitContainer调节大小位置 然后进行再新创建一个窗体名为add 在左侧拖入button按钮 进入代码阶段 更改属性 public Main() { InitializeComp ...
- 用JavaScript实现点击左侧列表右侧显示列表内容的方法
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...
- ajax实现简单的点击左侧菜单,右侧加载不同网页
实现:ajax实现点击左侧菜单,右侧加载不同网页(在整个页面无刷新的情况下实现右侧局部刷新,用到ajax注意需要在服务器环境下运行,从HBuilder自带的服务器中打开浏览效果即可) 原理:ajax的 ...
- C# WPF 左侧菜单右侧内容布局效果实现
原文:C# WPF 左侧菜单右侧内容布局效果实现 我们要做的效果是这样的,左侧是可折叠的菜单栏,右侧是内容区域,点击左侧的菜单项右侧内容区域则相应地切换. wpf实现的话,我的办法是用一个tabcon ...
- 权限模块_使用权限_实现主页面的效果_显示左侧菜单&只显示有权限的菜单项
权限模块__使用权限__实现主页面的效果 HomeAction.java public class HomeAction extends ActionSupport { public String i ...
- vue实现点击、滑动右侧字母对应各个城市
1.字母组件给父组件传递当前点击的字母值 @click="handleLetterClick" //绑定事件 handleLetterClick (e) { //向上传递参数 th ...
- 关于ubuntu16.4 中安装最新的eclipse或者是STS出现页面特卡,且新建项目没有提示,preference选项中点击左侧标签右侧没反应的解决办法,参照google, 排版不太好,希望对一些小伙伴有所帮助
up vote21down votefavorite 12 Eclipse was working as good as anything on 14.04. I did a clean instal ...
- 当chm文档点击左侧,右侧无内容时的解决方案
右击chm文件->属性->安全选项卡,选择你登陆计算机的用户名,把权限改成完全控制就可以显示了
随机推荐
- SpringBoot课程学习(四)
一.profile的多文档配置方式 1.profile文件方式:提供多个配置文件,每个代表一种环境 如: 1.application-dev.properties/yml 开发环境 2.applica ...
- 适用于移动端、PC 端 Vue.js 图片预览插件
1.安装:npm install --save vue-picture-preview 2.使用: (1)入口文件中main.js中全局引入: import Vue from 'vue' import ...
- 魔改xxl-job,彻底告别手动配置任务!
原创:微信公众号 码农参上,欢迎分享,转载请保留出处. 哈喽大家好啊,我是Hydra. xxl-job是一款非常优秀的任务调度中间件,轻量级.使用简单.支持分布式等优点,让它广泛应用在我们的项目中,解 ...
- 19.MongoDB系列之批量更新写入Groovy版
Groovy作为脚本,比Java在数据处理中具有更高的灵活性 // 获取mongo连接略 .... def count = 0 for(Township town : townships) { Doc ...
- PHP配置负载均衡
我项目是用宝塔面板.所以这次用宝塔面板演示. 环境: LNMP 代码:2套.2套代码除了配置其他都是一样 域名:1个.2级域名.其实一级二级都没关系 /************************ ...
- JavaBean组件<jsp:forward>动作<jsp:param>动作登录页面输入用户名和密码,然后进入检查页面判断是否符合要求,符合要求跳转到成功界面,不符合要求返回登录界面,显示错误信息。
JavaBean组件 JavaBean组件实际是一种java类.通过封装属性和方法成为具有某种功能或者处理某个业务的对象. 特点:1.实现代码的重复利用.2.容易编写和维护.3.jsp页面调用方便. ...
- python关于Django搭建简单博客项目 详解二-setting.py
这一篇我们来讲解setting.py,具体内容以注释形式写入到下面的setting.py代码中,篇幅所限已把官方所给英文注释删除. 全部源代码和详解请参看http://github.com/Cheng ...
- 题解 UVA439 骑士的移动 Knight Moves
前言 最近板子题刷多了-- 题意 一个 \(8\times 8\) 的棋盘,问马从起点到终点的最短步数为多少. \(\sf Solution\) 要求最短路径嘛,显然 bfs 更优. 读入 这个读入处 ...
- 初识vue3.0
vue3.0 源码组织方式的变化 采用ts重写 独立的功能模块提取到单独的包中 90%的api兼容Vue2.x Composition API 组合api 解决vue2.x options api 开 ...
- Spring三级缓存解决循环依赖
前提知识 1.解决循环依赖的核心依据:实例化和初始化步骤是分开执行的 2.实现方式:三级缓存 3.lambda表达式的延迟执行特性 spring源码执行逻辑 核心方法refresh(), popula ...