博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
小程序swiper月份日历滑动
阅读量:4100 次
发布时间:2019-05-25

本文共 5066 字,大约阅读时间需要 16 分钟。

在小程序里用swiper做了一个月份日历,先上图叭在这里插入图片描述直接贴代码叭:

{
{month}}月, {
{year}}年
  • { { d.day}}

css

.calendar {
width: 100%; padding: 37rpx 33rpx 27rpx 33rpx;}.calendar-head {
position: relative; padding:8rpx 16rpx; font-size: 36rpx; font-weight: 500; line-height: 50rpx; color: #000; text-align: center; display: flex; justify-content: space-between; align-items: center;}.calendar-head .icon {
position: absolute; display:inline-block; width:20rpx;}.calendar-head image {
width: 30rpx; height: 30rpx;}.calendar-head image:nth-child(1){
transform: rotate(180deg);}.weeks {
display: flex; justify-content: center; padding:16rpx 0;}.weeks li {
display: inline-flex; width:14.2857%; justify-content: center; font-size: 24rpx;}.weeks .weekend {
color:#ffaa71}.days {
flex-direction: row; flex-wrap: wrap; width: 100%; justify-content: space-between; height: 500rpx !important;}.days li {
float: left; display: flex; justify-content: center; align-items: center; flex: 1; box-sizing: border-box; width: 14.2857%; min-width: 14.2857%; max-width: 14.2857%; height: 80rpx; color: #161616;}.day_date {
width: 81rpx; height: 81rpx; line-height: 42rpx; font-size: 30rpx; font-weight: 500; text-align: center; border-radius: 50%; position: relative;}.day_date text {
display: inline-block; margin-top: 15rpx;}.days .gray {
color: darkgray;}.now_bgk {
background: rgba(0, 137, 255, .1);}.circle {
position: absolute; top: 61rpx; left: 50%; margin-left: -6rpx; width: 12rpx; height: 12rpx; border-radius: 24rpx; background: #0089FF;}.circle-punch {
position: absolute; top: 44rpx; left: 26rpx; width: 10rpx; height: 10rpx; border-radius: 20rpx; background: #0089FF;}.is_bgk {
background: #0089FF !important; color: #fff !important;}.is_bgk .circle {
background: #fff !important;}.current {
background-image: linear-gradient(-45deg, #3857e3, #567bfd); color: #fff; font-weight: bold;}.swiper {
height: 960rpx;}
const dayjs = require("../../utils/dayjs");Page({
data: {
index: 0, // 初次渲染使用monthList[0] sign: null, currentDate: '', now: dayjs().format('YYYY-MM-DD'), month: '', year: '', monthList: [], // 包含3个元素的数组 days: [], checkDate: '', monthCheck: '', yearCheck: '', }, onReady: function(){
this.init(); }, onLoad: function () {
}, swiperMonth(e) {
let sign = e.currentTarget.dataset.sign; let {
index } = this.data; let cur_index; if (sign == -1) {
cur_index = index - 1 < 0 ? 2 : index - 1 }else {
cur_index = index + 1 > 2 ? 0 : index + 1 } this.setData({
sign, index: cur_index, }) }, change(e) {
let current = e.detail.current; let source = e.detail.source; let {
index, currentDate, monthList } = this.data; // 判断左右滑动方向,修改month let flag = null; if(!source){
flag = this.data.sign; }else {
flag = current - index == -2 || current - index == 1 ? 1 : -1; } currentDate = currentDate.add(flag, 'month') this.setData({
currentDate, year: currentDate.year(), month: currentDate.month() + 1, day: currentDate.date(), index: current }) // 滑动后current值,有3种情况需要处理 if (current == 0) monthList = [this.getMonth(0), this.getMonth(1), this.getMonth(-1)] if (current == 1) monthList = [this.getMonth(-1), this.getMonth(0), this.getMonth(1)] if (current == 2) monthList = [this.getMonth(1), this.getMonth(-1), this.getMonth(0)] this.setData({
monthList }) }, init() {
// 初始化 日期为今天 this.setData({
currentDate: dayjs(), checkDate: dayjs(), }) this.setData({
year: dayjs().year(), month: dayjs().month() + 1, day: dayjs().date(), monthList: [this.getMonth(0, 1), this.getMonth(1), this.getMonth(-1)], monthCheck: dayjs().month() + 1, yearCheck: dayjs().year(), }) }, getMonth(index) {
let {
currentDate, year, month, day } = this.data year = currentDate.year() month = currentDate.month() + 1 + index; day = currentDate.date() let first = dayjs(new Date(year, month - 1, 1)) let arrDays = [] let preDays = first.day() == 0 ? 7 : first.day(); for (let i = 0 - preDays; i < 42 - preDays; i++) {
let preDate = first.add(i, 'day'); let d = preDate.format('YYYY-MM-DD'); arrDays.push({
date: d, day: preDate.format('D'), month: preDate.format('M') }) } return {
m: arrDays } }, getDateDetail(e) {
console.log(e.currentTarget.dataset); },})

在这位博主上加了左右箭头点击切换。这一版是用day.js做的。这个直接用js写的。有点繁琐。但方便理清思路

转载地址:http://onzsi.baihongyu.com/

你可能感兴趣的文章
i2c-tools
查看>>
Linux分区方案
查看>>
nc 命令详解
查看>>
如何使用 systemd 中的定时器
查看>>
git命令速查表
查看>>
linux进程监控和自动重启的简单实现
查看>>
OpenFeign学习(三):OpenFeign配置生成代理对象
查看>>
OpenFeign学习(四):OpenFeign的方法同步请求执行
查看>>
OpenFeign学习(五):OpenFeign请求结果处理及重试控制
查看>>
OpenFeign学习(六):OpenFign进行表单提交参数或传输文件
查看>>
OpenFeign学习(七):Spring Cloud OpenFeign的使用
查看>>
Ribbon 学习(二):Spring Cloud Ribbon 加载配置原理
查看>>
Ribbon 学习(三):RestTemplate 请求负载流程解析
查看>>
深入理解HashMap
查看>>
XML生成(一):DOM生成XML
查看>>
XML生成(三):JDOM生成
查看>>
Ubuntu Could not open lock file /var/lib/dpkg/lock - open (13:Permission denied)
查看>>
collect2: ld returned 1 exit status
查看>>
C#入门
查看>>
查找最大值最小值
查看>>