当前位置:   article > 正文

uni-app使用scroll-view实现锚点定位和滚动监听功能_uniapp锚点定位和滚动监听

uniapp锚点定位和滚动监听

1.html代码:

  1. <template>
  2. <view class="classicsBox">
  3. <scroll-view class="classics-left" scroll-y="true" scroll-with-animation :scroll-into-view="clickId">
  4. <view v-for="(item,index) in contentData" :key="item.id" :id="item.id" class="classics-left-item" :class="picked==index?'checkedStyle':''" @click="selectActiveEvt(item)">
  5. {{item.title}}
  6. </view>
  7. </scroll-view>
  8. <!-- :scroll-top="scrollRightTop" -->
  9. <scroll-view scroll-y="true" :scroll-into-view="clickId" :scroll-anchoring="true" scroll-with-animation class="classics-right" @scroll="scrollEvt">
  10. <view class="classics-right-item" v-for="item in contentData" :key="item.id" :id="item.id">
  11. <view class="title">
  12. {{item.title}}
  13. </view>
  14. <view class="item-box" v-for="it in item.content">
  15. <img class="item-box-left" :src="it.img" alt="">
  16. <view class="item-box-right">
  17. <view class="item-box-right-name">
  18. {{it.name}}
  19. </view>
  20. <view class="item-box-right-describe">
  21. {{it.desc}}
  22. </view>
  23. <view class="item-box-right-buy">
  24. <view class="item-box-right-price">
  25. ¥{{it.price}}
  26. </view>
  27. <view class="item-box-right-pick">
  28. 选规格
  29. </view>
  30. </view>
  31. </view>
  32. </view>
  33. </view>
  34. </scroll-view>
  35. </view>
  36. </template>

2.script代码

  1. <script>
  2. export default {
  3. components: {},
  4. data() {
  5. return {
  6. contentData:[{
  7. id:'tab1',
  8. title:'热销专区',
  9. content:[
  10. {id:101,img:'static/rexiao1.png',name:'郁金香',desc:'花语',price:'99',},
  11. {id:102,img:'static/rexiao2.png',name:'郁金香',desc:'花语',price:'99',},
  12. ]
  13. },
  14. {
  15. id:'tab2',
  16. title:'生日鲜花',
  17. content:[
  18. {id:201,img:'static/shengri1.png',name:'郁金香',desc:'花语',price:'99',},
  19. {id:202,img:'static/shengri2.png',name:'郁金香',desc:'花语',price:'99',},
  20. ]
  21. },......],//列表数据
  22. clickId:'tab1', //点击选项的id
  23. picked:0, // 左侧选中选项的index
  24. nowRightIndex:0, // 右边当前滚动的index
  25. itemArr:[], //用于存放右侧item位置数据
  26. scrollRightTop:0,
  27. timer:null,
  28. }
  29. },
  30. methods: {
  31. // 左侧切换点击事件
  32. selectActiveEvt(e) {
  33. this.clickId = e.id;
  34. this.picked = this.contentData.findIndex(it=>it.id==e.id);
  35. },
  36. scrollEvt(e){
  37. // 防抖
  38. if(this.timer){
  39. clearTimeout(this.timer);
  40. }
  41. this.timer = setTimeout(()=>{
  42. this.nowRightIndex = this.itemArr.findLastIndex(it=>e.detail.scrollTop>=(it.bottom-228))+1; //判断当前顶部是处于哪个item,获取当前item的index
  43. if(this.nowRightIndex==-1) this.nowRightIndex=0;
  44. if(this.nowRightIndex==this.picked) return;
  45. this.clickId = this.contentData[this.nowRightIndex].id;
  46. this.picked = this.nowRightIndex;
  47. },500);
  48. },
  49. // 计算右侧每个item到顶部的距离,存放到数组
  50. getItemDistence(){
  51. new Promise(resolve=>{
  52. let selectQuery = uni.createSelectorQuery().in(this);
  53. selectQuery.selectAll('.classics-right-item').boundingClientRect(rect=>{
  54. if(!rect.length){
  55. setTimeout(()=>{
  56. this.getItemDistence();
  57. },10);
  58. return;
  59. }
  60. rect.forEach(it=>{
  61. this.itemArr.push(it); // 这里获取到的数据是每个item距离页面顶部的数据,以及每个item的自身数据
  62. resolve();
  63. })
  64. }).exec()
  65. })
  66. },
  67. },
  68. mounted(){
  69. // 设置一个延时,确保所有dom和样式加载完成,否则拿到的数据可能有误
  70. setTimeout(()=>{
  71. this.getItemDistence();
  72. },500)
  73. },
  74. }
  75. </script>

 3.css样式

  1. .classicsBox{
  2. box-sizing: border-box;
  3. width: 100%;
  4. height: 100%;
  5. display: flex;
  6. padding-top: 8px;
  7. }
  8. .classics-left{
  9. width: 25%;
  10. height: 100%;
  11. box-sizing: border-box;
  12. padding-right: 6px;
  13. /* overflow-y: auto; */
  14. }
  15. .classics-left-item{
  16. height: 60px;
  17. box-sizing: border-box;
  18. text-align: center;
  19. line-height: 60px;
  20. color: black;
  21. font-weight: 600;
  22. font-size: 14px;
  23. }
  24. .classics-left-item:last-child{
  25. border: none;
  26. }
  27. .classics-right{
  28. background-color: #fff;
  29. width: 75%;
  30. box-sizing: border-box;
  31. /* overflow-y: auto; */
  32. }
  33. .classics-right-item{
  34. width: 100%;
  35. box-sizing: border-box;
  36. }
  37. .title{
  38. height: 50px;
  39. line-height: 50px;
  40. font-weight: 600;
  41. font-size: 14px;
  42. background: url('../../../static/itembackg.png') center center no-repeat ;
  43. background-size: 100%;
  44. box-sizing: border-box;
  45. padding-left: 10px;
  46. color: #87ff07;
  47. font-family: 'Courier New', Courier, monospace;
  48. /* -webkit-text-stroke: 1px #ff55ff; */
  49. }
  50. .item-box{
  51. display: flex;
  52. width: 100%;
  53. box-sizing: border-box;
  54. padding-bottom: 20px;
  55. padding-left: 10px;
  56. }
  57. .item-box-left{
  58. width: 90px;
  59. height: 90px;
  60. background-color: #fafafa;
  61. border-radius: 8px;
  62. box-sizing: border-box;
  63. }
  64. .item-box-right{
  65. width: calc(100% - 102px);
  66. box-sizing: border-box;
  67. padding-left: 12px;
  68. position: relative;
  69. box-sizing: border-box;
  70. background-color: #fff;
  71. }
  72. .item-box-right-name,.item-box-right-price{
  73. color: black;
  74. font-weight: 600;
  75. font-size: 14px;
  76. }
  77. .item-box-right-describe{
  78. color: #909090;
  79. font-size: 12px;
  80. }
  81. .item-box-right-buy{
  82. width: 100%;
  83. position: absolute;
  84. bottom: 0;
  85. display: flex;
  86. justify-content: space-between;
  87. box-sizing: border-box;
  88. padding-right: 12px;
  89. }
  90. .item-box-right-pick{
  91. background: linear-gradient(#ffa7b7,#ffcee4);
  92. padding: 4px 6px;
  93. border-radius: 12px;
  94. color: #ffffff;
  95. font-size: 12px;
  96. }
  97. .checkedStyle{
  98. background-color: #ffffff;
  99. border-left: 5px solid #ffb3c8;
  100. box-sizing: border-box;
  101. color: #ff797b;
  102. border-radius: 0 20px 20px 0;
  103. }

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/Cpp五条/article/detail/312523
推荐阅读
相关标签
  

闽ICP备14008679号