您当前的位置: 首页 > 

30秒实现Vue吸顶效果

发布时间:2020-05-04 09:33:58 ,浏览量:0

酱酱,好久不见鸭!

前言:吸顶效果图: 1、滚动前:
image.png
2、滚动中:
image.png
3、滚动超过后:
image.png
直观效果可参pc端微博左侧的信息栏 第一步:html 中:

在你要实现吸顶效果的div上,加上如下这一行代码:

image.png
下方代码自取↓
:class="{'is_fixed' : isFixed}" 

触发吸顶效果的位置,如下:

image.png
下方代码自取↓
<div id="boxFixed"> return { isFixed: false, offsetTop: 0, } } 
第三步:在 mounted 与 destroyed 中
mounted() { window.addEventListener('scroll', this.initHeight); this.$nextTick(() => { //获取对象相对于版面或由 offsetTop 属性指定的父坐标的计算顶端位置  this.offsetTop = document.querySelector('#boxFixed').offsetTop; }) }, //回调中移除监听 destroyed() { window.removeEventListener('scroll', this.handleScroll) }, 
第四步:在 methods 方法定义中:
/********* start-目录吸顶********/ initHeight() { // 设置或获取位于对象最顶端和窗口中可见内容的最顶端之间的距离 (被卷曲的高度) var scrollTop = window.pageYOffset || document.documentElement.scrollTop || document.body.scrollTop //如果被卷曲的高度大于吸顶元素到顶端位置 的距离 this.isFixed = scrollTop > this.offsetTop ? true : false; }, /********* end-目录吸顶********/ 
最后一步:在css中:
.is_fixed{ width: 100%; position: fixed; top: 0; z-index: 999; } 

大功告成,感谢博文: https://www.jianshu.com/p/5fc421a2f12a

关注
打赏
1688896170
查看更多评论

暂无认证

  • 0浏览

    0关注

    109691博文

    0收益

  • 0浏览

    0点赞

    0打赏

    0留言

私信
关注
热门博文
立即登录/注册

微信扫码登录

0.2657s