uView2.0 ScrollList 多菜单扩展

ScrollList 多菜单扩展

使用uni/vue2

html 复制代码
 // HTML
<u-scroll-list>
    <view class="scroll-list margin-top-xs">
      <!-- 第一行 -->
      <view class="scroll-list__row">
        <view
          class="scroll-list__goods-item"
		  style="width: 248rpx;height:120rpx;display:flex;justify-content: center;align-items:center"
          v-for="(item, index) in list.slice(0, 6)" 
          :key="'row1-' + index"
          :class="[(index === 4) && 'scroll-list__goods-item--no-margin-right']"
        >
		<text>{{item.name}}</text>
		<text class="margin-top-sm text-bold" style="color:#ff6347">¥{{ item.price }}</text>
        </view>
      </view>
      <!-- 第二行 -->
      <view class="scroll-list__row">
        <view
          class="scroll-list__goods-item"
		  style="width: 248rpx;height:120rpx;display:flex;justify-content: center;align-items:center"
          v-for="(item, index) in list.slice(6, 12)" 
          :key="'row2-' + index"
          :class="[(index === 4) && 'scroll-list__goods-item--no-margin-right']"
        >
		<text>{{item.name}}</text>
		<text class="margin-top-sm text-bold" style="color:#ff6347">¥{{ item.price }}</text>
        </view>
      </view>
    </view>
  </u-scroll-list>
javascript 复制代码
// JS
data() {
			return {
				list: [
					{name: '全部销售额',price: 11.90},
					{name: '全部单数', price: 11.90},
					{name: '预估佣金', price: 11.90},
					{name: '退款销售额', price: 11.90},
					{name: '退款订单',price: 11.90},
					{name: '退款服务费',price: 11.90},
					{name: '有效销售额',price: 11.90},
					{name: '有效单数',price: 11.90},
					{name: '有效佣金',price: 11.90},
					{name: '已结算销售额',price: 11.90},
					{name: '已结算订单',price: 11.90},
					{name: '已结算服务费',price: 11.90},
				],
			}
		}
css 复制代码
// css
<style lang="scss">
  .scroll-list {
	display: flex;
	flex-direction: column;
      .scroll-list__row {
        display: flex;
        flex-direction: row;
        margin-top:5rpx;
     }
     .scroll-list__goods-item {
       display: flex;
       flex-direction: column;
       align-items: center;
    }
}

<style>

如果下面的指示器跟上面的滑动不同步的情况下用下面的代码

javascript 复制代码
<template>
  <u-scroll-list @scroll="handleScroll">
    <view class="scroll-list margin-top-xs" ref="scrollList">
      <!-- 第一行 -->
      <view class="scroll-list__row">
        <view
          class="scroll-list__goods-item"
          style="width: 248rpx;height:120rpx;display:flex;justify-content: center;align-items:center"
          v-for="(item, index) in list.slice(0, 6)" 
          :key="'row1-' + index"
          :class="[(index === 4) && 'scroll-list__goods-item--no-margin-right']"
        >
          <text>{{item.name}}</text>
          <text class="margin-top-sm text-bold" style="color:#ff6347">¥{{ item.price }}</text>
        </view>
      </view>
      <!-- 第二行 -->
      <view class="scroll-list__row">
        <view
          class="scroll-list__goods-item"
          style="width: 248rpx;height:120rpx;display:flex;justify-content: center;align-items:center"
          v-for="(item, index) in list.slice(6, 12)" 
          :key="'row2-' + index"
          :class="[(index === 4) && 'scroll-list__goods-item--no-margin-right']"
        >
          <text>{{item.name}}</text>
          <text class="margin-top-sm text-bold" style="color:#ff6347">¥{{ item.price }}</text>
        </view>
      </view>
    </view>
    <div class="indicator-container">
      <div class="indicator" :style="{ width: indicatorWidth + '%' }"></div>
    </div>
  </u-scroll-list>
</template>

<script>
export default {
  data() {
    return {
    	list: [
			{name: '全部销售额',price: 11.90},
			{name: '全部单数', price: 11.90},
			{name: '预估佣金', price: 11.90},
			{name: '退款销售额', price: 11.90},
			{name: '退款订单',price: 11.90},
			{name: '退款服务费',price: 11.90},
			{name: '有效销售额',price: 11.90},
			{name: '有效单数',price: 11.90},
			{name: '有效佣金',price: 11.90},
			{name: '已结算销售额',price: 11.90},
			{name: '已结算订单',price: 11.90},
			{name: '已结算服务费',price: 11.90},
		],
      indicatorWidth: 0,
      totalScrollableWidth: 0,
    };
  },
  mounted() {
    this.calculateScrollableWidth();
    uni.onWindowResize(this.calculateScrollableWidth);
  },
  beforeDestroy() {
    uni.offWindowResize(this.calculateScrollableWidth);
  },
  methods: {
    calculateScrollableWidth() {
      this.$nextTick(() => {
        const query = uni.createSelectorQuery().in(this);
        query.select('.scroll-list').boundingClientRect((data) => {
          if (data) {
            this.totalScrollableWidth = data.scrollWidth - data.width;
          }
        }).exec();
      });
    },
    handleScroll(event) {
      const scrollLeft = event.detail.scrollLeft;
      if (this.totalScrollableWidth > 0) {
        this.indicatorWidth = (scrollLeft / this.totalScrollableWidth) * 100;
      } else {
        this.indicatorWidth = 0;
      }
    },
  },
};
</script>

<style>
.scroll-list {
  display: flex;
  flex-direction: column;
  overflow-x: auto;
}

.scroll-list__row {
  display: flex;
  flex-direction: row;
  margin-top: 5rpx;
}

.scroll-list__goods-item {
  display: flex;
  flex-direction: column;
  align-items: center;
}

.indicator-container {
  position: relative;
  height: 4px;
  background-color: #e0e0e0;
  margin-top: 10px;
}

.indicator {
  position: absolute;
  height: 100%;
  background-color: #ff6347;
  transition: width 0.3s;
}
</style>
相关推荐
一个处女座的程序猿O(∩_∩)O18 小时前
《深入探索Vben框架:使用经验与心得分享》
vue
Pro_er1 天前
Vue3 性能优化十大技巧:打造高性能应用的终极指南
vue·前端开发
Pro_er2 天前
Vue3响应式编程三剑客:计算属性、方法与侦听器深度实战指南
vue·前端开发
乔冠宇2 天前
微信小程序中将图片截图为正方形(自动居中)
微信小程序·小程序·typescript·uniapp
鑫~阳3 天前
Vue2是如何利用Object.defineProperty实现数据的双向绑定?
前端·vue.js·vue
寰宇软件3 天前
PHP房屋出租出售高效预约系统小程序源码
前端·小程序·uni-app·vue·php
爱学习的小王!3 天前
nvm安装、管理node多版本以及配置环境变量【保姆级教程】
经验分享·笔记·node.js·vue
敲代码的鱼哇3 天前
设备唯一ID获取,支持安卓/iOS/鸿蒙Next(uni-device-id)UTS插件
android·ios·uniapp·harmonyos
程序员小续3 天前
Excel 表格和 Node.js 实现数据转换工具
前端·javascript·react.js·前端框架·vue·excel·reactjs
胖头鱼不吃鱼-4 天前
开源低代码平台与 Vue.js
开源·vue