js实现web端监听页面滚动切换当前tab

开发背景:Vue项目

实现代码:

  mounted () {
    // 添加页面滚动监听事件
    window.addEventListener('scroll', this.srcollHandler, true)
  },
  
  destroyed () {
    // 记得移除监听事件
    window.removeEventListener("scroll", this.srcollHandler, true)
  }

  methods: {
    // 监听页面滚动,设置当前tab
    srcollHandler () {
      // console.log(216, event.target.documentElement.scrollTop)
      const headerHeight = document.querySelector('.page-header').clientHeight
      const a = document.getElementById('notice').getBoundingClientRect().top - headerHeight // 政策兑现通知公告
      const b = document.getElementById('publicity').getBoundingClientRect().top - headerHeight // 结果公示
      const c = document.getElementById('professional').getBoundingClientRect().top - headerHeight // 职业动态
      const d = document.getElementById('industry').getBoundingClientRect().top - headerHeight // 产业扶持政策
      const e = document.getElementById('news').getBoundingClientRect().top - headerHeight // 新闻资讯
      // console.log('a', a, 'b', b, 'c', c, 'd', d, 'e', e)
      if (a <= 0 && b > 0) this.currentTab = 'notice'
      else if (b <= 0 && c > 0) this.currentTab = 'publicity'
      else if (c <= 0 && d > 0) this.currentTab = 'professional'
      else if (d <= 0 && e > 0) this.currentTab = 'industry'
      else if (e <= 0) this.currentTab = 'news'
      else if (a > 0) this.currentTab = 'notice'
    }
  }

记录于2022-3-2

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
在Vue中实现元素吸顶或固定位置的方式是通过监听页面滚动事件并动态改变元素的样式或位置。具体步骤如下: 1. 在Vue的组件中,通过`mounted`钩子函数监听页面滚动事件: ``` mounted() { window.addEventListener('scroll', this.handleScroll); } ``` 2. 在`methods`中定义`handleScroll`方法来处理滚动事件,并使用`scrollTop`属性获取页面滚动位置: ``` methods: { handleScroll() { const scrollTop = document.documentElement.scrollTop || document.body.scrollTop; // 根据scrollTop的值来决定元素的样式或位置 // ... } } ``` 3. 在模板中,使用动态绑定的方式来设置元素的样式或位置。可以通过绑定CSS类名或行内样式实现,以下是两种常见的实现示例: - 使用CSS类名: 在data中定义一个`isFixed`变量,根据滚动位置的条件来切换`isFixed`的值,在模板中绑定CSS类名根据`isFixed`的值设置元素的样式: ``` data() { return { isFixed: false, }; }, methods: { handleScroll() { const scrollTop = document.documentElement.scrollTop || document.body.scrollTop; // 根据scrollTop的值来判断是否满足条件,如果满足条件则设置isFixed为true,否则为false this.isFixed = scrollTop > 100; }, }, ``` 在模板中绑定CSS类名,并设置元素的样式: ``` <div :class="{ 'fixed': isFixed }">固定的元素</div> <style> .fixed { position: fixed; top: 0; left: 0; } </style> ``` - 使用行内样式: 直接通过绑定`style`属性来设置元素的样式: ``` data() { return { topPosition: 0, }; }, methods: { handleScroll() { const scrollTop = document.documentElement.scrollTop || document.body.scrollTop; // 根据scrollTop的值来判断是否满足条件,如果满足条件则设置topPosition为固定的位置,否则为0 this.topPosition = scrollTop > 100 ? '100px' : '0'; }, }, ``` 在模板中绑定行内样式: ``` <div :style="{ top: topPosition }">固定的元素</div> ``` 通过以上步骤就可以实现元素吸顶或固定位置的效果了。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值