欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

微信小程序scroll-view指定滾動(dòng)元素起始位置怎么做

 更新時(shí)間:2022年12月26日 10:21:45   作者:落雪小軒韓  
scroll-view為滾動(dòng)視圖,分為水平滾動(dòng)和垂直滾動(dòng),這篇文章主要給大家介紹了關(guān)于微信小程序scroll-view不能左右滑動(dòng)問題的解決方法,需要的朋友可以參考下

scroll-into-view屬性,值為某子元素的id,不能以數(shù)字開頭,設(shè)置哪個(gè)方向滾動(dòng),則在哪個(gè)方向上滾動(dòng)到該元素。

使用場(chǎng)景一:查看當(dāng)前日期之前的數(shù)據(jù)(需求:初始化時(shí)為當(dāng)前日期,然后從右往左滑動(dòng))

  <scroll-view enable-flex scroll-x class="scroll-x" scroll-into-view="{{scrollIntoView}}" style="height: 120rpx;width: 100%;">
    <view id="{{item.id ? item.id : index}}" wx:for="{{monthList}}" wx:key="index" class="view_item" style="position: relative;">
      <view wx:if="{{item.month == 1}}" style="position: absolute;top: -55rpx;left:40rpx;font-size: 24rpx;color:#a1a1a1;height: 60rpx;">{{item.year}}</view>
      <view style="background-color: {{item.background ? item.background : '#f0f8fa'}};" class="view_item_time" bindtap="clickItem" data-item="{{index}}">{{item.month}}月</view>
    </view>
  </scroll-view>

初始化數(shù)據(jù)時(shí),給最后一個(gè)item元素設(shè)置一個(gè)id為left,然后再設(shè)置scroll-into-view屬性的值為left即可

注意點(diǎn):要先加載完列表數(shù)據(jù)再設(shè)置scroll-into-view屬性的值

使用場(chǎng)景二:scroll-view結(jié)合日歷組件,默認(rèn)從當(dāng)前日期開始向右滑動(dòng),日歷選擇哪個(gè)日期,scroll-view就從哪個(gè)日期開始滑動(dòng),可選日期為當(dāng)前日期后一個(gè)月。

處理數(shù)據(jù):

  data: {
    dateList:[],
    timeList:[],
    showCalender: false,
    date: "",
    minDate:'',
    maxDate:'',
    scrollIntoView:'',
  },
 // 獲取當(dāng)前年月日
  getCurrentDate(index) {
    var datetime = new Date();
    var year = datetime.getFullYear();
    var month = datetime.getMonth() + 1 < 10 ? '0' + (datetime.getMonth() + 1) : datetime.getMonth() + 1;
    var date = datetime.getDate() < 10 ? '0' + datetime.getDate() : datetime.getDate();
    var time = year + '/' + month + '/' + date
    // 獲取當(dāng)前日期加上30天后的日期,限制日歷只能選擇后30天
    var date1 = new Date();
    var date2 = new Date(date1);
    // 設(shè)置一個(gè)月的某一天
    date2.setDate(date1.getDate() + 30);
    let dateLater = date2.getFullYear() + "/" + (date2.getMonth() + 1) + "/" + date2.getDate()
    // 日歷中的最小和最大可選日期
    this.setData({minDate:new Date(time).getTime(),maxDate:new Date(dateLater).getTime()})
    // 獲取當(dāng)前日期加上某天后的日期
    let dateList = [],timeList = [],weekList = []
    for(let i = 0;i <= 30;i++) {
      dateList[i] = new Date(date1)
      // 列表展示需要的數(shù)據(jù)
      dateList[i].setDate(date1.getDate() + i)
      // 請(qǐng)求接口需要的完整日期數(shù)據(jù)
      timeList[i] = dateList[i].getFullYear() + "/" + (dateList[i].getMonth() + 1)  + "/" + dateList[i].getDate()
      // 獲取星期幾
      weekList[i] = dateList[i].getDay()
    }
    dateList = dateList.map(item => {
      return item.getDate()
    })
    weekList = weekList.map(item => {
      if(item === 0) {
        return '周日'
      } else if(item == 1) {
        return '周一'
      } else if(item == 2) {
        return '周二'
      } else if(item == 3) {
        return '周三'
      } else if(item == 4) {
        return'周四'
      } else if(item == 5) {
        return '周五'
      } else if(item == 6) {
        return '周六'
      }
    })
    dateList = dateList.map(item => {
      return {date:item}
    })
    if(index) {
    	 // 選擇了日歷中的日期,設(shè)置對(duì)應(yīng)滑動(dòng)列表中的id
      dateList[index].background = '#00bcd4'
      dateList[index].color = '#fff'
      dateList[index].id = 'target'
    } else {
    	 // 沒有選擇日歷,默認(rèn)設(shè)置滑動(dòng)列表中第一條數(shù)據(jù)的id
      dateList[0].background = '#00bcd4'
      dateList[0].color = '#fff'
      dateList[0].id = 'target'
    }
    dateList.map((item1,index1) => {
      weekList.map((item2,index2) => {
        if(index1 === index2) {
          item1.week = item2
        }
      })
    })
    // 給scroll-view設(shè)置
    this.setData({dateList,timeList,scrollIntoView:'target'})
  },

日歷

<van-calendar poppable row-height='50' min-date="{<!--{cke_protected}{C}%3C!%2D%2D%20%2D%2D%3E-->{minDate}}" max-date="{<!--{cke_protected}{C}%3C!%2D%2D%20%2D%2D%3E-->{maxDate}}" show="{<!--{cke_protected}{C}%3C!%2D%2D%20%2D%2D%3E-->{ showCalender }}" color='rgba(0, 188, 212, 1)' show-title='{<!--{cke_protected}{C}%3C!%2D%2D%20%2D%2D%3E-->{false}}' close-on-click-overlay show-confirm bind:confirm="chooseDate" bind:close='closeCalender' />

滑動(dòng)列表

<scroll-view enable-flex scroll-x class="scroll-x" scroll-into-view="{{scrollIntoView}}">
  <view id="{{item.id ? item.id : index}}" style="background-color: {{item.background ? item.background : '#fff'}};color: {{item.color ? item.color : '#000'}};" class="date-item" bindtap="clickDate" wx:for="{{dateList}}" wx:key="index" data-index="{{index}}">
     <view class="item-week">{{item.week}}</view>
     <view class="item-date">{{item.date}}</view>
   </view>
</scroll-view>

選擇日期

  chooseDate(e) {
    let timeStamp = new Date(Date.parse(e.detail))
    let year = timeStamp.getFullYear()
    let month = timeStamp.getMonth() + 1
    let day = timeStamp.getDate()
    let date = `${year}/${month}/${day}`
    this.setData({
      date,
      showCalender: false
    })
    this.data.timeList.map((item,index) => {
      if(item === date) { 
      // 選中日歷中的日期與滑動(dòng)列表中的日期相等
      // 每選一次日歷日期都重新渲染一下滑動(dòng)列表中的數(shù)據(jù)
        this.getCurrentDate(index)
        setTimeout(() => { this.getCurrentDate(index)},500)
      } else {              
        this.data.dateList[index].id = index
        this.data.dateList[index].background = '#fff'
        this.data.dateList[index].color = '#000'
      }
    })
    this.setData({dateList:this.data.dateList})
  },

到此這篇關(guān)于微信小程序scroll-view指定滾動(dòng)元素起始位置怎么做的文章就介紹到這了,更多相關(guān)小程序scroll-view內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

最新評(píng)論