实现锚点效果scroll-view的scroll-into-view属性+微信小程序手动画表格

10 篇文章 0 订阅
2 篇文章 0 订阅

 1.实现效果列表中每一项都有一个属性值,如果该属性值为1,则进入页面直接滑动该列数据到列表顶部(以下为自己画)(注:代码中实现滑动练习demo(效果ok)标注的代码为正常的列表滑动可以参考)

1-0.实现例子所示

如图所示,该列表中12/8为属性是1,则该列在最上面显示(此图为12/8下面数据太少,所以没有到顶部)

1-1设计图

以下是微信小程序中自己画的表格(根据ui和后端数据相结合画的有点乱哦~)

1-1wxml

<!--pages/mySchedulePage/mySchedulePage.wxml-->
<view class="page">
  <image class="bg_img" src="{{bgUrl}}" mode="" />
  <view class="dtwion">
    <view bindtap="changeuseBack">
      <image class="chageuseback" src="../../images/change-user-back.png" mode="" />
    </view>
    <image class="runtext" src="../../images/myschel.png" mode="" />
  </view>
  <view wx:if="{{isOver==false}}">
    <view class="opschele">
      <view class="onedate">日期</view>
      <view class="twotime">时间</view>
      <view class="threebattle">对阵</view>
      <view class="fourachiev">成绩</view>
    </view>
    <!-- 表格 -->
    <scroll-view class="tabfirst" scroll-y scroll-into-view="{{'item-'+toView}}" scroll-with-animation="true">
      <!-- <view class="tabfirst"> -->
      <!-- <table> -->
      <!-- 实现滑动练习demo(效果ok)-->
      <!-- <view wx:for="{{list}}" wx:for-item="item" wx:for-index="index">
      <view id="{{'item-'+index}}" class="list-item" bindtap="jumpTo">
        {{item.item}}
      </view>
    </view> -->
    
      <!-- 页面代码 -->
      <view class="allblock" wx:for="{{listArry}}" wx:for-item="item" wx:for-index="index">
        <view class="firstlist" id="{{'item-'+index}}" bindtap="jumpTo">
          <view class="oneblock">
            <view class="righttdate">
              <view class="rigtex">{{item.newTime}}</view>
            </view>
            <view style="display: flex; ">
              <view class="twoblocka" wx:for="{{item.qpScheduleList}}" wx:if="{{indexx===0&&(itemm.timeType==1)}}" wx:for-item="itemm" wx:for-index="indexx">
                <view class="rightimea">全天</view>
              </view>
              <view style="display: flex; flex-direction: column;">
                <view wx:for="{{item.qpScheduleList}}" wx:for-item="itemm" wx:for-index="indexx" wx:if="{{itemm.timeType==2}}">
                  <view class="twoblock">
                    <view class="rightime">{{itemm.startTime}}-{{itemm.endTime}}</view>
                  </view>
                </view>
              </view>
              <view>
                <view style="display: flex; flex-direction: column;" wx:for="{{item.qpScheduleList}}" wx:for-item="itemm" wx:for-index="indexx">
                  <view class="threeblock" wx:if="{{itemm.isRandom==1}}">
                    <view class="leftcontent">随机组队</view>
                  </view>
                  <view class="threeblock" wx:if="{{itemm.isRandom==0}}">
                    <view class="leftcontent">{{itemm.vsStr}}</view>
                  </view>
                </view>
              </view>

              <view style="display: flex;flex-direction: column;">
                <view class="fourblock" wx:for="{{item.qpScheduleList}}" wx:for-item="itemm" wx:for-index="indexx">
                  <view wx:if="{{itemm.result==0}}" class="leftrton">-</view>
                  <view wx:if="{{itemm.result==1}}" class="leftrighton">胜利</view>
                  <view wx:if="{{itemm.result==2}}" class="leftlefton">失败</view>
                </view>
              </view>
            </view>
          </view>
        </view>
      </view>


      <!-- </table> -->


    </scroll-view>
    <view style="height: 10rpx;width: 100%;"></view>
  </view>
  <view wx:if="{{isOver==true}}">
    <view class="obowr">暂无数据</view>
  </view>
</view>

1-2 wxss

/* pages/mySchedulePage/mySchedulePage.wxss */
.page {
  width: 100%;
  height: 100vh;
  background: rgba(0, 0, 0, 0.18);
}
.bg_img{
  position: fixed;
  width: 100vw;
  height: 100vh;
  z-index: -1;
}
.dtwion{
  display: flex;
}
.chageuseback{
  width: 17rpx;
  height: 15rpx;
  margin-top: 25rpx;
  margin-left: 29rpx;
}
.runtext{
  width: 70rpx;
  height: 15rpx;
  margin-top: 37rpx;
  margin-left: 293rpx;
}
.opschele{
  display: flex;
  margin-top: 23rpx;
  padding-bottom: 10rpx;
}
.onedate{
  font-size: 12rpx;
   height: 16rpx;
   line-height: 14rpx;
   color: #ffffff;
   font-weight: 500;
   margin-left: 79rpx;
}
.twotime{
  font-size: 12rpx;
  height: 16rpx;
  line-height: 14rpx;
  color: #ffffff;
  font-weight: 500;
  margin-left: 112rpx;
}
.threebattle{
  font-size: 12rpx;
  height: 16rpx;
  line-height: 14rpx;
  color: #ffffff;
  font-weight: 500;
  margin-left: 203rpx;
}
.fourachiev{
  font-size: 12rpx;
  height: 16rpx;
  line-height: 14rpx;
  color: #ffffff;
  font-weight: 500;
  margin-left: 184rpx;
}
/* 表格 */
.tabfirst{
  overflow: hidden;
  width: 683rpx;
  margin-left: 34rpx;
  max-height: 237rpx;
  border-top:1rpx solid #1BD5EC;
  border-left: 1rpx solid #1BD5EC; 
  border-right: 1rpx solid #1BD5EC; 
  border-radius: 3rpx;

}
.allblock{
  align-items: center;
}
.firstlist{
  display: flex;
  background-color: #00000020;
}
.oneblock{
  display: flex;
  text-align: center;
}
.twoblocka{
  position: relative;
  width: 151rpx;
  text-align: center;
  border-right: 1rpx solid #1BD5EC; 
  border-left: 1rpx solid #1BD5EC; 
  border-bottom: 1rpx solid #1BD5EC;
}
.twoblock{
  width: 151rpx;
  height: 36rpx;
  text-align: center;
  border-right: 1rpx solid #1BD5EC; 
  border-left: 1rpx solid #1BD5EC; 
  border-bottom: 1rpx solid #1BD5EC;
}
.threeblock{
  width: 281rpx;
  height: 36rpx;
  text-align: center;
  /* border-top:1rpx solid #1BD5EC; */
  border-right: 1rpx solid #1BD5EC; 
  border-bottom: 1rpx solid #1BD5EC;
}
.fourblock{
  width: 136rpx;
  height: 36rpx;
  text-align: center;
  border-bottom: 1rpx solid #1BD5EC;
}
.righttdate{
  position: relative;
  width: 113rpx;
  font-size: 12rpx;
  font-weight: 500;
  color: #ffffff;
  padding-top: 11rpx;
  /* align-items: center; */
  padding: 0,auto;
  text-align: center;
  border-bottom: 1rpx solid #1BD5EC;

}
.rigtex{
  position: absolute;
  top:50%;
  left: 50%;
  transform: translate(-50%,-50%);
  text-align: center;
  height: 16rpx;
}
.rightimea{
  position: absolute;
  top:50%;
  left: 50%;
  transform: translate(-50%,-50%);
  font-size: 12rpx;
  font-weight: 500;
  color: #ffffff;
}
.rightime{
  font-size: 12rpx;
  font-weight: 500;
  color: #ffffff;
  padding-top: 11rpx;
}
.leftcontent{
  font-size: 12rpx;
  font-weight: 500;
  color: #ffffff;
  /* height: 35rpx; */
  padding-top: 11rpx;
}
.leftrton{
  width: 23rpx;
  height: 16rpx;
  font-size: 12rpx;
  font-weight: 500;
  color: #3DFFD0;
  line-height: 14rpx;
  padding-top: 11rpx;
  padding-left: 65rpx;
}
.leftrighton{
  width: 23rpx;
  height: 16rpx;
  font-size: 12rpx;
  font-weight: 500;
  color: #3DFFD0;
  line-height: 14rpx;
  padding-top: 11rpx;
  padding-left: 57rpx;
}
.leftlefton{
  width: 23rpx;
  height: 16rpx;
  font-size: 12rpx;
  font-family: PingFang SC, PingFang SC;
  font-weight: 500;
  color: #FF8A58;
  line-height: 14rpx;
  padding-top: 11rpx;
  padding-left: 57rpx;
}
.obowr{
  font-size: 30rpx;
  color: #ffffff;
  margin-left: 313rpx;
  margin-top: 100rpx;
}

1-3  js

// pages/mySchedulePage/mySchedulePage.js
var http = require('../../utils/http')
Page({

  /**
   * 页面的初始数据
   */
  data: {
    toView:'',
    scrollTop: 0,
    list:[
      {
        item:'Item 1',
        anchor:0
      },
      {
        item:'Item 2',
        anchor:0
      },
      {
        item:'Item 3',
        anchor:0
      },
      {
        item:'Item 4',
        anchor:1
      },
      {
        item:'Item 5',
        anchor:0
      },
      {
        item:'Item 6',
        anchor:0
      },
      {
        item:'Item 7',
        anchor:0
      },
      {
        item:'Item 8',
        anchor:0
      },
      {
        item:'Item 9',
        anchor:0
      },
      {
        item:'Item 10',
        anchor:0
      },
      {
        item:'Item 11',
        anchor:0
      },
      {
        item:'Item 12',
        anchor:0
      },
      {
        item:'Item 13',
        anchor:0
      },
      {
        item:'Item 4',
        anchor:0
      },
      {
        item:'Item 15',
        anchor:0
      },],
    anchor:'',//接口返回的需要锚点的地方
    intoIndex: '', // 前端要锚点的地方
    isOver:'',
    newTime:'',//日期
    bgUrl:'https://ydhv4.oss-cn-beijing.aliyuncs.com/ee6ceb5ae183eb696e28cda3437f4ca0f5b0da156e68c-VuyMTT_fw1200webp%201.png',
    isSucceed:'',
    // listArry:[
    //   {
    //     data:'2023-11-01 15:11:23',
    //     message:[
    //       {
    //         red:[{name:'林俊杰'},{name:'周杰伦'}],
    //         blue:[{name:'吴彦祖'},{name:'彭于晏'}],
    //         text:[
    //           {
    //             nameA:'林俊杰'
    //           },
    //           {
    //             nameA:'林俊杰'
    //           },
    //           {
    //             nameB:'林俊杰'
    //           },
    //           {
    //             nameB:'林俊杰'
    //           }
    //         ],
    //         isSucceed:0,//1胜利,0失败
    //         timeType:1,
    //         startime:'2023-11-01 15:11:23',
    //         endtime:'2023-11-01 15:11:23',
    //         type:1//组队类型:1随机,2固定
    //       }
    //     ]
    //   },
    
    // ],
    listArry:{},//大数组
    qpScheduleList:{},//里面赛程
    selarry:[],//对决人名
    actionInfo:'',
    userid:''

  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    let userid = wx.getStorageSync('userid')
    let action = wx.getStorageSync('actioninfo')
    this.setData({
      actionInfo:action,
      userid:userid
    })
    // 实现进入页面新的数据在最上面实现
    // setTimeout(() => {
    //   this.scrollToItem();
    // }, 100); 
    // this.jumpTo()
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {
    
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {
    let isOver=this.data.isOver
    if(isOver==false){
      this.getMessgae()
    }

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  },
  // 滚动时候调用
  // scrollToItem(){
  //   const itemId = 'item-2'; // 设置要滚动到的项的 id
  //   this.setData({
  //     scrollIntoView: itemId
  //   });
  // },

  jumpTo(e){
    console.log(this.data.listArry)
    console.log(this.data.list)
    let index = e.currentTarget.dataset.index;
    console.log(index)
    console.log(this.data.listArry[index])
    let anchor=this.data.listArry[index].anchor
    // let target = 2
    if (anchor==1) {
      this.setData({
        toView:'item' + index
      })
      console.log('第几个锚点',this.data.toView)
    }
    console.log('第几个锚点',this.data.toView)
  },
  onScrollView(e) {
    this.setData({
      scrollTop: e.detail.scrollTop
    });
  },
// 实现滑动练习demo(效果ok)
  // jumpTo(e){
  //   let index = e.currentTarget.dataset.index;
  //   console.log(index)
  //   console.log(this.data.list[index])
  //   let anchor=this.data.list[index].anchor
  //   // let target = 2
  //   if (anchor==1) {
  //     this.setData({
  //       toView:index.toString
  //     })
  //     console.log('第几个锚点',this.data.toView)
  //   }
  //   console.log('第几个锚点',this.data.toView)
  // },
  changeuseBack(){
    wx.navigateBack({
      delta:1
    })
  },
  getMessgae(){
    let selfThis=this
    let selectList={}
    let actionInfo=selfThis.data.actionInfo
    let userid=selfThis.data.userid
    let activityId=actionInfo.id
    selectList.activityId=activityId
    selectList.userId=userid
      var params = {
      url: "/scheduleRest/list",
      method: "GET",
      data:selectList,
      callBack: function(res) {
       if(res.data){ 
        let getmessage=res.data
        let index = 0;
        let anchorIndex = -1;
        for(let item of getmessage){
         let anchor=item.anchor
         if(anchor==1){
            anchorIndex = index;
         }
         let getDate=item.date
         let neyear=getDate.slice(0,4)
         let nemouth=getDate.slice(5,7)
         let neday=getDate.slice(8,10)
         item.newTime=neyear+'/'+nemouth+'/'+neday 
        let qpScheduleList=item.qpScheduleList
        for(let item of qpScheduleList){
          //时间
           let newstart=item.startTime
           let newend=item.endTime
           let newStrstart=newstart.slice(11,16)
           let newStrend=newend.slice(11,16)
           item.startTime=newStrstart
           item.endTime=newStrend
          //  console.log(qpScheduleList)
          //  console.log(item.timeType)
         }

         index++;
        } 
          selfThis.setData({
          listArry:getmessage,
          isOver:false
          }) 
          if(anchorIndex != -1){
              selfThis.setData({
                toView:anchorIndex
              });
          }
        
       }else{
        selfThis.setData({
          isOver:true
         })
       }
      },
    };
    http.request(params);
  }
})

3.

  • 9
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
根据提供的引用内容,实现微信小程序scroll-viewscroll-into-view效果可能是由以下几个原因导致的: 1. scroll-into-view 属性的值与要跳转到的点标记的 id 属性值一致。例如,如果要跳转到一个 id 为 "anchor1" 的点标记,那么 scroll-into-view 的值应该设置为 "anchor1"。 2. scroll-view 容器高度不足:如果 scroll-view 容器的高度不足以显示要跳转到的点标记,那么 scroll-into-view 将无法生效。确保 scroll-view 容器的高度足够大,以容纳所有的内容和要跳转到的点标记。 3. scroll-into-view 设置在子组件上:scroll-into-view 属性应该设置在 scroll-view 组件上,而不是其子组件上。确保 scroll-into-view 属性被正确地设置在 scroll-view 组件上。 4. scroll-into-view 设置在动态生成的内容上:如果要跳转到的点标记是在动态生成的内容中,那么需要在动态生成内容之后再设置 scroll-into-view 属性。因为 scroll-into-view 属性需要等待内容渲染完成后才能生效。 5. scroll-into-view 设置在隐藏的内容上:如果要跳转到的点标记是在初始状态下是隐藏的内容中,那么需要在显示该内容后再设置 scroll-into-view 属性。因为 scroll-into-view 属性需要等待内容显示后才能生效。 以下是一个示例代码,演示了如何在微信小程序中使用 scroll-viewscroll-into-view 实现点跳转效果: ```html <scroll-view scroll-into-view="{{toView}}" scroll-y="true" style="height: 300px;"> <view id="anchor1">点1</view> <view id="anchor2">点2</view> <view id="anchor3">点3</view> <view id="anchor4">点4</view> <view id="anchor5">点5</view> </scroll-view> <button bindtap="scrollToAnchor">跳转到点2</button> ``` ```javascript Page({ data: { toView: '' }, scrollToAnchor: function() { this.setData({ toView: 'anchor2' }) } }) ``` 在上述示例中,scroll-view 组件设置了 scroll-into-view 属性为 toView 变量的值,当点击按钮时,toView 变量的值被设置为 "anchor2",从而实现了跳转到点2的效果

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值