移动端H5页面实现日历左右滑动上一月下一月

<!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>

🌰 http://mengmei219.top/calendar/calendar.php

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

相关阅读更多精彩内容

友情链接更多精彩内容