vue 之 商品列表的左右联动

vue 之 商品列表的左右联动

在这里插入图片描述

思路

  • 1:初始化的时候,左右两侧实现 可以滚动
    • 监听右侧的滚动事件,把具体的滚动y轴的值,赋值给 scrollY
  • 2:然后计算出右侧的每一个板块的距离顶部高度,为一个高度数组
  • 3:计算属性之中,设置当前高亮 返回当前索引
    • 需要找到当前索引,然后根据这个高度数组,和scrollY进行比较,需要返回那个索引
  • 4:点击左侧切换的时候,拿当前的索引号,然后再拿到右侧的滚动位置,赋值给scrollY,最后调用右侧的滚动事件
  • 5:右侧滚动时候,实现左侧的联动,
    • 先拿到当前索引(这需要在计算属性之中的,获取当前索引内调用)
    • 然后拿到右侧的 每一个li中的ul父节点,这个一个外容器
    • 在拿到当前的节点,最后滚动到该节点

实现代码

<!--  -->
<template>
  <div class="chat_wrap">
    <div class="shop">
      <div class="menu_wrap">
        <ul>
          <li
            v-for="(item, index) in leftD"
            :key="index"
            :class="{ current: index === currentIndex }"
            @click="clickLeftItem(index)"
            ref="meneList"
          >
            <span>{{ item }}</span>
          </li>
        </ul>
      </div>
      <div class="shop_wrap">
        <ul ref="shopListP">
          <li class="shopList" v-for="item in rightD" :key="item.title">
            <div class="shop_title">
              <div class="title_t">
                <h4>{{ item.title }}</h4>
                <a href="###">{{ item.msg }}</a>
              </div>
              <ul>
                <li v-for="items in item.list" :key="items">
                  <img :src="items.imgUrl" alt />
                  <span>{{ items.title }}</span>
                </li>
              </ul>
            </div>
          </li>
        </ul>
      </div>
    </div>
  </div>
</template>

<script>
import BScroll from "better-scroll";
export default {
  name: "Chat",
  data() {
    return {
      scrollY: 0, //右侧列表滚动的y轴
      rightItemTop: [], //所有分类的头部位置
   };
  },
  watch: {
    // // searchGoods 是请求回来的数据 监听这个数据的变化 然后调用 由于写死的数据 因此在mounted之中调用 实际上this.$nextTick存放在watch之中
    // searchGoods() {
    //   this.$nextTick(() => {
    //     this.init();
    //   });
    // }
  },
  mounted() {
    //基本的滚动 就是在 mounted之中调用的
    this.$nextTick(() => {
      this.init();
      // 求出 所有版块的头部位置
      this.rightListTop();
    });
  },
  computed: {
    //左侧的动态选中
    currentIndex() {
      //获取数据
      const { scrollY, rightItemTop } = this;
      // 取出索引 滚动到哪里 然后左侧显示高亮
      return rightItemTop.findIndex((LiTop, index) => {
        this._leftScroll(index); //调用 左侧的联动
        return scrollY >= LiTop && scrollY < rightItemTop[index + 1];
      });
    }
  },
  methods: {
    init() {
      //左侧
      this.leftScroll = new BScroll(".menu_wrap", {
        scrollY: true,
        click: true
      });
      // 右侧
      this.rightScroll = new BScroll(".shop_wrap", {
        scrollY: true,
        click: true,
        probeType: 3 //滚动时候 监听
      });
      //监听右侧的滚动事件
      this.rightScroll.on("scrollEnd", pos => {
        //scroll  scrollEnd滚动结束的时候 监听
        this.scrollY = Math.abs(pos.y);
      });
    },
    // 获取右侧的 每一个模块的距离顶部的距离
    rightListTop() {
      const tempArr = [];
      let top = 0; //记录高度
      tempArr.push(top);
      // 遍历 对应的ul 中的li 取出每一个高度
      let allList = this.$refs.shopListP.querySelectorAll(".shopList");
      allList.forEach(item => {
        top += item.clientHeight;
        // console.log("top", top);
        tempArr.push(top);
      });
      this.rightItemTop = tempArr;
    },
    //点击切换
    clickLeftItem(index) {
      // console.log("index", this.rightItemTop[index]);
      this.scrollY = this.rightItemTop[index];
      this.rightScroll.scrollTo(0, -this.scrollY, 300);
    },
    //右侧滚动时候 实现左侧的联动
    _leftScroll(index) {
      let meneList = this.$refs.meneList;
      // console.log("左侧", index, meneList);
      let el = meneList[index];
      this.leftScroll.scrollToElement(el, 300, 0, -100);
    }
  },
  components: {}
};
</script>
<style lang="scss" scoped>
ul,
li {
  list-style: none;
  margin: 0;
  padding: 0;
}
.chat_wrap {
  padding: 0;
  margin: 0;
  width: 100%;
  height: 100%;
  overflow: hidden;
  .shop {
    width: 100%;
    height: 100%;
    display: flex;
    overflow: hidden;

    .menu_wrap {
      width: 75px;
      height: 100%;
      background: #ccc;
      & > ul > li {
        width: 75px;
        height: 60px;
        background: #fafafa;
        span {
          display: flex;
          justify-content: center;
          align-items: center;
          width: 100%;
          height: 100%;
        }
      }
      .current {
        position: relative;
        color: red;
        &::before {
          position: absolute;
          left: 0;
          top: 15px;
          content: "";
          height: 30px;
          background: red;
          width: 4px;
        }
      }
    }
    .shop_wrap {
      width: 300px;
      height: 100%;
      // background: yellow;
      overflow-x: hidden;
      overflow-y: scroll;
      .shop_title {
        & > .title_t {
          display: flex;
          justify-content: space-between;
          align-items: center;
        }
        & > ul {
          display: flex;
          flex-wrap: wrap;
        }
        & > ul > li {
          width: 50%;
          height: 130px;
          margin-bottom: 5px;
          background: pink;
          img {
            width: 100%;
            height: 85%;
          }
          span {
            display: block;
            height: 20px;
            line-height: 20px;
            color: #fff;
            text-align: center;
            background: #000;
          }
        }
      }
    }
  }
}
</style>

  • 数据
      leftD: [
        "女装",
        "鞋包",
        "T恤",
        "靴子",
        "男装",
        "珠宝",
        "首饰",
        "鞋子",
        "帽子",
        "水果",
        "家电",
        "蓝莓",
        "五金"
      ],
      rightD: [
        {
          title: "女装",
          msg: "查看更多",
          list: [
            {
              title: "女装",
              imgUrl:
                "https://t00img-c.yangkeduo.com/goods/images/2020-08-13/ce99a1b2f665ba478d9573c314686c5d.jpeg"
            },
            {
              title: "女装",
              imgUrl:
                "https://t00img-c.yangkeduo.com/goods/images/2020-08-13/ce99a1b2f665ba478d9573c314686c5d.jpeg"
            },
            {
              title: "女装",
              imgUrl:
                "https://t00img-c.yangkeduo.com/goods/images/2020-08-13/ce99a1b2f665ba478d9573c314686c5d.jpeg"
            },
            {
              title: "女装",
              imgUrl:
                "https://t00img-c.yangkeduo.com/goods/images/2020-08-13/ce99a1b2f665ba478d9573c314686c5d.jpeg"
            }
          ]
        },
        {
          title: "鞋包",
          msg: "查看更多",
          list: [
            {
              title: "鞋包",
              imgUrl:
                "https://t00img-c.yangkeduo.com/goods/images/2020-08-13/ce99a1b2f665ba478d9573c314686c5d.jpeg"
            },
            {
              title: "鞋包",
              imgUrl:
                "https://t00img-c.yangkeduo.com/goods/images/2020-08-13/ce99a1b2f665ba478d9573c314686c5d.jpeg"
            },
            {
              title: "鞋包",
              imgUrl:
                "https://t00img-c.yangkeduo.com/goods/images/2020-08-13/ce99a1b2f665ba478d9573c314686c5d.jpeg"
            },
            {
              title: "鞋包",
              imgUrl:
                "https://t00img-c.yangkeduo.com/goods/images/2020-08-13/ce99a1b2f665ba478d9573c314686c5d.jpeg"
            }
          ]
        },
        {
          title: "T恤",
          msg: "查看更多",
          list: [
            {
              title: "T恤",
              imgUrl:
                "https://t00img-c.yangkeduo.com/goods/images/2020-08-13/ce99a1b2f665ba478d9573c314686c5d.jpeg"
            },
            {
              title: "T恤",
              imgUrl:
                "https://t00img-c.yangkeduo.com/goods/images/2020-08-13/ce99a1b2f665ba478d9573c314686c5d.jpeg"
            },
            {
              title: "T恤",
              imgUrl:
                "https://t00img-c.yangkeduo.com/goods/images/2020-08-13/ce99a1b2f665ba478d9573c314686c5d.jpeg"
            },
            {
              title: "T恤",
              imgUrl:
                "https://t00img-c.yangkeduo.com/goods/images/2020-08-13/ce99a1b2f665ba478d9573c314686c5d.jpeg"
            }
          ]
        },
        {
          title: "靴子",
          msg: "查看更多",
          list: [
            {
              title: "靴子",
              imgUrl:
                "https://t00img-c.yangkeduo.com/goods/images/2020-08-13/ce99a1b2f665ba478d9573c314686c5d.jpeg"
            },
            {
              title: "靴子",
              imgUrl:
                "https://t00img-c.yangkeduo.com/goods/images/2020-08-13/ce99a1b2f665ba478d9573c314686c5d.jpeg"
            },
            {
              title: "靴子",
              imgUrl:
                "https://t00img-c.yangkeduo.com/goods/images/2020-08-13/ce99a1b2f665ba478d9573c314686c5d.jpeg"
            },
            {
              title: "靴子",
              imgUrl:
                "https://t00img-c.yangkeduo.com/goods/images/2020-08-13/ce99a1b2f665ba478d9573c314686c5d.jpeg"
            }
          ]
        },
        {
          title: "男装",
          msg: "查看更多",
          list: [
            {
              title: "男装",
              imgUrl:
                "https://t00img-c.yangkeduo.com/goods/images/2020-08-13/ce99a1b2f665ba478d9573c314686c5d.jpeg"
            },
            {
              title: "男装",
              imgUrl:
                "https://t00img-c.yangkeduo.com/goods/images/2020-08-13/ce99a1b2f665ba478d9573c314686c5d.jpeg"
            },
            {
              title: "男装",
              imgUrl:
                "https://t00img-c.yangkeduo.com/goods/images/2020-08-13/ce99a1b2f665ba478d9573c314686c5d.jpeg"
            },
            {
              title: "男装",
              imgUrl:
                "https://t00img-c.yangkeduo.com/goods/images/2020-08-13/ce99a1b2f665ba478d9573c314686c5d.jpeg"
            }
          ]
        },
        {
          title: "珠宝",
          msg: "查看更多",
          list: [
            {
              title: "珠宝",
              imgUrl:
                "https://t00img-c.yangkeduo.com/goods/images/2020-08-13/ce99a1b2f665ba478d9573c314686c5d.jpeg"
            },
            {
              title: "珠宝",
              imgUrl:
                "https://t00img-c.yangkeduo.com/goods/images/2020-08-13/ce99a1b2f665ba478d9573c314686c5d.jpeg"
            },
            {
              title: "珠宝",
              imgUrl:
                "https://t00img-c.yangkeduo.com/goods/images/2020-08-13/ce99a1b2f665ba478d9573c314686c5d.jpeg"
            },
            {
              title: "珠宝",
              imgUrl:
                "https://t00img-c.yangkeduo.com/goods/images/2020-08-13/ce99a1b2f665ba478d9573c314686c5d.jpeg"
            }
          ]
        },
        {
          title: "首饰",
          msg: "查看更多",
          list: [
            {
              title: "首饰",
              imgUrl:
                "https://t00img-c.yangkeduo.com/goods/images/2020-08-13/ce99a1b2f665ba478d9573c314686c5d.jpeg"
            },
            {
              title: "首饰",
              imgUrl:
                "https://t00img-c.yangkeduo.com/goods/images/2020-08-13/ce99a1b2f665ba478d9573c314686c5d.jpeg"
            },
            {
              title: "首饰",
              imgUrl:
                "https://t00img-c.yangkeduo.com/goods/images/2020-08-13/ce99a1b2f665ba478d9573c314686c5d.jpeg"
            },
            {
              title: "首饰",
              imgUrl:
                "https://t00img-c.yangkeduo.com/goods/images/2020-08-13/ce99a1b2f665ba478d9573c314686c5d.jpeg"
            }
          ]
        },
        {
          title: "鞋子",
          msg: "查看更多",
          list: [
            {
              title: "鞋子",
              imgUrl:
                "https://t00img-c.yangkeduo.com/goods/images/2020-08-13/ce99a1b2f665ba478d9573c314686c5d.jpeg"
            },
            {
              title: "鞋子",
              imgUrl:
                "https://t00img-c.yangkeduo.com/goods/images/2020-08-13/ce99a1b2f665ba478d9573c314686c5d.jpeg"
            },
            {
              title: "鞋子",
              imgUrl:
                "https://t00img-c.yangkeduo.com/goods/images/2020-08-13/ce99a1b2f665ba478d9573c314686c5d.jpeg"
            },
            {
              title: "鞋子",
              imgUrl:
                "https://t00img-c.yangkeduo.com/goods/images/2020-08-13/ce99a1b2f665ba478d9573c314686c5d.jpeg"
            }
          ]
        },
        {
          title: "帽子",
          msg: "查看更多",
          list: [
            {
              title: "帽子",
              imgUrl:
                "https://t00img-c.yangkeduo.com/goods/images/2020-08-13/ce99a1b2f665ba478d9573c314686c5d.jpeg"
            },
            {
              title: "帽子",
              imgUrl:
                "https://t00img-c.yangkeduo.com/goods/images/2020-08-13/ce99a1b2f665ba478d9573c314686c5d.jpeg"
            },
            {
              title: "帽子",
              imgUrl:
                "https://t00img-c.yangkeduo.com/goods/images/2020-08-13/ce99a1b2f665ba478d9573c314686c5d.jpeg"
            },
            {
              title: "帽子",
              imgUrl:
                "https://t00img-c.yangkeduo.com/goods/images/2020-08-13/ce99a1b2f665ba478d9573c314686c5d.jpeg"
            }
          ]
        },
        {
          title: "水果",
          msg: "查看更多",
          list: [
            {
              title: "水果",
              imgUrl:
                "https://t00img-c.yangkeduo.com/goods/images/2020-08-13/ce99a1b2f665ba478d9573c314686c5d.jpeg"
            },
            {
              title: "水果",
              imgUrl:
                "https://t00img-c.yangkeduo.com/goods/images/2020-08-13/ce99a1b2f665ba478d9573c314686c5d.jpeg"
            },
            {
              title: "水果",
              imgUrl:
                "https://t00img-c.yangkeduo.com/goods/images/2020-08-13/ce99a1b2f665ba478d9573c314686c5d.jpeg"
            },
            {
              title: "水果",
              imgUrl:
                "https://t00img-c.yangkeduo.com/goods/images/2020-08-13/ce99a1b2f665ba478d9573c314686c5d.jpeg"
            }
          ]
        },
        {
          title: "家电",
          msg: "查看更多",
          list: [
            {
              title: "家电",
              imgUrl:
                "https://t00img-c.yangkeduo.com/goods/images/2020-08-13/ce99a1b2f665ba478d9573c314686c5d.jpeg"
            },
            {
              title: "家电",
              imgUrl:
                "https://t00img-c.yangkeduo.com/goods/images/2020-08-13/ce99a1b2f665ba478d9573c314686c5d.jpeg"
            },
            {
              title: "家电",
              imgUrl:
                "https://t00img-c.yangkeduo.com/goods/images/2020-08-13/ce99a1b2f665ba478d9573c314686c5d.jpeg"
            },
            {
              title: "家电",
              imgUrl:
                "https://t00img-c.yangkeduo.com/goods/images/2020-08-13/ce99a1b2f665ba478d9573c314686c5d.jpeg"
            }
          ]
        },
        {
          title: "蓝莓",
          msg: "查看更多",
          list: [
            {
              title: "蓝莓",
              imgUrl:
                "https://t00img-c.yangkeduo.com/goods/images/2020-08-13/ce99a1b2f665ba478d9573c314686c5d.jpeg"
            },
            {
              title: "蓝莓",
              imgUrl:
                "https://t00img-c.yangkeduo.com/goods/images/2020-08-13/ce99a1b2f665ba478d9573c314686c5d.jpeg"
            },
            {
              title: "蓝莓",
              imgUrl:
                "https://t00img-c.yangkeduo.com/goods/images/2020-08-13/ce99a1b2f665ba478d9573c314686c5d.jpeg"
            },
            {
              title: "蓝莓",
              imgUrl:
                "https://t00img-c.yangkeduo.com/goods/images/2020-08-13/ce99a1b2f665ba478d9573c314686c5d.jpeg"
            }
          ]
        },
        {
          title: "五金",
          msg: "查看更多",
          list: [
            {
              title: "五金",
              imgUrl:
                "https://t00img-c.yangkeduo.com/goods/images/2020-08-13/ce99a1b2f665ba478d9573c314686c5d.jpeg"
            },
            {
              title: "五金",
              imgUrl:
                "https://t00img-c.yangkeduo.com/goods/images/2020-08-13/ce99a1b2f665ba478d9573c314686c5d.jpeg"
            },
            {
              title: "五金",
              imgUrl:
                "https://t00img-c.yangkeduo.com/goods/images/2020-08-13/ce99a1b2f665ba478d9573c314686c5d.jpeg"
            },
            {
              title: "五金",
              imgUrl:
                "https://t00img-c.yangkeduo.com/goods/images/2020-08-13/ce99a1b2f665ba478d9573c314686c5d.jpeg"
            }
          ]
        }
      ]
  • 8
    点赞
  • 34
    收藏
    觉得还不错? 一键收藏
  • 3
    评论
Vue 中实现列表商品双向联动效果,可以考虑如下步骤: 1. 定义商品列表组件和商品详情组件 首先需要定义左侧商品列表组件和右侧商品详情组件,可以分别使用 `<template>`、`<script>` 和 `<style>` 标签定义组件模板、脚本和样式。 2. 在商品列表组件中绑定 click 事件 在商品列表组件中,需要为每个商品元素绑定 click 事件,当用户点击某个商品时触发事件。事件处理函数中可以通过 `this.$emit` 方法发送事件通知,将当前商品信息传递给父组件。 3. 在商品详情组件中监听事件 在商品详情组件中,需要使用 `props` 属性定义父组件传递过来的商品信息,并监听父组件发送的事件。当事件发生时,可以根据事件参数更新商品详情区域的内容。 4. 在父组件中使用子组件 在父组件中,需要引入商品列表组件和商品详情组件,并在模板中使用它们。同时需要定义一个 `selectedItem` 数据,用于存储当前选中的商品信息。当商品列表组件发送事件通知时,更新 `selectedItem` 数据;当商品详情组件接收到事件时,根据事件参数更新 `selectedItem` 数据和右侧商品详情区域的内容。 下面是一个简单的示例代码,用于演示 Vue 中实现列表商品双向联动效果的方法: ```html <!-- 商品列表组件 --> <template> <div> <div v-for="item in items" :key="item.id" @click="handleClick(item)"> {{ item.name }} </div> </div> </template> <script> export default { props: { items: Array }, methods: { handleClick(item) { this.$emit('item-click', item) } } } </script> <!-- 商品详情组件 --> <template> <div> <div v-if="selectedItem"> <h2>{{ selectedItem.name }}</h2> <p>{{ selectedItem.description }}</p> </div> </div> </template> <script> export default { props: { selectedItem: Object }, mounted() { this.$parent.$on('item-click', this.handleItemClick) }, methods: { handleItemClick(item) { this.$emit('item-selected', item) } } } </script> <!-- 父组件 --> <template> <div> <div> <item-list :items="items" @item-click="handleItemClick"></item-list> <item-detail :selected-item="selectedItem" @item-selected="handleItemSelected"></item-detail> </div> </div> </template> <script> import ItemList from './ItemList.vue' import ItemDetail from './ItemDetail.vue' export default { components: { ItemList, ItemDetail }, data() { return { items: [ { id: 1, name: '商品1', description: '这是商品1的描述信息' }, { id: 2, name: '商品2', description: '这是商品2的描述信息' }, { id: 3, name: '商品3', description: '这是商品3的描述信息' } ], selectedItem: null } }, methods: { handleItemClick(item) { this.selectedItem = item }, handleItemSelected(item) { this.selectedItem = item } } } </script> ``` 在上面的示例代码中,我们定义了 `ItemList` 和 `ItemDetail` 两个组件,分别用于展示商品列表商品详情。在父组件中,我们将这两个组件引入,并定义了 `items` 和 `selectedItem` 两个数据,用于存储所有商品和当前选中的商品。在 `ItemList` 组件中,我们为每个商品元素绑定了 click 事件,并通过 `this.$emit` 方法发送了一个 `item-click` 事件,将当前商品信息传递给父组件。在 `ItemDetail` 组件中,我们通过 `props` 属性定义了 `selectedItem` 数据,并在 `mounted` 钩子函数中监听了 `item-click` 事件,当事件发生时,通过 `this.$emit` 方法发送了一个 `item-selected` 事件,将当前商品信息传递给父组件。在父组件中,我们通过 `handleItemClick` 和 `handleItemSelected` 两个方法来更新 `selectedItem` 数据,并将其传递给 `ItemList` 和 `ItemDetail` 两个子组件,实现了列表商品双向联动效果。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值