vue 组件封装 | s-scroll 页面滚动动画

简介: vue 组件封装 | s-scroll 页面滚动动画

目标

实现类似插件 wow.js 的页面滚动时,加载动画的效果( wow.js的教程详见 https://blog.csdn.net/weixin_41192489/article/details/111088137)

实现思路

根据指定元素与浏览器窗口顶部的距离是否小于浏览器窗口的高度来判断元素是否进入浏览器窗口

核心要点

1. 要动态生成ref , 避免组件复用时,出现重复的ref (注意:要在页面加载前执行,即放在created 生命周期中)

        created() {
            // 动态生成ref
            this.ref = (Math.random() * 10000000).toString(16).substr(0, 4) + (new Date()).getTime() + Math.random().toString().substr(2, 5);
        },

2. 获取浏览器窗口高度

// 获取浏览器窗口高度
this.screeHeight = document.documentElement.clientHeight

3. 在滚动事件中,实时更新浏览器窗口高度(因浏览器缩放时,浏览器窗口高度会发生变化)

4. 获取元素与浏览器窗口顶部之间的距离

// 获取元素与浏览器窗口顶部之间的距离
let targetTop = this.$refs[this.ref].getBoundingClientRect().top

s-scroll 完整代码

src/plugins/SUI/s-scroll.vue

<template>
    <div style="display: inline-block;">
        <transition
                :enter-active-class="inClass"
                :leave-active-class="outClass"
        >
            <div v-show="show" :ref="ref">
                <slot></slot>
            </div>
        </transition>
    </div>
</template>
<script>
    export default {
        props: {
            inClass: String,
            outClass: String
        },
        data() {
            return {
                screeHeight: 0,
                ref: '',
                show: false,
            }
        },
        created() {
            // 动态生成ref
            this.ref = (Math.random() * 10000000).toString(16).substr(0, 4) + (new Date()).getTime() + Math.random().toString().substr(2, 5);
        },
        mounted() {
            this.$nextTick(
                () => {
// 获取浏览器窗口高度
this.screeHeight = document.documentElement.clientHeight
                    // 监听页面滚动事件
                    window.addEventListener("scroll", this.scrolling)
                    this.scrolling()
                }
            )
        },
        methods: {
            scrolling() {
                // 更新浏览器窗口高度
                this.screeHeight = document.documentElement.clientHeight
// 获取元素与浏览器窗口顶部之间的距离
let targetTop = this.$refs[this.ref].getBoundingClientRect().top
                // 当距离小于浏览器窗口的高度(即元素进入浏览器窗口时),开启过渡动画,显示元素
                if (targetTop < this.screeHeight) {
                    this.show = true;
                } else {
                    this.show = false;
                }
            },
        }
    }
</script>
<style scoped>
</style>

使用范例代码

<template>
    <div>
        <div class="box100">0</div>
        <div class="box100">100</div>
        <div class="box100">200</div>
        <div class="box100">300</div>
        <div class="box100">400</div>
        <div class="box100">500</div>
        <div class="box100">600</div>
        <div class="box100">700</div>
        <s-scroll
                inClass="rollIn"
                outClass="rollOut"
        >
            <div class="circle200">自定义动画</div>
        </s-scroll>
        <s-scroll
                inClass="animate__animated animate__rollIn  animate__slower"
                outClass="animate__animated animate__rollOut  animate__slower"
        >
            <div class="circle200">animate.css动画</div>
        </s-scroll>
        <div class="box100">1000</div>
        <div class="box100">1100</div>
        <div class="box100">1200</div>
    </div>
</template>
<script>
    export default {}
</script>
<style scoped>
    .box100 {
        height: 100px;
        background: #3a8ee6;
        border: 1px solid black;
    }
 
    .circle200 {
        height: 200px;
        width: 200px;
        background: red;
        border-radius: 50%;
        text-align: center;
        line-height: 200px;
    }
 
    /*滚入——从左侧*/
    @keyframes rollIn {
        0% {
            opacity: 0;
            transform: translate(-100%) rotate3d(0, 0, 1, -360deg);
        }
        100% {
            opacity: 1;
        }
    }
 
    /*滚出——从左侧*/
    @keyframes rollOut {
        0% {
            opacity: 1;
        }
        100% {
            opacity: 0;
            transform: translate(-100%) rotate3d(0, 0, 1, -360deg);
        }
    }
 
    /*滚入——从左侧*/
    /deep/ .rollIn {
        animation: rollIn 1s;
    }
 
    /*滚出——从左侧*/
    /deep/ .rollOut {
        animation: rollOut 1s;
    }
</style>


目录
相关文章
|
6月前
|
JavaScript
Vue中如何实现兄弟组件之间的通信
在Vue中,兄弟组件可通过父组件中转、事件总线、Vuex/Pinia或provide/inject实现通信。小型项目推荐父组件中转或事件总线,大型项目建议使用Pinia等状态管理工具,确保数据流清晰可控,避免内存泄漏。
564 2
|
5月前
|
缓存 JavaScript
vue中的keep-alive问题(2)
vue中的keep-alive问题(2)
468 137
|
8月前
|
人工智能 JSON JavaScript
VTJ.PRO 首发 MasterGo 设计智能识别引擎,秒级生成 Vue 代码
VTJ.PRO发布「AI MasterGo设计稿识别引擎」,成为全球首个支持解析MasterGo原生JSON文件并自动生成Vue组件的AI工具。通过双引擎架构,实现设计到代码全流程自动化,效率提升300%,助力企业降本增效,引领“设计即生产”新时代。
607 1
|
8月前
|
JavaScript 安全
在 Vue 中,如何在回调函数中正确使用 this?
在 Vue 中,如何在回调函数中正确使用 this?
418 0
|
9月前
|
人工智能 JavaScript 算法
Vue 中 key 属性的深入解析:改变 key 导致组件销毁与重建
Vue 中 key 属性的深入解析:改变 key 导致组件销毁与重建
1016 0
|
11月前
|
JavaScript
vue实现任务周期cron表达式选择组件
vue实现任务周期cron表达式选择组件
1272 4
|
9月前
|
JavaScript UED
用组件懒加载优化Vue应用性能
用组件懒加载优化Vue应用性能
|
10月前
|
JavaScript 数据可视化 前端开发
基于 Vue 与 D3 的可拖拽拓扑图技术方案及应用案例解析
本文介绍了基于Vue和D3实现可拖拽拓扑图的技术方案与应用实例。通过Vue构建用户界面和交互逻辑,结合D3强大的数据可视化能力,实现了力导向布局、节点拖拽、交互事件等功能。文章详细讲解了数据模型设计、拖拽功能实现、组件封装及高级扩展(如节点类型定制、连接样式优化等),并提供了性能优化方案以应对大数据量场景。最终,展示了基础网络拓扑、实时更新拓扑等应用实例,为开发者提供了一套完整的实现思路和实践经验。
1362 78
|
11月前
|
缓存 JavaScript 前端开发
Vue 基础语法介绍
Vue 基础语法介绍
|
9月前
|
JavaScript 前端开发 开发者
Vue 自定义进度条组件封装及使用方法详解
这是一篇关于自定义进度条组件的使用指南和开发文档。文章详细介绍了如何在Vue项目中引入、注册并使用该组件,包括基础与高级示例。组件支持分段配置(如颜色、文本)、动画效果及超出进度提示等功能。同时提供了完整的代码实现,支持全局注册,并提出了优化建议,如主题支持、响应式设计等,帮助开发者更灵活地集成和定制进度条组件。资源链接已提供,适合前端开发者参考学习。
617 17