<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="pragma" content="no-cache">
<meta http-equiv="cache-control" content="no-cache">
<meta http-equiv="expires" content="0">
<meta name="viewport" content="initial-scale=1, maximum-scale=1, user-scalable=no">
<title>Title</title>
<script src="util/jquery-1.12.0.min.js"></script>
<style>
*{
margin: 0;
padding: 0;
}
body{
padding-top: 80px;
}
#calendar{
width: 560px;
height: 560px;
margin: 0 auto;
}
#tr1{
height: 160px;
}
#tr2{
height: 400px;
}
#view{
position: relative;
}
.page{
position: absolute;
top:0;
width: 100%;
height: 100%;
text-align: center;
}
.page:nth-of-type(2n){
background-color: #a8e5cf;
}
.page:nth-of-type(2n+1){
background-color: #ffa7d7;
}
@media screen and (max-width: 560px){
body{
padding-top: 0;
}
#calendar{
width: 100%;
height: 375px;
}
#tr1{
height: 75px;
}
#tr2{
height: 300px;
}
}
</style>
</head>
<body>
<table id="calendar" border="1" cellspacing="0">
<tr id="tr1"><td></td></tr>
<tr id="tr2">
<td id="view"> <!--监听滑动的视窗-->
<div class="page">0</div>
</td>
</tr>
</table>
<script>
var screenWidth = window.screen.availWidth;
if(screenWidth > 560){
screenWidth = 560;
}
var num = 0;
$(function(){
swipe();
});
//滑动处理
function swipe() {
var view = document.getElementById('view');
var startX, startY, moveEndX, moveEndY, X, Y, startT;
//滑动开始
view.addEventListener('touchstart', function(e) {
//记录开始时鼠标坐标
startX = e.touches[0].clientX;
startY = e.touches[0].clientY;
//记录手指按下开始时间
startT = new Date().getTime();
//追加表格
if ($(".page").size()==1){
//向左追加表格,设置位置
$("#view").prepend('<div class="page">'+(num-1)+'</div>');
$(".page").first().css("left", -screenWidth+"px");
//向右追加表格,设置位置
$("#view").append('<div class="page">'+(num+1)+'</div>');
$(".page").last().css("left", screenWidth+"px");
}
}, false);
//滑动时
view.addEventListener('touchmove', function(e) {
moveEndX = e.changedTouches[0].clientX;
moveEndY = e.changedTouches[0].clientY;
X = moveEndX - startX;
Y = moveEndY - startY;
if ( Math.abs(X) > Math.abs(Y) ) { //横向滑动
e.preventDefault();
//去掉过渡效果,跟着鼠标滑动
$(".page").each(function () {
this.style.transition = "";
this.style.webkitTransform = "translate("+X+"px, 0px)";
});
}
}, false);
//滑动结束
view.addEventListener('touchend', function(e) {
moveEndX = e.changedTouches[0].clientX;
moveEndY = e.changedTouches[0].clientY;
X = moveEndX - startX;
Y = moveEndY - startY;
//坐标对比
if ( Math.abs(X) > Math.abs(Y) ) { //横向滑动
if ( new Date().getTime()-startT < 500 || Math.abs(X) > screenWidth*0.5 ) { //快滑 或 滑动过半
if ( X > 0 ) { //向右滑
//整体向右平移
$(".page").last().remove();
$(".page").each(function () {
this.style.transition = "-webkit-transform 300ms ease";
this.style.webkitTransform = "translate("+screenWidth+"px, 0px)";
});
//删除多余的表格,将当前页位归 0px
setTimeout(function () {
$(".page").last().remove();
$(".page").first()[0].style.transition = "";
$(".page").first()[0].style.webkitTransform = "translate("+0+"px, 0px)";
$(".page").first().css("left","0px");
}, 300);
num--;
} else if ( X < 0 ) { //向左滑
//整体向左平移
$(".page").first().remove();
$(".page").each(function () {
this.style.transition = "-webkit-transform 300ms ease";
this.style.webkitTransform = "translate(-"+screenWidth+"px, 0px)";
});
//删除多余的表格,将当前页位归 0px
setTimeout(function () {
$(".page").first().remove();
$(".page").last()[0].style.transition = "";
$(".page").last()[0].style.webkitTransform = "translate("+0+"px, 0px)";
$(".page").last().css("left","0px");
}, 300);
num++;
}
} else if ( Math.abs(X) < screenWidth*0.5 ) { //滑动不过半
$(".page").each(function () { //整体回滚
this.style.transition = "-webkit-transform 300ms ease";
this.style.webkitTransform = "translate("+0+"px, 0px)";
});
}
}
});
}
</script>
</body>
</html>
移动端H5页面实现日历左右滑动上一月下一月
最后编辑于 :
©著作权归作者所有,转载或内容合作请联系作者
- 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
- 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
- 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
推荐阅读更多精彩内容
- 找到fullcalendar.js, 找到代码为 isRTL:false,这句话 输入以下几句 monthName...
- Getting Started Use the Current Stable Version (7.1) Buil...