找回密码
 立即注册
搜索
查看: 2325|回复: 2

微信小程序实现锚点定位

  [复制链接]

581

主题

110

回帖

4066

积分

管理员

积分
4066

众神之神

发表于 2017-7-20 18:51:16 | 显示全部楼层 |阅读模式
锚点定位利用:scroll-into-view 来实现
wxml
  1. <scroll-view class="content" scroll-into-view="{{toView}}" scroll-y="true" scroll-with-animation="true">
  2.   <view wx:for="{{act_addList}}">
  3.     <view class="address_top" id="{{ 'inToView'+item.id}}">{{item.region}}</view>
  4.     <view wx:for="{{item.city}}">
  5.       <view class="address_bottom">{{item.name}}</view>
  6.     </view>
  7.   </view>
  8. </scroll-view>
  9. <view class="orientation_region">
  10.   <view class="orientation">自动定位</view>
  11.   <block wx:for="{{orientationList}}" >
  12.     <block wx:if="{{curCid == item.id}}">
  13.       <view class="orientation_city orientation_select" bindtap="scrollToViewFn" data-id="{{item.id}}">{{item.region}}</view>
  14.     </block>
  15.     <block wx:else>
  16.       <view class="orientation_city" bindtap="scrollToViewFn" data-id="{{item.id}}">{{item.region}}</view>
  17.     </block>
  18.   </block>
  19. </view>
复制代码



js
  1. Page({
  2.   /**
  3.    * 页面的初始数据
  4.    */
  5.   data: {
  6.     orientationList: [
  7.       { id: "01", region: "东北" },
  8.       { id: "02", region: "华北" },
  9.       { id: "03", region: "华东" },
  10.       { id: "04", region: "华南" },
  11.       { id: "05", region: "华中" },
  12.       { id: "06", region: "西北" },
  13.       { id: "07", region: "西南" }
  14.     ],
  15.     act_addList: [
  16.       {
  17.         id: "01", region: "东北地区",
  18.         city: [{ id: "0101", name: "白山江源" },
  19.           { id: "0102", name: "白山市" },
  20.           { id: "0103", name: "宾县" },
  21.           { id: "0104", name: "大庆" },
  22.           { id: "0105", name: "测试1" },
  23.           { id: "0106", name: "测试2" },
  24.           { id: "0107", name: "测试3" },
  25.           { id: "0108", name: "测试4" },
  26.           { id: "0109", name: "测试5" },
  27.           { id: "0110", name: "测试6" },
  28.         ]
  29.       },
  30.       {
  31.         id: "02", region: "华北地区",
  32.         city: [{ id: "0201", name: "包头" },
  33.           { id: "0202", name: "保定" },
  34.           { id: "0206", name: "测试2" },
  35.           { id: "0207", name: "测试3" },
  36.           { id: "0208", name: "测试4" },
  37.           { id: "0209", name: "测试5" },
  38.           { id: "0210", name: "测试6" },
  39.         ]
  40.       },
  41.       {
  42.         id: "03", region: "华东地区",
  43.         city: [{ id: "0303", name: "开封市" },
  44.           { id: "3104", name: "安阳市" },]
  45.       },
  46.       {
  47.         id: "04", region: "华南地区",
  48.         city: [
  49.           { id: "0401", name: "黑龙江市" },
  50.           { id: "0407", name: "测试3" },
  51.           { id: "0508", name: "测试4" },
  52.           { id: "0609", name: "测试5" },
  53.           { id: "0710", name: "测试6" },
  54.           { id: "0711", name: "测试8" },
  55.           { id: "0712", name: "测试9" },
  56.           { id: "0713", name: "测试10" },
  57.           { id: "0714", name: "测试11" },
  58.         ]
  59.       },
  60.       { id: "05", region: "华中地区", city: [{ id: "0501", name: "黑龙江市" }] },
  61.       {
  62.         id: "06", region: "西北地区",
  63.         city: [{ id: "0603", name: "开封市" },
  64.           { id: "0604", name: "安阳市" },]
  65.       },
  66.       {
  67.         id: "07", region: "西南地区",
  68.         city: [{ id: "0703", name: "开封市" },
  69.           { id: "0704", name: "安阳市" },
  70.           { id: "0401", name: "黑龙江市" },
  71.           { id: "0407", name: "测试3" },
  72.           { id: "0508", name: "测试4" },
  73.           { id: "0609", name: "测试5" },
  74.           { id: "0710", name: "测试6" },
  75.           { id: "0711", name: "测试8" },
  76.           { id: "0712", name: "测试9" },
  77.           { id: "0713", name: "测试10" },
  78.           { id: "0714", name: "测试11" },
  79.           { id: "0401", name: "黑龙江市" },
  80.           { id: "0407", name: "测试3" },
  81.           { id: "0508", name: "测试4" },
  82.           { id: "0609", name: "测试5" },
  83.           { id: "0710", name: "测试6" },
  84.           { id: "0711", name: "测试8" },
  85.           { id: "0712", name: "测试9" },
  86.           { id: "0713", name: "测试10" },
  87.           { id: "0714", name: "测试11" },
  88.         ]
  89.       },
  90.     ],
  91.     toView: 'inToView01',
  92.     curCid: '01'
  93.   },
  94.   scrollToViewFn: function (e) {
  95.     var _id = e.target.dataset.id;
  96.     this.setData({
  97.       toView: 'inToView' + _id,
  98.       curCid: _id
  99.     })
  100.   },
  101.   onLoad: function (options) {
  102.   }
  103. })
复制代码



wxss

  1. page {
  2.   height: 100%;
  3. }

  4. .content {
  5.   padding-bottom: 20rpx;
  6.   box-sizing: border-box;
  7.   height: 100%;
  8. }

  9. .location {
  10.   width: 100%;
  11. }

  12. .location_top {
  13.   height: 76rpx;
  14.   line-height: 76rpx;
  15.   background: #f4f4f4;
  16.   color: #606660;
  17.   font-size: 28rpx;
  18.   padding: 0 20rpx;
  19. }

  20. .location_bottom {
  21.   height: 140rpx;
  22.   line-height: 140rpx;
  23.   color: #d91f16;
  24.   font-size: 28rpx;
  25.   border-top: 2rpx #ebebeb solid;
  26.   border-bottom: 2rpx #ebebeb solid;
  27.   padding: 0 20rpx;
  28.   align-items: center;
  29.   display: -webkit-flex;
  30. }

  31. .address_top {
  32.   height: 76rpx;
  33.   line-height: 76rpx;
  34.   background: #f4f4f4;
  35.   color: #999999;
  36.   font-size: 28rpx;
  37.   padding: 0 20rpx;
  38. }

  39. .address_bottom {
  40.   height: 88rpx;
  41.   line-height: 88rpx;
  42.   background: #fff;
  43.   color: #000000;
  44.   font-size: 32rpx;
  45.   padding: 0 20rpx;
  46.   border-bottom: 2rpx #ebebeb solid;
  47.   margin-left: 20rpx;
  48.   margin-right: 50rpx;
  49. }

  50. .location_img {
  51.   width: 48rpx;
  52.   height: 48rpx;
  53.   position: absolute;
  54.   right: 20rpx;
  55.   top: 125rpx;
  56. }

  57. .add_city {
  58.   width: 228rpx;
  59.   height: 60rpx;
  60.   line-height: 60rpx;
  61.   text-align: center;
  62.   border: 2rpx solid #ebebeb;
  63.   color: #000000;
  64.   margin-right: 20rpx;
  65. }

  66. .add_citying {
  67.   width: 228rpx;
  68.   height: 60rpx;
  69.   line-height: 60rpx;
  70.   text-align: center;
  71.   border: 2rpx solid #09bb07;
  72.   color: #09bb07;
  73.   margin-right: 20rpx;
  74. }

  75. .orientation {
  76.   white-space: normal;
  77.   display: inline-block;
  78.   width: 55rpx;
  79.   height: 58rpx;
  80.   color: #999;
  81.   text-align: center;
  82. }

  83. .orientation_region {
  84.   width: 55rpx;
  85.   font-size: 20rpx;
  86.   position: fixed;
  87.   top: 220rpx;
  88.   right: 0rpx;
  89. }

  90. .orientation_city {
  91.   height: 50rpx;
  92.   line-height: 50rpx;
  93.   color: #000;
  94.   text-align: center;
  95. }

  96. .orientation_select {
  97.   color: #ff0000;
  98. }
复制代码




581

主题

110

回帖

4066

积分

管理员

积分
4066

众神之神

 楼主| 发表于 2017-7-20 18:56:42 | 显示全部楼层
疑问:当锚点位置在界面的最顶端的时候,如何实时改变锚点楼层的选中状态;这个坑记得要填上!

这里需要计算楼层距离界面顶部位置:具体可以在参考这里得到思考:
WXML节点信息:http://mp.weixin.qq.com/debug/wxadoc/dev/api/wxml-nodes-info.html

581

主题

110

回帖

4066

积分

管理员

积分
4066

众神之神

 楼主| 发表于 2017-7-21 12:07:33 | 显示全部楼层
admin 发表于 2017-7-20 18:56
疑问:当锚点位置在界面的最顶端的时候,如何实时改变锚点楼层的选中状态;这个坑记得要填上!

这里需要计 ...

前来填坑,献上完美解决方案,重点还是上面说的WXML节点信息
还需要思考的是:如果展示数据量很大呢?一下加载全部显然不是最优的,看来滚动分页这个坑,还是得继续往下填!

WXML
  1. <scroll-view class="content" scroll-into-view="{{toView}}" scroll-y="true" scroll-with-animation="true" bindscroll="itemScroll">
  2.   <view wx:for="{{act_addList}}">
  3.     <view class="address_top items_wrap {{ 'cateGory'+item.id}}" id="{{ 'inToView'+item.id}}">{{item.region}}</view>
  4.     <view wx:for="{{item.city}}">
  5.       <view class="address_bottom">{{item.name}}</view>
  6.     </view>
  7.   </view>
  8. </scroll-view>
  9. <view class="orientation_region">
  10. <view class="orientation">自动定位</view>
  11.   <block wx:for="{{orientationList}}" >
  12.     <block wx:if="{{curCid == 'inToView'+item.id}}">
  13.       <view class="orientation_city orientation_select" bindtap="scrollToViewFn" data-id="{{item.id}}">{{item.region}}</view>
  14.     </block>
  15.     <block wx:else>
  16.       <view class="orientation_city" bindtap="scrollToViewFn" data-id="{{item.id}}">{{item.region}}</view>
  17.     </block>
  18.   </block>
  19. </view>
复制代码



JS
  1. Page({
  2.   /**
  3.    * 页面的初始数据
  4.    */
  5.   data: {
  6.     orientationList: [
  7.       { id: "01", region: "东北" },
  8.       { id: "02", region: "华北" },
  9.       { id: "03", region: "华东" },
  10.       { id: "04", region: "华南" },
  11.       { id: "05", region: "华中" },
  12.       { id: "06", region: "西北" },
  13.       { id: "07", region: "西南" }
  14.     ],
  15.     act_addList: [
  16.       {
  17.         id: "01", region: "东北地区",
  18.         city: [{ id: "0101", name: "白山江源" },
  19.           { id: "0102", name: "白山市" },
  20.           { id: "0103", name: "宾县" },
  21.           { id: "0104", name: "大庆" },
  22.           { id: "0105", name: "测试1" },
  23.           { id: "0106", name: "测试2" },
  24.           { id: "0107", name: "测试3" },
  25.           { id: "0108", name: "测试4" },
  26.           { id: "0109", name: "测试5" },
  27.           { id: "0110", name: "测试6" },
  28.         ]
  29.       },
  30.       {
  31.         id: "02", region: "华北地区",
  32.         city: [{ id: "0201", name: "包头" },
  33.           { id: "0202", name: "保定" },
  34.           { id: "0206", name: "测试2" },
  35.           { id: "0207", name: "测试3" },
  36.           { id: "0208", name: "测试4" },
  37.           { id: "0209", name: "测试5" },
  38.           { id: "0210", name: "测试6" },
  39.         ]
  40.       },
  41.       {
  42.         id: "03", region: "华东地区",
  43.         city: [{ id: "0303", name: "开封市" },
  44.           { id: "3104", name: "安阳市" },]
  45.       },
  46.       {
  47.         id: "04", region: "华南地区",
  48.         city: [
  49.           { id: "0401", name: "黑龙江市" },
  50.           { id: "0407", name: "测试3" },
  51.           { id: "0508", name: "测试4" },
  52.           { id: "0609", name: "测试5" },
  53.           { id: "0710", name: "测试6" },
  54.           { id: "0711", name: "测试8" },
  55.           { id: "0712", name: "测试9" },
  56.           { id: "0713", name: "测试10" },
  57.           { id: "0714", name: "测试11" },
  58.         ]
  59.       },
  60.       { id: "05", region: "华中地区", city: [{ id: "0501", name: "黑龙江市" }] },
  61.       {
  62.         id: "06", region: "西北地区",
  63.         city: [{ id: "0603", name: "开封市" },
  64.           { id: "0604", name: "安阳市" },]
  65.       },
  66.       {
  67.         id: "07", region: "西南地区",
  68.         city: [{ id: "0703", name: "开封市" },
  69.           { id: "0704", name: "安阳市" },
  70.           { id: "0401", name: "黑龙江市" },
  71.           { id: "0407", name: "测试3" },
  72.           { id: "0508", name: "测试4" },
  73.           { id: "0609", name: "测试5" },
  74.           { id: "0710", name: "测试6" },
  75.           { id: "0711", name: "测试8" },
  76.           { id: "0712", name: "测试9" },
  77.           { id: "0713", name: "测试10" },
  78.           { id: "0714", name: "测试11" },
  79.           { id: "0401", name: "黑龙江市" },
  80.           { id: "0407", name: "测试3" },
  81.           { id: "0508", name: "测试4" },
  82.           { id: "0609", name: "测试5" },
  83.           { id: "0710", name: "测试6" },
  84.           { id: "0711", name: "测试8" },
  85.           { id: "0712", name: "测试9" },
  86.           { id: "0713", name: "测试10" },
  87.           { id: "0714", name: "测试11" },
  88.         ]
  89.       },
  90.     ],
  91.     toView: 'inToView01',
  92.     curCid: 'inToView01'
  93.   },
  94.   scrollToViewFn: function (e) {
  95.     var _id = e.target.dataset.id;
  96.     this.setData({
  97.       toView: 'inToView' + _id
  98.     })
  99.   },
  100.   itemScroll: function (e) {
  101.     var self = this;
  102.     self.queryMultipleNodes();
  103.   },
  104.   onLoad: function (options) {
  105.   },
  106.   queryMultipleNodes: function(){
  107.     var self = this;
  108.     var query = wx.createSelectorQuery();
  109.     query.selectAll('.items_wrap').boundingClientRect();
  110.     query.selectViewport().scrollOffset();
  111.     query.exec(function(res){
  112.       var scrollSelectCid;
  113.       for (var value of res[0]) {
  114.         if (value.top <= 0) {
  115.           scrollSelectCid = value.id;
  116.         }
  117.       }
  118.       self.setData({
  119.         curCid: scrollSelectCid
  120.       });

  121.     })
  122.   }
  123. });
复制代码




WXSS
  1. page {
  2.   height: 100%;
  3. }

  4. .content {
  5.   padding-bottom: 20rpx;
  6.   box-sizing: border-box;
  7.   height: 100%;
  8. }

  9. .location {
  10.   width: 100%;
  11. }

  12. .location_top {
  13.   height: 76rpx;
  14.   line-height: 76rpx;
  15.   background: #f4f4f4;
  16.   color: #606660;
  17.   font-size: 28rpx;
  18.   padding: 0 20rpx;
  19. }

  20. .location_bottom {
  21.   height: 140rpx;
  22.   line-height: 140rpx;
  23.   color: #d91f16;
  24.   font-size: 28rpx;
  25.   border-top: 2rpx #ebebeb solid;
  26.   border-bottom: 2rpx #ebebeb solid;
  27.   padding: 0 20rpx;
  28.   align-items: center;
  29.   display: -webkit-flex;
  30. }

  31. .address_top {
  32.   height: 76rpx;
  33.   line-height: 76rpx;
  34.   background: #f4f4f4;
  35.   color: #999999;
  36.   font-size: 28rpx;
  37.   padding: 0 20rpx;
  38. }

  39. .address_bottom {
  40.   height: 88rpx;
  41.   line-height: 88rpx;
  42.   background: #fff;
  43.   color: #000000;
  44.   font-size: 32rpx;
  45.   padding: 0 20rpx;
  46.   border-bottom: 2rpx #ebebeb solid;
  47.   margin-left: 20rpx;
  48.   margin-right: 50rpx;
  49. }

  50. .location_img {
  51.   width: 48rpx;
  52.   height: 48rpx;
  53.   position: absolute;
  54.   right: 20rpx;
  55.   top: 125rpx;
  56. }

  57. .add_city {
  58.   width: 228rpx;
  59.   height: 60rpx;
  60.   line-height: 60rpx;
  61.   text-align: center;
  62.   border: 2rpx solid #ebebeb;
  63.   color: #000000;
  64.   margin-right: 20rpx;
  65. }

  66. .add_citying {
  67.   width: 228rpx;
  68.   height: 60rpx;
  69.   line-height: 60rpx;
  70.   text-align: center;
  71.   border: 2rpx solid #09bb07;
  72.   color: #09bb07;
  73.   margin-right: 20rpx;
  74. }

  75. .orientation {
  76.   white-space: normal;
  77.   display: inline-block;
  78.   width: 55rpx;
  79.   height: 58rpx;
  80.   color: #999;
  81.   text-align: center;
  82. }

  83. .orientation_region {
  84.   width: 55rpx;
  85.   font-size: 20rpx;
  86.   position: fixed;
  87.   top: 220rpx;
  88.   right: 0rpx;
  89. }

  90. .orientation_city {
  91.   height: 50rpx;
  92.   line-height: 50rpx;
  93.   color: #000;
  94.   text-align: center;
  95. }

  96. .orientation_select {
  97.   color: #ff0000;
  98. }
复制代码





您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

Archiver|手机版|小黑屋|十三博客 ( 鲁ICP备2023000528号 )

GMT+8, 2026-6-1 19:24 , Processed in 0.051206 second(s), 21 queries .

Powered by Discuz! X3.5

© 2001-2024 Discuz! Team.

快速回复 返回顶部 返回列表