antdV中Anchor 锚点总结

45 篇文章 2 订阅
1 篇文章 0 订阅
   <div :class="$style.title">
          <a-anchor
            :get-container="getContainerBox"
            :affix="false"
            :target-offset="targetOffset"
            :class="$style.anchor"
            @click="handleAnchorClick"
          >
            <a-anchor-link
              v-for="item in anchorList"
              :key="item.id"
              :href="`#${item.anchor}`"
              :class="$style.topMenuItem"
              :title="item.name"
            />
          </a-anchor>
          <span>
            <a-icon :class="$style.close" type="close" @click="onClose" />
          </span>
        </div>

注意点一

getContainerBox 绑定滚动范围容器

   getContainerBox() {
      return this.$refs.containerBox
    },

在这里插入图片描述

注意点二

  1. 容器样式

    .body {
      flex: 1;
      box-sizing: border-box;
      height: calc(100vh - 50px);
      margin: 5px 0;
      padding: 0 15px;
      overflow: auto;

      :global {
        .ant-form .ant-form-item {
          margin-bottom: 10px;
        }
      }
    }
  1. 选中的锚点样式
 .ant-anchor {
      display: flex !important;
    }

    .ant-anchor-ink::before {
      background-color: transparent;
    }

    .ant-anchor-link {
      margin: 10px 34px 10px 0;
      padding: 3px 6px;
    }

    .ant-anchor-link-active {
      font-weight: 600;
      border-bottom: 3px solid RGB(0, 179, 167);
    }

注意点三

1.阻止浏览器默认事件

 handleAnchorClick(e, link) {
      // 阻止 a 标签默认事件
      e.preventDefault()
      if (link.href) {
        // 找到锚点对应得的节点
        let element = document.getElementById(link.href)
        // 如果对应id的锚点存在,就跳滚动到锚点顶部
        element && element.scrollIntoView({ block: 'end', behavior: 'smooth', alignToTop: 'false' })
      }
    },

注意点四

可能你定义的锚点位置与你真正想要的位置有点偏差 可以设置 targetOffset传一个offset值

  • 3
    点赞
  • 10
    收藏
    觉得还不错? 一键收藏
  • 2
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值