vue 组件封装 | s-fullpage 全屏滚动 (内含绑定鼠标滑轮滚动事件、避免鼠标滑轮连续滚动、滑动过渡动画等实用技巧)

简介: vue 组件封装 | s-fullpage 全屏滚动 (内含绑定鼠标滑轮滚动事件、避免鼠标滑轮连续滚动、滑动过渡动画等实用技巧)

目标

实现类似插件 vue-fullpage.js 的全屏滚动翻页效果( vue-fullpage.js 的教程详见 https://blog.csdn.net/weixin_41192489/article/details/111104443)

实现原理(要点)

1. 使用 “子绝父相” 定位,实现上下滑动过渡动画翻页(滑动动画的实现原理,详见 https://blog.csdn.net/weixin_41192489/article/details/112271981 )

2. 通过插槽获取页面列表,根据下标变化完成翻页

this.$slots.default.forEach(item => {
    this.pageList.push(item.componentOptions.propsData.page)
})
this.currentPage = this.pageList[this.currentIndex]

3. 绑定鼠标滑轮滚动事件,通过限定事件触发时间间隔,来避免连续滚动触发越级翻页

@wheel.prevent="mouseWheel"
// 鼠标滑轮滚动事件
mouseWheel(e) {
   this.startTime = new Date().getTime()
   // 每次滚动事件触发1s后,才会再次触发(避免鼠标滚动事件连续触发)
   if (this.startTime - this.endTime > 1000) {
        ……  // 执行翻页事件,翻页完成后,执行 this.endTime = new Date().getTime()
   }
}

4. 通过监听下标变化来切换向上翻页和向下翻页的过渡动画

watch: {
    '$parent.currentIndex'(newIndex, oldindex) {
        if (newIndex >= oldindex) {
            // 向下翻页时,使用向下翻页的动画,默认为从下方滑入,从上方滑出
            this.inClass = this.nextInClass
            this.outClass = this.nextOutClass
        } else {
            // 向上翻页时,使用向上翻页的动画,默认为从上方滑入,从下方滑出
            this.inClass = this.previousInClass
            this.outClass = this.previousOutClass
        }
    }
},

完整代码 s-fullpage 和 s-fullpageItem

全屏滚动通过封装 父组件 s-fullpage 和 子组件 s-fullpageItem 来配合实现

s-fullpage.vue

<template>
    <div @wheel.prevent="mouseWheel" class="fullpage">
        <!--指示器-->
        <div class="fixed__indicatorsBox" :style="indicatorsBoxStyle">
            <div @click="goto(index)" :class="{'active_indicator':currentIndex === index}" class="indicator"
                 v-for="(item,index) in pageList" :key="index">
            </div>
        </div>
        <slot></slot>
    </div>
</template>
<script>
    export default {
        name: "s-fullpage",
        props: {
            // 默认显示页
            activePage: String,
            // 指示器的位置
            indicatorPosition: {
                type: String,
                default: 'right'
            }
        },
        mounted() {
            if (this.indicatorPosition === 'right') {
                this.$set(this.indicatorsBoxStyle, 'right', 0)
            } else if (this.indicatorPosition === 'left') {
                this.$set(this.indicatorsBoxStyle, 'left', 0)
            }
            this.$slots.default.forEach(item => {
                this.pageList.push(item.componentOptions.propsData.page)
            })
            if (!this.activePage && this.pageList.length > 0) {
                this.currentPage = this.pageList[0]
            } else {
                this.currentPage = this.activePage
                this.pageList.forEach((item, index) => {
                    if (this.currentPage === item) {
                        this.currentIndex = index
                    }
                })
            }
        },
        data() {
            return {
                indicatorsBoxStyle: {},
                // 所有页构成的列表
                pageList: [],
                // 当前显示页
                currentPage: '',
                // 当前显示页的下标
                currentIndex: 0,
                // 鼠标滑轮滚动的方向
                direction: '',
                // 鼠标滑轮事件开始时间
                startTime: '',
                // 鼠标滑轮指令执行结束时间
                endTime: '',
            }
        },
        methods: {
            // 页面跳转
            goto(index) {
                this.currentIndex = index
                this.currentPage = this.pageList[index]
            },
            // 鼠标滑轮滚动事件
            mouseWheel(e) {
                this.startTime = new Date().getTime()
                // 每次滚动事件触发1s后,才会再次触发(避免鼠标滚动事件连续触发)
                if (this.startTime - this.endTime > 1000) {
                    e = e || window.event;
                    if (e.wheelDelta) {  //IE,谷歌浏览器滑轮事件
                        if (e.wheelDelta > 0) {
                            // 向上滚动
                            this.previousPage()
                        }
                        if (e.wheelDelta < 0) {
                            // 向下滚动
                            this.nextPage()
                        }
                    } else if (e.detail) {  //Firefox滑轮事件
                        if (e.detail > 0) {
                            // 向上滚动
                            this.previousPage()
                        }
                        if (e.detail < 0) {
                            // 向下滚动
                            this.nextPage()
                        }
                    }
                }
            },
            previousPage() {
                this.direction = 'up'
                this.currentIndex -= 1
                // 第1页时,禁止继续向上翻页
                if (this.currentIndex < 0) {
                    this.currentIndex = 0
                }
                this.currentPage = this.pageList[this.currentIndex]
                this.endTime = new Date().getTime()
            },
            nextPage() {
                this.direction = 'down'
                this.currentIndex += 1
                // 最后一页时,禁止继续向下翻页
                if (this.currentIndex > this.pageList.length - 1) {
                    this.currentIndex = this.pageList.length - 1
                }
                this.currentPage = this.pageList[this.currentIndex]
                this.endTime = new Date().getTime()
            },
        }
    }
</script>
<style scoped>
    /*指示器容器的样式*/
    .fixed__indicatorsBox {
        position: fixed;
        top: 50%;
        transform: translateY(-50%);
        z-index: 999;
    }
 
    /*指示器的样式*/
    .indicator {
        height: 16px;
        width: 16px;
        border-radius: 50%;
        background: white;
        opacity: 0.3;
        margin: 10px;
        cursor: pointer;
    }
 
    /*当前指示器的样式*/
    .active_indicator {
        opacity: 1;
        box-shadow: 0px 0px 8px 2px white;
    }
 
    .fullpage {
        height: 100vh;
        width: 100vw;
        color: white;
        background: black;
        position: relative;
        /*隐藏滚动条*/
        overflow: hidden;
    }
 
    /*滑出——从上方*/
    .slidOutFromTop {
        animation: slidOutFromTop 1s;
    }
 
    @keyframes slidOutFromTop {
        from {
            top: 0;
        }
        to {
            top: -100%;
        }
    }
 
    /*滑出——从下方*/
    .slidOutFromBottom {
        animation: slidOutFromBottom 1s;
    }
 
    @keyframes slidOutFromBottom {
        from {
            top: 0;
        }
        to {
            top: 100%;
        }
    }
 
 
    /*滑入——从上方*/
    .slidInFromTop {
        animation: slidInFromTop 1s;
    }
 
    @keyframes slidInFromTop {
        from {
            top: -100%;
        }
        to {
            top: 0;
        }
    }
 
    /*滑入——从下方*/
    .slidInFromBottom {
        animation: slidInFromBottom 1s;
    }
 
    @keyframes slidInFromBottom {
        from {
            top: 100%;
        }
        to {
            top: 0;
        }
    }
</style>

s-fullpageItem.vue

<template>
    <transition
            :enter-active-class="inClass"
            :leave-active-class="outClass"
    >
        <div v-show="$parent.currentPage === page" class="s_fullpageItem">
            <slot></slot>
        </div>
    </transition>
</template>
<script>
    export default {
        name: "s-fullpageItem",
        props: {
            // 页面标识,必传且不能与其他页重复!
            page: {
                type: String,
                required: true
            },
            nextInClass: {
                type: String,
                default: 'slidInFromBottom'
            },
            nextOutClass: {
                type: String,
                default: 'slidOutFromTop'
            },
            previousInClass: {
                type: String,
                default: 'slidInFromTop'
            },
            previousOutClass: {
                type: String,
                default: 'slidOutFromBottom'
            },
        },
        watch: {
            '$parent.currentIndex'(newIndex, oldindex) {
                if (newIndex >= oldindex) {
                    // 向下翻页时,使用向下翻页的动画,默认为从下方滑入,从上方滑出
                    this.inClass = this.nextInClass
                    this.outClass = this.nextOutClass
                } else {
                    // 向上翻页时,使用向上翻页的动画,默认为从上方滑入,从下方滑出
                    this.inClass = this.previousInClass
                    this.outClass = this.previousOutClass
                }
            }
        },
        data() {
            return {
                inClass: '',
                outClass: '',
            }
        },
    }
</script>
<style scoped>
    .s_fullpageItem {
        position: absolute;
        height: 100vh;
        width: 100vw;
        text-align: center;
        display: flex;
        justify-content: center;
        align-items: center;
    }
 
    /*滑出——从上方*/
    .slidOutFromTop {
        animation: slidOutFromTop 1s
    }
 
    @keyframes slidOutFromTop {
        from {
            top: 0;
        }
        to {
            top: -100%;
        }
    }
 
    /*滑出——从下方*/
    .slidOutFromBottom {
        animation: slidOutFromBottom 1s
    }
 
    @keyframes slidOutFromBottom {
        from {
            top: 0;
        }
        to {
            top: 100%;
        }
    }
 
 
    /*滑入——从上方*/
    .slidInFromTop {
        animation: slidInFromTop 1s
    }
 
    @keyframes slidInFromTop {
        from {
            top: -100%;
        }
        to {
            top: 0;
        }
    }
 
    /*滑入——从下方*/
    .slidInFromBottom {
        animation: slidInFromBottom 1s
 
    }
 
    @keyframes slidInFromBottom {
        from {
            top: 100%;
        }
        to {
            top: 0;
        }
    }
</style>

使用范例

<template>
    <div>
        <s-fullpage activePage="2" indicatorPosition="left">
            <s-fullpageItem page="1" >第1页</s-fullpageItem>
            <s-fullpageItem page="2" style="background: blue">第2页</s-fullpageItem>
            <s-fullpageItem page="3" style="background: green">第3页</s-fullpageItem>
        </s-fullpage>
    </div>
</template>

范例效果

 

目录
相关文章
|
27天前
|
人工智能 JavaScript 算法
Vue 中 key 属性的深入解析:改变 key 导致组件销毁与重建
Vue 中 key 属性的深入解析:改变 key 导致组件销毁与重建
184 0
|
28天前
|
JavaScript UED
用组件懒加载优化Vue应用性能
用组件懒加载优化Vue应用性能
|
1月前
|
JavaScript 前端开发 UED
Vue 表情包输入组件实现代码及详细开发流程解析
这是一篇关于 Vue 表情包输入组件的使用方法与封装指南的文章。通过安装依赖、全局注册和局部使用,可以快速集成表情包功能到 Vue 项目中。文章还详细介绍了组件的封装实现、高级配置(如自定义表情列表、主题定制、动画效果和懒加载)以及完整集成示例。开发者可根据需求扩展功能,例如 GIF 搜索或自定义表情上传,提升用户体验。资源链接提供进一步学习材料。
80 1
|
前端开发 JavaScript
初识 Vue(24)---(Vue 中同时使用过渡和动画)
Vue 中同时使用过渡和动画 在上篇博客 《Vue 中使用 animate.css 库》基础上开始这篇博客 在上篇博客中,完成了 引入 animate.
1267 0
|
前端开发 内存技术
Vue_同时使用过渡和动画
在上一节我们用animate动画库,在刷新页面时没有动画 如何解决第一次就显示动画内容呢? 在transform 上加上appear 和appear-active-class <transition name='fade' appear enter-active-class='animate.
1635 0
|
3月前
|
JavaScript
vue实现任务周期cron表达式选择组件
vue实现任务周期cron表达式选择组件
368 4
|
2月前
|
JavaScript 数据可视化 前端开发
基于 Vue 与 D3 的可拖拽拓扑图技术方案及应用案例解析
本文介绍了基于Vue和D3实现可拖拽拓扑图的技术方案与应用实例。通过Vue构建用户界面和交互逻辑,结合D3强大的数据可视化能力,实现了力导向布局、节点拖拽、交互事件等功能。文章详细讲解了数据模型设计、拖拽功能实现、组件封装及高级扩展(如节点类型定制、连接样式优化等),并提供了性能优化方案以应对大数据量场景。最终,展示了基础网络拓扑、实时更新拓扑等应用实例,为开发者提供了一套完整的实现思路和实践经验。
238 77
|
1月前
|
JavaScript 前端开发 开发者
Vue 自定义进度条组件封装及使用方法详解
这是一篇关于自定义进度条组件的使用指南和开发文档。文章详细介绍了如何在Vue项目中引入、注册并使用该组件,包括基础与高级示例。组件支持分段配置(如颜色、文本)、动画效果及超出进度提示等功能。同时提供了完整的代码实现,支持全局注册,并提出了优化建议,如主题支持、响应式设计等,帮助开发者更灵活地集成和定制进度条组件。资源链接已提供,适合前端开发者参考学习。
154 17
|
3月前
|
缓存 JavaScript 前端开发
Vue 基础语法介绍
Vue 基础语法介绍
|
1月前
|
监控 JavaScript 前端开发
Vue 文件批量下载组件封装完整使用方法及优化方案解析
本文详细介绍了批量下载功能的技术实现与组件封装方案。主要包括两种实现方式:**前端打包方案(基于file-saver和jszip)** 和 **后端打包方案**。前者通过前端直接将文件打包为ZIP下载,适合小文件场景;后者由后端生成ZIP文件流返回,适用于大文件或大量文件下载。同时,提供了可复用的Vue组件`BatchDownload`,支持进度条、失败提示等功能。此外,还扩展了下载进度监控和断点续传等高级功能,并针对跨域、性能优化及用户体验改进提出了建议。可根据实际需求选择合适方案并快速集成到项目中。
178 17