该头部高度为328+margin-top:30px = 358
mounted() { window.addEventListener("scroll", this.handleScroll); }, methods: { handleScroll() { let scrollY = window.scrollY; let scrollObj = document.querySelector(".practice-right"); if (scrollY > 358) { //这个358就是上面的距离 scrollObj.style.position = "fixed"; scrollObj.style.right = "150px"; scrollObj.style.top = "0"; } else { scrollObj.style.position = "static"; scrollObj.style.marginTop = "0"; } } }
要实现的是这个右边的答题卡的定位
当滚动一定距离后自动绝对定位到顶部,当向上滚动回来时又取消绝对定位