小程序?qū)崿F(xiàn)日歷左右滑動(dòng)效果
本文實(shí)例為大家分享了小程序日歷左右滑動(dòng)效果的具體代碼,供大家參考,具體內(nèi)容如下
效果圖



wxml
<scroll-view class="scroll-view" scroll-left="{{scrollLeftIndex*itemWidth}}" scroll-x scroll-with-animation>
<view class='item' style='width:{{itemWidth}}px' wx:for="{{dateList}}" data-index='{{index}}' bindtap='clickDate'>
<view class='text-view'>
<view class='day {{index==clickIndex?"day-active":""}}'>{{item.day}}</view>
<text class='month'>{{item.month}}月</text>
<view wx:if="{{item.month == sysmonth && item.day == nowDate}}">
<text class='week'>今日</text>
</view>
<view wx:else>
<text class='week'>{{item.week}}</text>
</view>
</view>
</view>
</scroll-view>
js
// byjs_sun/pages/product/all/all.js
Page({
/**
* 頁(yè)面的初始數(shù)據(jù)
*/
data: {
dateList:[], //存放日期的數(shù)組
nowDate:'', //系統(tǒng)當(dāng)前日期
},
// 格式化日期,時(shí)間
formatTime(date) {
const year = date.getFullYear()
const month = date.getMonth() + 1
const day = date.getDate()
const hour = date.getHours()
const minute = date.getMinutes()
const second = date.getSeconds()
return [year, month, day].map(this.formatNumber).join('/') + ' ' + [hour, minute, second].map(this.formatNumber).join(':')
},
// 格式化數(shù)字
formatNumber(n) {
n = n.toString()
return n[1] ? n : '0' + n
},
// 獲取日期詳情
getDateInfo(ts) {
const date = new Date(ts);
const weekArr = new Array("日", "一", "二", "三", "四", "五", "六");
const week = date.getDay();
let dateString = this.formatTime(date);
let shortDateString = dateString.replace(/\//g, '-').substring(5, 10).replace(/-/g, '月') + "日";
if (date.getDate() < 10) {
shortDateString = shortDateString.replace(/0/g, '');
}
return {
shortDateString,
dateString,
month: date.getMonth() + 1,
day: date.getDate(),
week: weekArr[week]
}
},
/**
* 生命周期函數(shù)--監(jiān)聽(tīng)頁(yè)面加載
*/
onLoad: function (options) {
var that = this;
var myDate = new Date(); //獲取系統(tǒng)當(dāng)前時(shí)間
var sysmonth = myDate.getMonth() + 1
var nowDate = myDate.getDate(); //當(dāng)前是本月幾日
var today = myDate.toLocaleDateString(); //今日年月日
that.setData({
nowDate: nowDate,
sysmonth: sysmonth
}),
console.log('系統(tǒng)日期:',myDate);
console.log('系統(tǒng)日期(年/月/日):',today);
console.log('系統(tǒng)日期(月):', sysmonth);
console.log('系統(tǒng)日期(日):', nowDate);
// 獲取屏幕寬度,設(shè)置每個(gè)日期寬度
wx.getSystemInfo({
success: (res) => {
console.log(res);
this.setData({
windowWidth: res.windowWidth,
itemWidth: parseInt(res.windowWidth / 7)
});
},
})
this.initData();
},
// 初始化日期
initData() {
const nowDateTime = +new Date();
let dateList = [];
for (let i = -30; i < 30; i++) {
let obj = this.getDateInfo(nowDateTime + i * 24 * 60 * 60 * 1000);
obj.isChoose = i == 0;
dateList.push(obj);
}
this.setData({
dateList,
clickIndex: 30,
scrollLeftIndex: 30
});
},
// 點(diǎn)擊日期方法
clickDate(e) {
var that = this;
console.log('點(diǎn)擊日期攜帶的下標(biāo):', e.currentTarget.dataset.index); //當(dāng)前的點(diǎn)擊的日期
var index = e.currentTarget.dataset.index;
that.setData({
clickIndex: index
});
// console.log(that.data.scrollLeftIndex);
console.log('當(dāng)前點(diǎn)擊日期:',that.data.dateList[index].shortDateString); //當(dāng)前點(diǎn)擊的日期
// const {
// index
// } = e.currentTarget.dataset;
// this.setData({
// clickIndex: index
// });
// console.log(this.data.dateList[index]);
},
})
樣式
.scroll-view {
height: 200rpx;
background-color: #313751;
width: 100%;
white-space: nowrap;
}
.item {
height: 200rpx;
display: inline-block;
}
.text-view {
display: flex;
align-items: center;
justify-content: center;
flex-direction: column;
width: 100%;
height: 100%;
color: #fff;
}
.month {
font-size: 30rpx;
margin-top: 10rpx;
}
.week {
font-size: 32rpx;
margin-top: 10rpx;
}
.day {
font-size: 32rpx;
width: 60rpx;
height: 60rpx;
border-radius: 10rpx;
text-align: center;
line-height: 60rpx;
margin-top: 20rpx;
}
/* 日期選中的樣式 */
.day-active {
background-color: #FFB400;
color: #fff;
}
更多教程點(diǎn)擊《Vue.js前端組件學(xué)習(xí)教程》,歡迎大家學(xué)習(xí)閱讀。
關(guān)于vue.js組件的教程,請(qǐng)大家點(diǎn)擊專題vue.js組件學(xué)習(xí)教程進(jìn)行學(xué)習(xí)。
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
- 微信小程序?qū)崿F(xiàn)卡片左右滑動(dòng)效果的示例代碼
- 微信小程序左右滑動(dòng)的實(shí)現(xiàn)代碼
- 微信小程序滾動(dòng)Tab實(shí)現(xiàn)左右可滑動(dòng)切換
- 微信小程序圖片橫向左右滑動(dòng)案例
- 微信小程序左右滑動(dòng)切換頁(yè)面詳解及實(shí)例代碼
- 微信小程序側(cè)邊欄滑動(dòng)特效(左右滑動(dòng))
- 小程序?qū)崿F(xiàn)點(diǎn)擊tab切換左右滑動(dòng)
- 微信小程序頁(yè)面滑動(dòng)屏幕加載數(shù)據(jù)效果
- 微信小程序之滑動(dòng)頁(yè)面隱藏和顯示組件功能的實(shí)現(xiàn)代碼
- 微信小程序?qū)崿F(xiàn)頁(yè)面左右滑動(dòng)
相關(guān)文章
JavaScript股票的動(dòng)態(tài)買賣規(guī)劃實(shí)例分析下篇
這篇文章主要介紹了JavaScript對(duì)于動(dòng)態(tài)規(guī)劃解決股票問(wèn)題的真題例舉講解。文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2022-08-08
Webpack框架核心概念(知識(shí)點(diǎn)整理)
webpack 是一個(gè)現(xiàn)代 JavaScript 應(yīng)用程序的模塊打包器(module bundler)。這篇文章主要介紹了Webpack框架核心概念(知識(shí)點(diǎn)整理),需要的朋友可以參考下2017-12-12
javascript實(shí)現(xiàn)前端input密碼輸入強(qiáng)度驗(yàn)證
這篇文章主要為大家詳細(xì)介紹了javascript實(shí)現(xiàn)前端input密碼輸入強(qiáng)度驗(yàn)證,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2020-06-06
js中symbol類型以及symbol的三大應(yīng)用場(chǎng)景詳解
Symbol是ES6新推出的一種基本類型,它表示獨(dú)一無(wú)二的值,它可以接受一個(gè)字符串作為參數(shù),帶有相同參數(shù)的兩個(gè)Symbol值不相等,這個(gè)參數(shù)只是表示Symbol值的描述而已,下面這篇文章主要給大家介紹了關(guān)于js中symbol類型以及symbol的三大應(yīng)用場(chǎng)景,需要的朋友可以參考下2022-09-09
JavaScript/TypeScript 實(shí)現(xiàn)并發(fā)請(qǐng)求控制的示例代碼
這篇文章主要介紹了JavaScript/TypeScript 實(shí)現(xiàn)并發(fā)請(qǐng)求控制,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2021-01-01
通過(guò)實(shí)例解析json與jsonp原理及使用方法
這篇文章主要介紹了通過(guò)實(shí)例解析json與jsonp原理及使用方法,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2020-09-09
js實(shí)現(xiàn)轉(zhuǎn)盤抽獎(jiǎng)功能
這篇文章主要為大家詳細(xì)介紹了js實(shí)現(xiàn)轉(zhuǎn)盤抽獎(jiǎng)功能,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2022-03-03

