使用js制作页面切换动画

先来个效果图



再贴代码...
此处使用vue的方式写,如使用其它框架,按思路修改即可。

<template>
    <div class="all" @touchstart.stop.prevent="touchstart" @touchmove.stop.prevent="touchmove" @touchend.stop.prevent="touchend">
        <div class="top" ref='login'>
            登陆
        </div>
        <div class="bottom" ref="reg">
            注册
        </div>
    </div>
</template>
<script>
export default {
    data () {
        return {
            clientHeight:document.body.clientHeight,//屏幕高度
            startclientY:0,//开始点击的高度
            clientMove:0,//移动距离
            nextPage:true,//当前页面
        }
    },
    methods:{
        touchstart(e){
            this.startclientY = e.touches[0].clientY;//Y
        },
        touchmove(e){
            this.clientMove = this.startclientY-e.touches[0].clientY;//计算移动距离
                //跟随鼠标移动页面
            if(this.nextPage){
                this.$refs.reg.style.top = this.clientHeight-this.clientMove+'px';
            }else{
                this.$refs.login.style.top = this.clientHeight-this.clientMove+'px';
            }
        },
        touchend(e){
            if(this.clientMove>=50){//移动距离超过50px
                if(this.nextPage){
                    //添加动画
                    this.$refs.reg.className = 'bottom go_top';
                    setTimeout(()=>{
                        this.$refs.reg.className = 'bottom go_top1';
                    },600);
                    //动画执行结束,修改底部页面位置
                    setTimeout(()=>{
                        this.$refs.login.className = 'top';
                        this.$refs.login.style.top = this.clientHeight+'px';
                        this.$refs.login.style.zIndex = 2;
                        this.nextPage = !this.nextPage;
                    },1100);
                }else{
                    this.$refs.login.className = 'top go_top';
                    setTimeout(()=>{
                        this.$refs.login.className = 'top go_top1';
                    },600);
                    setTimeout(()=>{
                        this.$refs.reg.className = 'bottom';
                        this.$refs.reg.style.top = this.clientHeight+'px';
                        this.$refs.login.style.zIndex = 0;
                        this.nextPage = !this.nextPage;
                    },1100);
                }
            }else{
                //复原
                if(this.nextPage){
                    this.$refs.reg.style.top = this.clientHeight+'px';
                    this.$refs.login.style.top = '0px';
                }else{
                    this.$refs.login.style.top = this.clientHeight+'px';
                    this.$refs.reg.style.top = '0px';
                }
            }
        }
    }
}
</script>
<style lang="scss" scoped>
@import "../../assets/css/reset.scss";
.all{
    width: 100vw;
    height: 100vh;
    overflow: hidden;
    position: relative;
}
.top{
    background: #FDDB43;
    width: 100vw;
    height: 100vh;
    position: absolute;
    top: 0;
}
.bottom{
    background: #00AA9A;
    width: 100vw;
    height: 100vh;
    position: absolute;
    top: 100vh;
}
.go_top{
    animation: goTop .6s ease-in forwards;
    animation-timing-function: cubic-bezier(.4, .2, .9, .6);
}
.go_top1{
    animation: goTop1 .5s linear forwards;
    animation-timing-function: cubic-bezier(0.3, 0.6, 0.5, 1);
}
@keyframes goTop{
    100%{top:0;}
}
@keyframes goTop1{
    0%{top: 0;}
    30%{top: 30px;}
    60%{top:5px;}
    80%{top: 15px;}
    100%{top:0;}
}
</style>
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

相关阅读更多精彩内容

  • 发现 关注 消息 iOS 第三方库、插件、知名博客总结 作者大灰狼的小绵羊哥哥关注 2017.06.26 09:4...
    肇东周阅读 16,356评论 4 61
  • 密密麻麻的收藏点,每一个地点都有我和广州的故事。 记得南国会议中心,当时脑子发热,特别想去互联网企业,那个时候就想...
    船长Captain阅读 450评论 0 1
  • 史说岳飞乃被秦桧所害,实则不然这话不全面,有秦桧岳飞得死,无秦桧岳飞还是死,秦桧无非是昏君的替罪羊罢了。 岳飞直接...
    李小忍阅读 678评论 0 1
  • 1、回到家我打开行李箱,看了一眼觉得有些烦,不想收拾了,行李箱上盖翻开着我就洗漱睡觉了,半夜突然闪过一个念头:我家...
    语岸阅读 402评论 2 1
  • 女人的江山(十四) 文| 绿茉儿 时间像一个车轮,滚滚向前,哪管身后烟尘渺渺?黑色七月,像一股浓烟,笼罩在每个学子...
    陕北一姐阅读 254评论 0 0

友情链接更多精彩内容