Vue实现滑动页面视图内容上移过渡动画效果

12 篇文章 0 订阅
2 篇文章 0 订阅

在官网的开发中 ,PC上需要加一些动画让网页更加的炫酷

常见的就是 将出现的视图内容向上加载过渡动画

将没有动画的“盒子”开发完后加上myanimation 类名就可以。logobox类名是自己的样式类名

<div :class="Animate1 ? 'logobox myanimation' : 'logobox'" ref="logobox">
    你的盒子的内容
</div>	

myanimation可以设置为公共的class,这样可以只要加上该样式,都能够有上移过渡显示效果 

.myanimation{
 
  animation: load 0.51s linear;
  transform:translateY(0px);
  opacity: 1;
}
@keyframes load {
	0% {
		opacity: 0;
    transform:translateY(80px)
	}
	100% {
		opacity: 1;
    transform:translateY(0px)
	}
}

utils.js文件

判断当前视口内容是否出现

export const isElementNotInViewport = function(el) {
    if (el) {
        let rect = el.getBoundingClientRect();
        return (
            rect.top >=
                (window.innerHeight || document.documentElement.clientHeight)
            // || rect.bottom <= 0
        );
    }
};

引用刚才的文件 import { isElementNotInViewport } from "@/utils/utils.js";

监听滚动事件,将div挂载的ref信息传参到isElementNotInViewport函数

data(){
    return {
        Animate1: false
    }
},
mounted() {
    // 监听滚动事件
    window.addEventListener("scroll", this.scrollToTop);
  },
  beforeRouteLeave(to, form, next) {
    // 离开路由移除滚动事件
    window.removeEventListener('scroll',this.scrollToTop);
    next();
  },
  methods: {
	// 滚动事件
    scrollToTop() {
	  this.Animate1=!isElementNotInViewport(this.$refs.logobox)
    },
}

  • 2
    点赞
  • 9
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 3
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

三线码工

码字不易,有钱打赏,没打钱点赞

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值