vue实现描点链接跳转

<!--
 * @Author: xiaohe
 * @LastEditors: xiaohe
-->
<template>
  <div class="score-preview-container">
    <div class="nav-button-box">
      <div
        class="nav-button"
        v-for="(item, index) in buttonArr"
        :key="index"
        :class="{ active: activeBtn == index }"
      >
        <span @click="goAnchor('anchor-' + index, index)">
          {{ item.name }}</span
        >
      </div>
    </div>
    <div id="app-root" ref="app_root" class="right-content">
      <div class="contentbox">
        <div class="content-page-box">
          <div id="anchor-0">1</div>
          <div id="anchor-1">2</div>
          <div id="anchor-2">3</div>
          <div id="anchor-3">4</div>
          <div id="anchor-4">5</div>
          <div id="anchor-5">6</div>
          <div id="anchor-6">7</div>
          <div id="anchor-7">8</div>
        </div>
      </div>
    </div>
  </div>
</template>
<script>
export default {
  components: {},
  data() {
    return {
      buttonArr: [
        { name: "工商信息" },
        { name: "股东信息" },
        { name: "法律风险" },
        { name: "经营风险" },
        { name: "关联图谱" },
        { name: "舆情信息" },
        { name: "知识产权" },
        { name: "1111" },
      ],
      activeBtn: 0,
    };
  },
  mounted() {
    document.addEventListener("scroll", this.handleScroll, true);
  },
  destroyed() {
    document.removeEventListener("scroll", this.handleScroll, true);
  },
  methods: {
    //锚点跳转
    goAnchor(selector, index) {
      this.activeBtn = index;
      let top = document.getElementById(selector).offsetTop - 50;
      this.$refs.app_root.scroll({ top: top, left: 0, behavior: "smooth" });
    },
    // 滚动监听描点链接
    handleScroll() {
      let scrollTop = document.getElementById("app-root").scrollTop; // 滚动条偏移量\
      //获取每个模块元素的高
      var objData = {};
      for (let i = 0, len = this.buttonArr.length; i <= len; i++) {
        let anchor = `anchor-${i}`;

        if (this.$el.querySelector("#" + anchor)) {
          objData["anchorOffset" + i] =
            this.$el.querySelector("#" + anchor).offsetTop - 50;
        }
      }
      //tabs当前下标
      for (var i = 0, len = this.buttonArr.length; i <= len; i++) {
        if (objData["anchorOffset" + i] > scrollTop && i > 0) {
          this.activeBtn = i - 1;
          break;
        } else if (objData["anchorOffset" + i] == undefined) {
          console.log(this.buttonArr.length);
          this.activeBtn = this.buttonArr.length - 1;
          break;
        }
      }
    },
  },
};
</script>

<style lang="less" scoped >
.section {
  margin-bottom: 10px;
  background: #fff;
}
.score-preview-container {
  display: flex;
  flex-direction: column;
  height: 97%;
  margin: 9px 24px;
  position: relative;
  .nav-button-box {
    flex-shrink: 0;
    background: #fff;
    margin-top: 19px;
    width: 100px;
    position: absolute;
    .nav-button {
      width: 100%;
      font-size: 14px;
      color: rgba(0, 0, 0, 0.85);
      cursor: pointer;
      line-height: 40px;
      text-align: center;
    }
    span:hover {
      color: #0964ff;
    }
    .active {
      color: #0964ff;
    }
  }
}

#app-root {
  display: block;
  overflow: auto;
  flex-grow: 1;
  width: 100%;
  height: 700px;
  text-align: center;
}
#app-root::-webkit-scrollbar {
  display: none !important;
}
#anchor-0 {
  background: brown;
  width: 100%;
  height: 300px;
}
#anchor-1 {
  background: blueviolet;
  width: 100%;
  height: 300px;
}
#anchor-2 {
  background: yellow;
  width: 100%;
  height: 300px;
}
#anchor-3 {
  background: blue;
  width: 100%;
  height: 300px;
}
#anchor-4 {
  background: red;
  width: 100%;
  height: 300px;
}
#anchor-5 {
  background: rgb(0, 174, 255);
  width: 100%;
  height: 3100px;
}
#anchor-6 {
  background: yellowgreen;
  width: 100%;
  height: 800px;
}
#anchor-7 {
  background: black;
  width: 100%;
  height: 800px;
}
.title_name {
  display: flex;
  font-size: 18px;
  padding: 30px 0 0 20px;
  color: rgba(0, 0, 0, 0.85);

  .link_title {
    width: 104px;
    display: inline-block;
  }
}

.quick_view {
  display: -webkit-flex;
  display: flex;
  flex-wrap: wrap;
  //width: 100%;
  padding-left: 25px;

  a {
    font-size: 12px;
    text-align: left;
    line-height: 25px;
    color: #6c7489;
    margin-right: 12px;
    cursor: default;

    span {
      display: inline-block;
      position: relative;
      top: 0;
      right: 0;
      height: 16px;
      padding: 0 5px;
      margin: 0 8px;
      font-size: 12px;
      line-height: 16px;
      text-align: center;
      background: #fff0eb;
      color: #1089ff;
      border-radius: 4px;
    }

    .gray {
      background: rgba(163, 171, 181, 0.2);
      color: #9ba7b6;
    }
  }
}
</style>

 

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值