微信小程序日歷效果
本文實(shí)例為大家分享了微信小程序日歷效果的具體代碼,供大家參考,具體內(nèi)容如下
源碼下載地址
項(xiàng)目需要一個(gè)日歷功能,花了一天時(shí)間實(shí)現(xiàn)的微信小程序日歷,js大小只有幾k,引入了bootstrap字體圖標(biāo)(文件icon.wxss,該字體已經(jīng)bootstrap字體圖標(biāo)需要的字體)看起來(lái)有80k,出去icon.wxss整個(gè)只有幾k,有問(wèn)題歡迎指正
如圖:

index.wxml
<view class="page">
<view class="box">
<view class="box-flex">
<view class="flex-item">
<view class="item-content" bindtap="doDay" data-key='left'>
<view class="glyphicon glyphicon-triangle-left"></view>
</view>
</view>
<view class="flex-item item-content-current-day">
<view class="item-content">{{currentDate}}</view>
</view>
<view class="flex-item">
<view class="item-content" bindtap="doDay" data-key="right">
<view class="glyphicon glyphicon-triangle-right"></view>
</view>
</view>
</view>
<view class="box-flex">
<view class="flex-item">
<view class="item-content">一</view>
</view>
<view class="flex-item">
<view class="item-content">二</view>
</view>
<view class="flex-item">
<view class="item-content">三</view>
</view>
<view class="flex-item">
<view class="item-content">四</view>
</view>
<view class="flex-item">
<view class="item-content">五</view>
</view>
<view class="flex-item">
<view class="item-content">六</view>
</view>
<view class="flex-item">
<view class="item-content">日</view>
</view>
</view>
<view class="box-flex">
<view class="flex-item" wx:for="{{currentDayList}}" wx:for-index='key' wx:for-item="vo" wx:key="{{key}}">
<view class="item-content" wx:if="{{currentDay != vo}}">{{vo}}</view>
<view class="item-content bk-color-day" wx:else>{{vo}}</view>
</view>
</view>
</view>
</view>
index.wxss
@import '../../dist/css/icon.wxss';
page {
background-color: #2a8cef;
background:-webkit-gradient(linear, 0 0, 0 bottom, from(#2a8cef), to(#8A2BE2));
display: flex;
flex-direction: column;
width: 100%;
height: 100%;
flex-wrap: nowrap;
justify-content: flex-start;
align-items: stretch;
font-size: 14px;
}
.box {
display: block;
margin: 10px;
}
.box-flex {
display: -webkit-box;
display: -webkit-flex;
display: flex;
flex-wrap: wrap;
}
.flex-item {
flex-flow: nowrap;
flex-grow: 1;
flex-shrink: 1;
width: 14.2%;
}
.item-content {
margin: 5px;
padding: 0 10px;
text-align: center;
background-color: #000;
height: 2rem;
line-height: 2rem;
color: #fff;
}
.bk-color-day {
background-color: #8A2BE2;
}
.item-content-current-day {
flex-grow: 2;
}
index.js
var app = getApp();
Page({
data: {
currentDate: "2017年05月03日",
dayList: '',
currentDayList: '',
currentObj: '',
currentDay: ''
},
onLoad: function (options) {
var currentObj = this.getCurrentDayString()
this.setData({
currentDate: currentObj.getFullYear() + '年' + (currentObj.getMonth() + 1) + '月' + currentObj.getDate() + '日',
currentDay: currentObj.getDate(),
currentObj: currentObj
})
this.setSchedule(currentObj)
},
doDay: function (e) {
var that = this
var currentObj = that.data.currentObj
var Y = currentObj.getFullYear();
var m = currentObj.getMonth() + 1;
var d = currentObj.getDate();
var str = ''
if (e.currentTarget.dataset.key == 'left') {
m -= 1
if (m <= 0) {
str = (Y - 1) + '/' + 12 + '/' + d
} else {
str = Y + '/' + m + '/' + d
}
} else {
m += 1
if (m <= 12) {
str = Y + '/' + m + '/' + d
} else {
str = (Y + 1) + '/' + 1 + '/' + d
}
}
currentObj = new Date(str)
this.setData({
currentDate: currentObj.getFullYear() + '年' + (currentObj.getMonth() + 1) + '月' + currentObj.getDate() + '日',
currentObj: currentObj
})
this.setSchedule(currentObj);
},
getCurrentDayString: function () {
var objDate = this.data.currentObj
if (objDate != '') {
return objDate
} else {
var c_obj = new Date()
var a = c_obj.getFullYear() + '/' + (c_obj.getMonth() + 1) + '/' + c_obj.getDate()
return new Date(a)
}
},
setSchedule: function (currentObj) {
var that = this
var m = currentObj.getMonth() + 1
var Y = currentObj.getFullYear()
var d = currentObj.getDate();
var dayString = Y + '/' + m + '/' + currentObj.getDate()
var currentDayNum = new Date(Y, m, 0).getDate()
var currentDayWeek = currentObj.getUTCDay() + 1
var result = currentDayWeek - (d % 7 - 1);
var firstKey = result <= 0 ? 7 + result : result;
var currentDayList = []
var f = 0
for (var i = 0; i < 42; i++) {
let data =[]
if (i < firstKey - 1) {
currentDayList[i] = ''
} else {
if (f < currentDayNum) {
currentDayList[i] = f + 1
f = currentDayList[i]
} else if (f >= currentDayNum) {
currentDayList[i] = ''
}
}
}
that.setData({
currentDayList: currentDayList
})
}
})
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
mpvue微信小程序的接口請(qǐng)求fly全局?jǐn)r截代碼實(shí)例
這篇文章主要介紹了mpvue微信小程序的接口請(qǐng)求fly全局?jǐn)r截代碼實(shí)例,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2019-11-11
JS 實(shí)現(xiàn)導(dǎo)航欄懸停效果(續(xù)2)
發(fā)現(xiàn)原來(lái)的方法還有是有幾個(gè)問(wèn)題:首先Js代碼冗余,導(dǎo)航條上的Tab是用js實(shí)現(xiàn)跳轉(zhuǎn)而不是超鏈接,還有導(dǎo)航條本身用fixed定位,但沒有被設(shè)置為水平居中,而是在JS中更改left值使其居中2013-09-09
JavaScript最全公共方法匯總并解析(前端開發(fā)收藏必備)
JavaScript掌握各種常用的公共方法更是提升開發(fā)效率和代碼質(zhì)量的關(guān)鍵,無(wú)論你是初學(xué)者還是資深開發(fā)者,了解并熟練運(yùn)用這些方法都能讓你的代碼更加簡(jiǎn)潔、高效,本篇博客將為你詳細(xì)匯總并解析最全的JavaScript公共方法,涵蓋數(shù)組、對(duì)象、字符串、日期等各個(gè)方面的常用技巧2024-06-06
詳解微信圖片防盜鏈“此圖片來(lái)自微信公眾平臺(tái) 未經(jīng)允許不得引用”的解決方案
這篇文章主要介紹了詳解微信圖片防盜鏈“此圖片來(lái)自微信公眾平臺(tái) 未經(jīng)允許不得引用”的解決方案,小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2019-04-04
JavaScript版經(jīng)典游戲之掃雷游戲完整示例【附demo源碼下載】
這篇文章主要介紹了JavaScript版經(jīng)典游戲之掃雷游戲?qū)崿F(xiàn)方法,結(jié)合完整實(shí)例形式分析了掃雷游戲的原理與具體實(shí)現(xiàn)流程,并附帶demo源碼供讀者下載參考,需要的朋友可以參考下2016-12-12
分享一個(gè)自己寫的table表格排序js插件(高效簡(jiǎn)潔)
在前不久做的一個(gè)web項(xiàng)目中,需要實(shí)現(xiàn)js表格排序的效果,當(dāng)時(shí)為了省事,就在網(wǎng)上找了幾個(gè)相關(guān)的js插件2011-10-10

