vue滚动监听动态更新激活吸顶导航

前言

实现效果:导航栏目随着滚动到页面头部并固定在顶部,随后顶部tab有页面的滚动项目对应激活更新

一、安装依赖及引入

npm install vue-waypoint --save-dev

main.js添加

import VueWaypoint from 'vue-waypoint'

// Waypoint plugin
Vue.use(VueWaypoint)

二、组件代码

<template>
  <div>
    <div class="nav">
      <div
        v-for="(item,index) of ['wayPoint1','wayPoint2','wayPoint3']"
        :class="{active:+activeIndex===+index}"
        :key="index">
        {{ item }}
      </div>
    </div>

    <div
      v-waypoint="{
        active: true,
        callback: onWaypoint,
        options: intersectionOptions
      }"
      data-index="0"
      class="waypoint"
    >
      wayPoint1
    </div>

    <div
      v-waypoint="{
        active: true,
        callback: onWaypoint,
        options: intersectionOptions
      }"
      data-index="1"
      class="waypoint"
    >
      wayPoint2
    </div>

    <div
      v-waypoint="{
        active: true,
        callback: onWaypoint,
        options: intersectionOptions
      }"
      data-index="2"
      class="waypoint"
    >
      wayPoint3
    </div>

    <div class="foot">foot</div>
  </div>
</template>

<script>
export default {
  name: 'WayPoint',
  data() {
    return {
      activeIndex: 0,
      viewList: new Set(),
      intersectionOptions: {
        root: null,
        rootMargin: '-42px 0px 0px 0px',
        threshold: [0, 1] // [0.25, 0.75] if you want a 25% offset!
      }
    }
  },
  methods: {
    onWaypoint({ el, going, direction }) {
      // going: in, out
      // direction: top, right, bottom, left
      // console.log(direction, el.innerText)
      // console.log(el.dataset.index)

      // console.log(el.innerText, going, direction)

      if (going === this.$waypointMap.GOING_IN) {
        // console.log(el.innerText, 'waypoint going in!')
        this.viewList.add(el.dataset.index)
        this.activeIndex = this.getActiveIndex()
      }

      if (going === this.$waypointMap.GOING_OUT) {
        // console.log(el.innerText, 'waypoint going out!')
        this.viewList.delete(el.dataset.index)
        this.activeIndex = this.getActiveIndex()
      }

      if (direction === this.$waypointMap.DIRECTION_TOP) {
        // console.log(el.innerText, 'waypoint going top!')
      }
    },
    getActiveIndex() {
      return [...this.viewList].sort((a, b) => a - b)[0]
    }
  }
}
</script>

<style scoped lang="scss">
.nav {
  position: sticky;
  top: 0;
  left: 0;
  display: flex;
  margin-bottom: 10px;

  > div {
    line-height: 30px;
    border: 1px solid #000;
    padding: 0 10px;
    transition: all .5s;
  }
}

.active {
  background-color: red;
}

.waypoint {
  background: cyan;
  height: 300px;
  margin-bottom: 10px;
}

.foot {
  background: grey;
  height: 200px;
}
</style>

二、效果图

在这里插入图片描述

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 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、付费专栏及课程。

余额充值