tsx封装锚点功能

本文介绍如何在Nuxt.js项目中利用TypeScript(TS)封装锚点功能,包括实现效果展示及关键代码解析。
摘要由CSDN通过智能技术生成

基于nuxt+ts封装的锚点功能

效果

第一页
点击333之后的效果

代码

子组件


<script lang='tsx'>
import { Component, Vue, Prop, Emit, Watch, Mutation } from 'nuxt-property-decorator'
interface anchorItem {
  icon: string,
  text: string,
  class: string,
  href: string
}
interface anchorContentItem {
  title: string,
  secondTitle: string
}
@Component
export default class SolutionItemComponent extends Vue {
  @Prop({
    type: Array,
    default () {
      return []
    }
  }) public anchorArr!: Array<anchorItem>;
  @Prop({
    type: Array,
    default () {
      return []
    }
  }) public anchorContent !: Array<anchorContentItem>
  // 是否在顶部
  public isPosTop: boolean = false
  
  // 锚点点击事件
  public clickAnchor (item) {
     this.anchorArr.forEach(item => {
      item.class = ''
    })
    item.class = 'active'
    let anchorTop: any = this.$refs.anchorTop
    let anchorTopHeight = anchorTop.clientHeight
    let menu: any = document.getElementsByClassName('head-box')[0]
    let menuHeight = menu.clientHeight
    let element: any = document.getElementById(item.href)
    let elementTop = element.offsetTop
    let oldScrollTop = document.getElementsByClassName('container-box')[0].scrollTop
    if (oldScrollTop < elementTop - anchorTopHeight - menuHeight) {
      let timer = setInterval(function () {
        let scrollTop = document.getElementsByClassName('container-box')[0].scrollTop
        if (scrollTop >= elementTop - anchorTopHeight - menuHeight) {
          clearInterval(timer)
        }
       
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值