微信小程序 - 菜单左右联动示例

今天记录一个个人写的二级联动示例。

下面是效果图:

功能实现关键是使用控件scroll-view,下面直接上示例代码。

页面对应的js文件:



Page({
  data: {
    select_index:0,
    scroll_height:0,
    left: [{
        id: 1,
        title: '选项一'
      },
      {
        id: 2,
        title: '选项二'
      },
      {
        id: 3,
        title: '选项三'
      },
      {
        id: 4,
        title: '选项四'
      },
      {
        id: 5,
        title: '选项五'
      },
      {
        id: 6,
        title: '选项六'
      },
      {
        id: 7,
        title: '选项七'
      }
    ],
    right:[
      {
        id: 1,
        title: '选项一',
        content:[
          {
            title:"产品一"
          },
          {
            title: "产品二"
          },
          {
            title: "产品三"
          },
          {
            title: "产品四"
          },
        ]
      },
      {
        id: 2,
        title: '选项二',
        content: [
          {
            title: "产品一"
          },
          {
            title: "产品二"
          },
          {
            title: "产品三"
          },
          {
            title: "产品四"
          },
        ]
      },
      {
        id: 3,
        title: '选项三',
        content: [
          {
            title: "产品一"
          },
          {
            title: "产品二"
          },
          {
            title: "产品三"
          },
          {
            title: "产品四"
          },
        ]
      },
      {
        id: 4,
        title: '选项四',
        content: [
          {
            title: "产品一"
          },
          {
            title: "产品二"
          },
          {
            title: "产品三"
          },
          {
            title: "产品四"
          },
        ]
      },
      {
        id: 5,
        title: '选项五',
        content: [
          {
            title: "产品一"
          },
          {
            title: "产品二"
          },
          {
            title: "产品三"
          },
          {
            title: "产品四"
          },
        ]
      },
      {
        id: 6,
        title: '选项六',
        content: [
          {
            title: "产品一"
          },
          {
            title: "产品二"
          },
          {
            title: "产品三"
          },
          {
            title: "产品四"
          },
        ]
      },
      {
        id: 7,
        title: '选项七',
        content: [
          {
            title: "产品一"
          },
          {
            title: "产品二"
          },
          {
            title: "产品三"
          },
          {
            title: "产品四"
          },
        ]
      }
    ]
  },

  // 右边scroll-view滑动事件
  scroll:function(e){
    var h = e.detail.scrollTop
    var scroll_height = 0;
    var select_index;
    for (var i = 0; i < this.data.right.length; i++) {
      if (scroll_height >= h){
        select_index = i;
        break;
      }
      scroll_height += this.data.right[i].content.length * 64 + 48;
    }
    this.setData({
      select_index: i,
    });
  },

  //左边点击事件
  left_tap:function(e){
    var scroll_height = 0;
    for (var i = 0; i < e.target.dataset.index;i++){
      scroll_height += this.data.right[i].content.length * 64 + 48;
    }
    console.log(scroll_height)
    this.setData({
      scroll_height: scroll_height,
      select_index: e.target.dataset.index,
    });
  }
})

页面对应的wxml文件:

<view class='main'>

  <view class='left'>
    <scroll-view scroll-y="true" scroll-with-animation="true">
      <block wx:for="{{left}}" wx:for-index="index">
        <view class='{{select_index==index?"active":""}}' data-index="{{index}}" bindtap='left_tap'>{{item.title}}</view>
      </block>
    </scroll-view>
  </view>

  <view class='right'>
    <scroll-view scroll-y="true" scroll-top="{{scroll_height}}" bindscroll="scroll" scroll-with-animation="true">
      <block wx:for="{{right}}">
        <view class='block'>
          <view style='background: lightgrey;'>{{item.title}}</view>
          <view class='list'>
            <block wx:for="{{item.content}}">
              <view>{{item.title}}</view>
            </block>
          </view>
        </view>
      </block>

    </scroll-view>
  </view>

</view>

注:纯个人编写,用于记录,欢迎讨论,不喜勿喷。

  • 0
    点赞
  • 33
    收藏
    觉得还不错? 一键收藏
  • 5
    评论
微信小程序是一种基于微信平台的应用的开发模式,可以快速的开发出符合用户需求的小程序。在小程序的开发中,组件是一个非常重要的概念,通过组件可以实现复用性和模块化编程思想。 组件应用是小程序开发的基础。通过组件可以将某一模块化并封装起来,使得组件可以在不同的页面间得到复用,大大提升了开发效率并减少了代码冗余。微信小程序提供了丰富的自带组件,包括文本、图片、按钮、输入框等等,开发者也可以自己开发组件来满足自己的需求。实际开发中,通过组件可以快速搭建页面框架和业务逻辑。 Demo是一个演示小程序的示例程序。在小程序的实际开发过程中,一个好的Demo非常重要。通过Demo,开发人员可以更深入的了解小程序的开发流程、组件的应用和实际的业务开发等等。在Demo中,通常会包括小程序的一些基础操作,如页面跳转、数据绑定、组件的使用等。而在实际开发中,Demo还会包括一些复杂的业务场景,如支付、登录、数据列表展示等等。Demo不仅为开发者提供了学习和实践的机会,也方便了使用者了解该小程序的功能和特点。 总之,微信小程序组件的应用和Demo的开发都是小程序开发过程中非常重要的两个部分。良好的组件应用和精心设计的Demo,可以在极短的时间内实现小程序开发。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值