资讯

精准传达 • 有效沟通

从品牌网站建设到网络营销策划,从策略到执行的一站式服务

怎么在小程序中实现一个日历左右滑动效果

这期内容当中小编将会给大家带来有关怎么在小程序中实现一个日历左右滑动效果,文章内容丰富且以专业的角度为大家分析和叙述,阅读完这篇文章希望大家可以有所收获。

创新互联专注于沾化网站建设服务及定制,我们拥有丰富的企业做网站经验。 热诚为您提供沾化营销型网站建设,沾化网站制作、沾化网页设计、沾化网站官网定制、重庆小程序开发服务,打造沾化网络公司原创品牌,更为您提供沾化网站排名全网营销落地服务。

wxml


 
 
 {{item.day}}
 {{item.month}}月
 
 今日
 
 
 {{item.week}}
 
 
 

js

// byjs_sun/pages/product/all/all.js
Page({
 
 /**
 * 页面的初始数据
 */
 data: {
 dateList:[], //存放日期的数组
 nowDate:'', //系统当前日期
 },
 // 格式化日期,时间
 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(':')
 },
 // 格式化数字
 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]
 }
 },
 
/**
 * 生命周期函数--监听页面加载
 */
 onLoad: function (options) {
 var that = this;
 var myDate = new Date(); //获取系统当前时间
 var sysmonth = myDate.getMonth() + 1
 var nowDate = myDate.getDate(); //当前是本月几日
 var today = myDate.toLocaleDateString(); //今日年月日
 that.setData({
 nowDate: nowDate,
 sysmonth: sysmonth
 }),
 console.log('系统日期:',myDate);
 console.log('系统日期(年/月/日):',today);
 console.log('系统日期(月):', sysmonth);
 console.log('系统日期(日):', nowDate);
 
 // 获取屏幕宽度,设置每个日期宽度
 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
 
 });
 },
 
 // 点击日期方法
 clickDate(e) {
 var that = this;
 console.log('点击日期携带的下标:', e.currentTarget.dataset.index); //当前的点击的日期
 var index = e.currentTarget.dataset.index;
 that.setData({
 clickIndex: index
 });
 // console.log(that.data.scrollLeftIndex);
 console.log('当前点击日期:',that.data.dateList[index].shortDateString); //当前点击的日期
 
 // 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;
}

上述就是小编为大家分享的怎么在小程序中实现一个日历左右滑动效果了,如果刚好有类似的疑惑,不妨参照上述分析进行理解。如果想知道更多相关知识,欢迎关注创新互联行业资讯频道。


本文名称:怎么在小程序中实现一个日历左右滑动效果
分享路径:http://cdkjz.cn/article/ppscei.html
多年建站经验

多一份参考,总有益处

联系快上网,免费获得专属《策划方案》及报价

咨询相关问题或预约面谈,可以通过以下方式与我们联系

业务热线:400-028-6601 / 大客户专线   成都:13518219792   座机:028-86922220