长文本锚点定位

代码(Javascript)

<template>
  <ul class="search-results" :id="'search-results-info'">
    <li
      class="search-result-item"
      v-for="issue,index in issueList"
      :key="issue.id"
      :id="issue.id"
      v-show="!!issueList?.length"
    >
          <h3 :id="'section-' + index">
            {{ issue.title }}
          </h3>
    </li>
  </ul>
</template>
<script>
export default {
  data() {
    return {

    };
  },
  methods: {
  //点击目录跳转页面
  handleContentJump(val) {
      this.isJump = true;
      let sectionIndex = 1;
      if(val && this.issueList?.length) {
      this.issueList.forEach((issue:any,index:any) => {
        if(issue?.data?.answers?.length) {
          issue.data.answers.forEach((item:any,index2:any) => {
            if(item.content === val) {
              sectionIndex = index2;
              return;
            }
      })
    }
    if(sectionIndex) return;
      })
    }
    if(sectionIndex) {
    // 需要跳转的子节点
      const ele = document.getElementById('section-' + sectionIndex);
      // 父级节点
      const parent = document.getElementById('search-results-info');
      // 主要依赖这里实现滚动定位
      parent.scrollTo({
        top: ele.offsetTop,
        behavior: 'smooth'
      });
      //跳转的时候暂停监听页面滚动
      setTimeout(() => {
        this.isJump = false;
      }, 1000);
    }
    } 
  }
};
</script>

<style scoped>
</style>


同时,贴上咱的个人博客,欢迎各位来访~
青枫阁

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值