antd vue 位置变动 固钉消失_使用vue封装固钉Affix组件,滚动条到底部时自动吸附,离开底部时自动相对窗口固定...

本文介绍如何使用Vue封装一个固钉(Affix)组件,该组件能够根据滚动条位置自动吸附到窗口底部或相对窗口固定。在滚动到底部时,按钮会变为固钉状态,离开底部时恢复原位。通过监听滚动事件和修改CSS类实现这一功能,并提供了调整底部高度的灵活性。
摘要由CSDN通过智能技术生成

@click="$router.go(-1)">返回

export default {

name: 'Affix',

props: {

},

data () {

return {

}

},

mounted () {

window.addEventListener('scroll', this.handleScroll)

},

methods: {

handleScroll (e) {

if (document.documentElement.scrollHeight - document.documentElement.scrollTop <= document.documentElement.clientHeight + 317) {

let fxDom = document.getElementsByClassName('back-btn-position-fx-style')

if (fxDom.length > 0) {

fxDom[0].classList.add('back-btn-position-ab-style')

fxDom[0].classList.remove('back-btn-position-fx-style')

}

} else {

let adDom = document.getElementsByClassName('back-btn-position-ab-style')

if (adDom.length > 0) {

adDom[0].classList.add('back-btn-position-fx-style')

adDom[0].classList.remove('back-btn-position-ab-style')

}

}

}

}

}

.back-btn-position-ab-style {

position: absolute;

bottom: 20px;

margin-left: 1074px;

}

.back-btn-position-fx-style {

position: fixed;

bottom: 20px;

margin-left: 1074px;

}

封装时我写的这个项目底部高度是317像素,可根据自己业务需求把代码中的317改成相应的底部高度

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值