微信小程序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)文章
layui關(guān)閉層級(jí)、簡(jiǎn)單監(jiān)聽的實(shí)例
今天小編就為大家分享一篇layui關(guān)閉層級(jí)、簡(jiǎn)單監(jiān)聽的實(shí)例,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧2019-09-09
移動(dòng)端日期插件Mobiscroll.js使用詳解
這篇文章主要為大家詳細(xì)介紹了移動(dòng)端日期插件Mobiscroll.js的使用方法,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2016-12-12
JavaScript sort數(shù)組排序方法和自我實(shí)現(xiàn)排序方法小結(jié)
這篇文章主要介紹了JavaScript sort數(shù)組排序方法和自我實(shí)現(xiàn)排序方法小結(jié)的相關(guān)資料,非常不錯(cuò)具有參考借鑒價(jià)值,需要的朋友可以參考下2016-06-06
js DIV滾動(dòng)條隨機(jī)位置的設(shè)置技巧
剛才一個(gè)朋友告訴我他的blog友情鏈接太多了,所以把所有鏈接放到一個(gè)DIV中,加了個(gè)滾動(dòng)條,可是他又不想每次別人來看,看到的都是前面幾個(gè)鏈接,于是問我有沒有什么辦法,想了一個(gè),呵呵,正好有個(gè)辦法2008-11-11
原生javascript實(shí)現(xiàn)的ajax異步封裝功能示例
這篇文章主要介紹了原生javascript實(shí)現(xiàn)的ajax異步封裝功能,結(jié)合完整實(shí)例形式分析了原生javascript實(shí)現(xiàn)的ajax異步交互函數(shù)與相應(yīng)的使用方法,需要的朋友可以參考下2016-11-11
利用ajaxfileupload插件實(shí)現(xiàn)文件上傳無刷新的具體方法
利用ajaxfileupload插件實(shí)現(xiàn)文件上傳無刷新的具體方法,需要的朋友可以參考一下2013-06-06

