原生微信小程序、h5、uniapp实现左右联动功能

1、原生微信小程序

实现方法

使用微信小程序文档的scroll-view的属性scroll-into-view可以实现简单类似于锚点的左右联动效果。当左侧切换选中状态navActive改变时随之右侧列表绑定的属性值改变时随着会滑动到对id的位置id="category-x"处(scroll-into-view="category-{ {toView}}")。注意:id不能以数字开头

 效果图:

 wxml

<!--shared_store/pages/tests/tests.wxml-->
<view class="test-box">
    <view class="box-box">
      <view class="aside" id="leftNav">
        <scroll-view scroll-y="true" scroll-with-animation="true">
          <view
            class="lf-lis {
  
  {navActive == index?'selected':''}}"
            wx:for="{
  
  {categories}}"
            wx:key="index"
						data-index="{
  
  {index}}"
            bindtap="navigateTap"
          >
            {
  
  { item.name }}
          </view>
        </scroll-view>
      </view>
      <view class="right-content" id="right-content" ref="rightContent">
        <!-- 通过scroll-into-view 可以实现类型于 瞄点链接的效果 当 navActive 改变时 会自动划到id="category-x"处 -->
        <scroll-view
          scroll-y="true"
          scroll-into-view="category-{
  
  {toView}}"
          bindscroll="scroll"
          bindscrolltolower="scrolltolower"
          scroll-with-animation="true"
          style="height: 100%"
          class="conterScroll"
        >
          <view
            wx:for="{
  
  {products}}"
            wx:key="index"
            id="category-{
  
  {index}}"
            class="ri-box"
          >
            <view class="ri-lis">
              <view class="ri_lis-title">------{
  
  { item.name }}------</view>
              <view class="ri-lis-box">
                <view
                  class="ri-lis-li"
									wx:for="{
  
  {item.child}}"
									wx:for-index="ind" wx:for-item="items">
                  <image
                    class="imgs"
                    src="{
  
  {items.img}}"
                    alt=""
                  />
                  <view class="li_title">{
  
  { items.name }}</view>
                </view>
              </view>
            </view>
          </view>
          <!-- 最后一类商品不够时,添加高度撑满屏幕 -->
          <view
            style="{
  
  {'height:' + (height -300) + 'rpx;'}}"
            wx:if="childLth < 15"
          ></view>
        </scroll-view>
      </view>
        
    </view>
  </view>

js

// shared_store/pages/tests/tests.js
const app = getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    http_host: http_host, //域名
    theme: app.globalData.style_color,
    categories: [
      // 左侧导航栏的选项
      { id: 1, name: "分类1" },
      { id: 2, name: "分类2" },
      { id: 3, name: "分类3" },
    ],
    navActive: 0,
    products: [
      // 右侧商品列表的模块
      {
        id: "product1",
        name: "商品1",
        child: [
          { id: "product2", name: "商品1-1",img:'https://develop.dongguanhongyun.com/resources/5903/202405/17168772712541156601615.png' },
          { id: "product2", name: "商品1-2",img:'https://develop.dongguanhongyun.com/resources/5903/202405/17168772712541156601615.png' },
          { id: "product2", name: "商品1-3",img:'https://develop.dongguanhongyun.com/resources/5903/202405/17168772712541156601615.png' },
          { id: "product4", name: "商品1-4",img:'https://develop.dongguanhongyun.com/resources/5903/202405/17168772712541156601615.png' },
          { id: "product5", name: "商品1-5",img:'https://develop.dongguanhongyun.com/resources/5903/202405/17168772712541156601615.png' },
          { id: "product2", name: "商品1-1",img:'https://develop.dongguanhongyun.com/resources/5903/202405/17168772712541156601615.png' },
          { id: "product2", name: "商品1-2",img:'https://develop.dongguanhongyun.com/resources/5903/202405/17168772712541156601615.png' },
          { id: "product2", name: "商品1-3",img:'https://develop.dongguanhongyun.com/resources/5903/202405/17168772712541156601615.png' },
          { id: "product4", name: "商品1-4",img:'https://develop.dongguanhongyun.com/resources/5903/202405/17168772712541156601615.png' },
          { id: "product5", name: "商品1-5",img:'https://develop.dongguanhongyun.com/resources/5903/202405/17168772712541156601615.png' },
        ],
      },
      {
        id: "product2",
        name: "商品2",
        child: [
          { id: "product2", name: "商品1-1",img:'https://develop.dongguanhongyun.com/resources/5903/202405/17168772712541156601615.png' },
          { id: "product2", name: "商品1-2",img:'https://develop.dongguanhongyun.com/resources/5903/202405/17168772712541156601615.png' },
          { id: "product2", name: "商品1-3",img:'https://develop.dongguanhongyun.com/resources/5903/202405/17168772712541156601615.png' },
        ],
      },
      {
        id: "product3",
        name: "商品3",
        child: [
          { id: "product1", name: "商品1-1",img:'https://develop.dongguanhongyun.com/resources/5903/202405/17168772712541156601615.png' },
          { id: "product2", name: "商品1-2",img:'https://develop.dongguanhongyun.com/resources/5903/202405/17168772712541156601615.png' },
          { id: "product3", name: "商品1-3",img:'https://develop.dongguanhongyun.com/resources/5903/202405/17168772712541156601615.png' },
          { id: "product4", name: "商品1-4",img:'https://develop.dongguanhongyun.com/resources/5903/202405/17168772712541156601615.png' },
          { id: "product5", name: "商品1-5",img:'https://develop.dongguanhongyun.com/resources/5903/202405/17168772712541156601615.png' },
          { id: "product6", name: "商品1-6",img:'https://develop.dongguanhongyun.com/resources/5903/202405/17168772712541156601615.png' },
          { id: "product6", name: "商品1-7",img:'https://develop.dongguanhongyun.com/resources/5903/202405/17168772712541156601615.png' },
        ],
      },
      // {
      //   id: "product4",
      //   name: "商品4",
      //   child:
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值