uni-app商城(四)分类页面

1.分类页面代码

<template>
	<view>
		<!-- 自定义搜索组件 -->
		<my-search @click="gotoSearch"></my-search>
		<view class="scroll-view-container">
			<!-- 左侧滑动区域 -->
			<scroll-view class="left-scroll-view" scroll-y="true" :style="{height: windowHeight+'px'}" :scroll-top="scrollTop">
				<block v-for="(item,i) in cateList" :key="i">
					<view :class="['left-scroll-item',i===active?'active':'']" @click="activeChanged(i)">
						{{item.cateName}}
					</view>
				</block>
			</scroll-view>
			<!-- 右侧滑动区域 -->
			<scroll-view scroll-y="true" :style="{height: windowHeight+'px'}">
				<view class="cate-lv2" v-for="(item2,i2) in cataLevel2" :key="i2">
					<!-- 二级分类标题 -->
					<view class="cate-lv2-title">/ {{item2.cateName}} /</view>
					<!-- 二级分类下的三级分类 -->
					<view class="cate-lv3-list">
						<!-- 三级分类的item -->
						<view class="cate-lv3-item" v-for="(item3,i3) in item2.children" :key="i3" @click="gotoGoodsList(item3)">
							<!-- 三级分类的图片 -->
							<image :src="item3.cataIcon"></image>
							<!--三级分类的文本 -->
							<text>{{item3.cateName}}</text>
						</view>
					</view>
				</view>
			</scroll-view>
		</view>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				//当前设备可用高度
				windowHeight: 0,
				//分类列表
				cateList: [],
				active: 0,
				//二级分类列表
				cataLevel2: [],
				scrollTop:0
			};
		},
		onLoad() {
			const sysInfo = uni.getSystemInfoSync()
			this.windowHeight = sysInfo.windowHeight-50
			this.getCateList()
		},
		methods: {
			getCateList() {
				//获取分类列表
				uni.request({
						url: uni.$baseUrl + '/api/Cate/GetCateList',
					})
					.then((res) => {
						// console.log(res.data.data);
						if (res.data.code != 200) return uni.$showMsg();
						this.cateList = res.data.data;
						//二级分类赋值
						this.cataLevel2 = res.data.data[0].children
					});
			},
			activeChanged(i) {
				this.active = i
				//为二级分类重新赋值
				this.cataLevel2 = this.cateList[i].children
				this.scrollTop=Math.random(0,1)
			},
			//跳转到商品列表页面
			gotoGoodsList(item){
				uni.navigateTo({
					url:'/subpkg/goods_list/goods_list?id='+item
				})
			},
			//跳转到搜索页面
			gotoSearch(){
				uni.navigateTo({
					url:'/subpkg/search/search'
				})
			}
		}
	}
</script>

<style lang="scss">
	.scroll-view-container {
		display: flex;

		.left-scroll-view {
			width: 120px;

			.left-scroll-item {
				background-color: #f7f7f7;
				line-height: 60px;
				text-align: center;
				font-size: 14px;

				&.active {
					background-color: #ffffff;
					position: relative;

					&::before {
						content: ' ';
						display: block;
						width: 3px;
						height: 30px;
						background-color: #c00000;
						position: absolute;
						top: 50%;
						left: 0;
						transform: translateY(-50%);
					}
				}
			}
		}
	}

	.cate-lv2-title {
		font-size: 12px;
		font-weight: bold;
		text-align: center;
		padding: 15px 0;
	}

	.cate-lv3-list {
		display: flex;
		flex-wrap: wrap;

		.cate-lv3-item {
			width: 33%;
			display: flex;
			flex-direction: column;
			justify-content: center;
			align-items: center;

			image {
				width: 60px;
				height: 60px;
			}

			text {
				font-size: 12px;
			}
		}
	}
</style>

自定义搜索组件

<template>
	<view @click="searchHandler" class="my-search-container" :style="{'background-color': bgcolor}">
		<view class="my-search-box" :style="{'border-radius': radius+'px'}">
			<uni-icons type="search" size="18"></uni-icons>
			<text class="placeholder">搜索</text>
		</view>
	</view>
</template>

<script>
	export default {
		//组件自定义属性
		props:{
			bgcolor:{
				type:String,
				default:'#55aaff'
			},
			radius:{
				type:Number,
				default:18//px
			}
		},
		name: "my-search",
		data() {
			return {

			};
		},
		methods:{
			searchHandler(){
				this.$emit('click')
			}
		}
	}
</script>

<style lang="scss">
	.my-search-container {
		height: 50px;
		// background-color: #55aaff;
		display: flex;
		align-items: center;
		padding: 0 10px;

		.my-search-box {
			height: 36px;
			// border-radius: 18px;
			background-color: #ffffff;
			width: 100%;
			display: flex;
			align-items: center;
			justify-content: center;
		}

		.placeholder {
			font-size: 15px;
			margin-left: 5px;
		}
	}
</style>

2.效果示例

在这里插入图片描述

  • 1
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值