微信小程序scroll-view指定滾動元素起始位置怎么做
scroll-into-view屬性,值為某子元素的id,不能以數(shù)字開頭,設(shè)置哪個方向滾動,則在哪個方向上滾動到該元素。
使用場景一:查看當前日期之前的數(shù)據(jù)(需求:初始化時為當前日期,然后從右往左滑動)

<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ù)時,給最后一個item元素設(shè)置一個id為left,然后再設(shè)置scroll-into-view屬性的值為left即可
注意點:要先加載完列表數(shù)據(jù)再設(shè)置scroll-into-view屬性的值
使用場景二:scroll-view結(jié)合日歷組件,默認從當前日期開始向右滑動,日歷選擇哪個日期,scroll-view就從哪個日期開始滑動,可選日期為當前日期后一個月。

處理數(shù)據(jù):
data: {
dateList:[],
timeList:[],
showCalender: false,
date: "",
minDate:'',
maxDate:'',
scrollIntoView:'',
},
// 獲取當前年月日
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
// 獲取當前日期加上30天后的日期,限制日歷只能選擇后30天
var date1 = new Date();
var date2 = new Date(date1);
// 設(shè)置一個月的某一天
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()})
// 獲取當前日期加上某天后的日期
let dateList = [],timeList = [],weekList = []
for(let i = 0;i <= 30;i++) {
dateList[i] = new Date(date1)
// 列表展示需要的數(shù)據(jù)
dateList[i].setDate(date1.getDate() + i)
// 請求接口需要的完整日期數(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è)置對應(yīng)滑動列表中的id
dateList[index].background = '#00bcd4'
dateList[index].color = '#fff'
dateList[index].id = 'target'
} else {
// 沒有選擇日歷,默認設(shè)置滑動列表中第一條數(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' />滑動列表
<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) {
// 選中日歷中的日期與滑動列表中的日期相等
// 每選一次日歷日期都重新渲染一下滑動列表中的數(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指定滾動元素起始位置怎么做的文章就介紹到這了,更多相關(guān)小程序scroll-view內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
微信小程序開發(fā)之map地圖組件定位并手動修改位置偏差
這篇文章主要介紹了微信小程序開發(fā)之map地圖組件,定位,并手動修改位置偏差,本文給大家介紹的非常詳細,具有一定的參考借鑒價值,需要的朋友可以參考下2019-08-08
js AppendChild與insertBefore用法詳細對比
本篇文章主要是對js中AppendChild與insertBefore的用法進行了詳細的對比。需要的朋友可以過來參考下,希望對大家有所幫助2013-12-12
JS實現(xiàn)DOM節(jié)點插入操作之子節(jié)點與兄弟節(jié)點插入操作示例
這篇文章主要介紹了JS實現(xiàn)DOM節(jié)點插入操作之子節(jié)點與兄弟節(jié)點插入操作,涉及JavaScript節(jié)點的創(chuàng)建、添加簡單操作技巧,需要的朋友可以參考下2018-07-07
javascript 移動鼠標得到單元格所在table表中的rowIndex位置[兼容ie,firefox]
移動鼠標,得到單元格所在表中的位置,主要是學(xué)習使用js的e.srcElement.2009-12-12
Bootstrap table 服務(wù)器端分頁功能實現(xiàn)方法示例
這篇文章主要介紹了Bootstrap table 服務(wù)器端分頁功能實現(xiàn)方法,結(jié)合實例形式詳細分析了Bootstrap table 服務(wù)器端后臺交互與分頁功能相關(guān)操作技巧,需要的朋友可以參考下2020-06-06

