Vue滑动隐藏图标效果

Vue实现固定定位图标滑动隐藏效果

页面滚动前
在这里插入图片描述
页面滚动后
在这里插入图片描述

<template>
<!-- 注意:我这里是Vue3,所以最外层没有套一个div盒子 -->
    <!-- <router-view></router-view> -->
    <div class="main">
        <div class="toAsk" :class="[ !transition ? 'fixed-transition' : '']" @click="toAsk">
            <img src="http://celiang.oss-cn-hangzhou.aliyuncs.com/measurement/2022-01/10/cGSADphpNnphGe1641795949220110.png"
                alt="">
        </div>

    </div>
</template>
<script>
    export default {
        data() {
            return {
                transition: true,
                timer: null

            }
        },
        mounted() {
            /**
             * @description 监听页面滑动**/
            window.addEventListener('scroll', this.handleScroll);
        },
        methods: {
            /**
             * @description 页面滑动
             * 监听滑动事件,每次进行滑动时,触发动画,添加定时器,1.4s后显示该图标
             * 
             * **/
            handleScroll() { 
                this.transition = false;
                if (this.timer) { // 判断是否已存在定时器
                    clearTimeout(this.timer);
                }
                this.timer = setTimeout(() => { // 创建定时器,1.4s后图标回归原位置
                    this.transition = true;
                }, 1000);
            },

        }
    }
</script>
<style scoped>

    .main {
        height: 120vh;
    }

    .toAsk {
        width: 88px;
        position: fixed;
        right: 10px;
        bottom: 10vh;
        transition: 0.5s ease-in-out;
    }

    .fixed-transition {
        right: -60px;
        opacity: 0.4;
        transition: 0.5s ease-in-out;
    }

    img {
        max-width: 100%;
    }
</style>
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值