微信小程序 锚点定位案例

  1. 菜单和列表都 使用scroll-view组件
  2. 通过下标 动态绑定元素
  3. 添加点击事件 实现切换效果
  4. 效果如下:
    在这里插入图片描述
<!--导航滚动  -->
<scroll-view class="tui-city-scroll" scroll-x="true" scroll-into-view="NAV{{status}}" scroll-with-animation="true">
  <text bindtap="getStatus" id="NAV{{index}}" class="tui-nav-li {{index === status ? 'tui-nav-active' : ''}}" data-index="{{index}}" wx:for="{{navList}}" wx:key="">{{item}}</text>
</scroll-view>

<!--列表滚动区  -->
<view class="tui-fixed-y">
  <scroll-view class="tui-city-scroll-y" scroll-y="true" scroll-into-view="NAV{{status}}" scroll-with-animation="true">
    <view wx:for="{{navList}}" wx:key="">
      <view id="NAV{{index}}" class="tui-list-head">{{item}}</view>
      <view class="tui-list-li">{{item}} 列表 {{index}}</view>
    </view>
  </scroll-view>
</view>
.tui-fixed-x{
  width: 100%;
  position: fixed;
  top: 0;
  left: 0;
}
.tui-city-scroll{
  height: 220rpx;
  line-height: 80rpx;
  width: 100%;
  white-space: nowrap;
}
.tui-city-scroll text{
  height: 120rpx;
  line-height: 80rpx;
  width: 100%;
  white-space: nowrap;
}
.tui-nav-li{
  font-size: 33rpx;
  padding: 0 10rpx;
}
.tui-nav-li:first-child{padding-left: 16rpx;}
.tui-nav-li:last-child{padding-right: 16rpx;}
.tui-nav-active{
  color: red;
  border-bottom: 3rpx solid red;
}
.tui-fixed-y{
  width: 100%;
  height: calc(100% - 80rpx);
  position: fixed;
  bottom: 0;
  left: 0;
}
.tui-city-scroll-y{
  padding: 0 20rpx;
  height: 100%;
  box-sizing: border-box;
}
.tui-list-head{
  height: 50px;
  line-height: 50px;
  text-align: center;
  font-size: 30rpx;
  color: blue;
}
.tui-list-li{
  height: 400px;
  padding: 10rpx;
  color: #fff;
  font-size: 50rpx;
  background-color: blue;
}
.btn{
  background-color: yellow;
  z-index: 1000;
  position: fixed;
  bottom: 0;
}
Page({
  data: {
    status: 0,
    navList: ['苹果', '香蕉', '橘子', '火龙果', '苹果2', '香蕉2', '橘子2', '火龙2果', '苹果3', '香蕉3', '橘子4', '火龙2果4']
  },
  getStatus(e) {
    this.setData({
      status: e.currentTarget.dataset.index
    })
  },

})
  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值