vue 双向锚点实现 简易版(scrollIntoView)

49 篇文章 1 订阅
40 篇文章 0 订阅

一. 需求:左边是内容板块,右边是目录结构,点击右边内容跳转到左边相应位置展示,滑动左边内容右边目录自动跳转。 

 二、实现:

  1. 左边每一个内容模块都给一个ref和应该相同的class类名,方便获取dom;
  2. 左边内容区域使用滑动事件@scroll="handleScroll",内容区域滑动即触发该方法;
  3. 右边使用点击事件@click="goAnchor('anchor-' + index, index)"获取当前点击的dom;

  • handleScroll()  滚动监听方法实现滑动左边内容对应上右边目录

  •  goAnchor() 右边锚点选中跳转相应内容区域,通过scrollIntoView({ behavior: 'smooth' })平滑跳转

  3.代码:

<template>
  <div class="panel-block flex">
    <div class="panel-left" ref="anchorRef" @scroll="handleScroll">
      <div class="panel-item anchor-item" ref="anchor-0">
        <div class="panel-item-title">内容区域1</div>
        xxx
      </div>
     <div class="panel-item anchor-item" ref="anchor-1">
        <div class="panel-item-title">内容区域2</div>
        xxx
      </div>
      <div class="panel-item anchor-item" ref="anchor-2">
        <div class="panel-item-title">内容区域3</div>
        <div class="panel-item-content">
         xxx
        </div>
      </div>
      <div class="panel-item anchor-item" ref="anchor-3">
        <div class="panel-item-title">内容区域4</div>
        <div class="panel-item-content">
         xxx
        </div>
      </div>
    </div>
    <div class="panel-right">
      <div class="step-line"></div>
      <ul class="step-ul">
        <li
          class="flex_align_item_center"
          v-for="(item, index) in stepData"
          :key="index"
          :class="{ 'step-active': activeBtn == index }"
          @click="goAnchor('anchor-' + index, index)"
        >
          <div class="step-icon"></div>
          <div class="step-label">{{ item }}</div>
        </li>
      </ul>
    </div>
  </div>
</template>

<script>
export default {
  data() {
    return {
      activeBtn: 0, //锚点按钮
      stepData: ['区域1', '区域2', '区域3', '区域4'],
      contentDiv: null, //锚点区域
    }
  },
  methods: {
    //锚点跳转
    goAnchor(selector, index) {
      this.activeBtn = index
      this.$refs[selector].scrollIntoView({ behavior: 'smooth' })
    },
    // 滚动监听器
    handleScroll(i) {
      // 获取所有锚点元素
      const navContents = document.querySelectorAll('.anchor-item')
      // 所有锚点元素的 offsetTop
      const offsetTopArr = []
      navContents.forEach((item) => {
        offsetTopArr.push(item.offsetTop)
      })
      // 获取当前文档流的 scrollTop
      const scrollTop = this.$refs.anchorRef.scrollTop
      offsetTopArr.forEach((item, index) => {
        if (scrollTop >= item) {
          this.activeBtn = index
        }
      })
    },
  }
}
</script>

-----------------以下为样式代码----------------------
<style lang="scss" scoped>
.panel-block {
    height: 100%;
    .panel-left {
      width: calc(100% - 180px);
      overflow-y: auto;
      height: 100%;
      .panel-item {
        .panel-item-content {
          padding: 10px;
          border-top: 0;
          min-height: 40px;
          .table-params {
            .table-header {
              height: 40px;
              line-height: 40px;
              width: 100%;
              border: 1px solid #ebeef5;
              border-bottom: none;
              background: #f5f7fa;
              font-weight: 700;
              font-size: 14px;
              color: #303133;
              padding-left: 15px;
            }
            .table-body-type {
              height: 32px;
              line-height: 32px;
              width: 100%;
              border: 1px solid #ebeef5;
              border-bottom: none;
              color: #303133;
              padding-left: 15px;
              .type-title {
                font-weight: 700;
                font-size: 12px;
              }
            }
          }
        }
      }
    }
    .panel-right {
      padding-top: 10px;
      position: fixed;
      left: calc(100% - 210px);
      .step-line {
        position: absolute;
        left: 6px;
        top: 0;
        width: 1px;
        background: #dcdfe6;
        height: calc(50vh - 85px);
        z-index: 0;
      }
      .step-ul {
        li {
          padding-bottom: 20px;
          .step-icon {
            width: 13px;
            height: 13px;
            margin-right: 20px;
            border-radius: 50%;
            z-index: 1;
          }
          .step-label {
            font-weight: 700;
            font-size: 14px;
            line-height: 22px;
            color: #303133;
          }
        }
        .step-active {
          .step-icon {
            border: 1px solid #1989fe;
            background: #fff;
          }
          .step-label {
            color: #1989fe;
          }
        }
      }
    }
    ::-webkit-scrollbar {
      width: 0 !important;
    }
  }
</style>

  • 7
    点赞
  • 48
    收藏
    觉得还不错? 一键收藏
  • 7
    评论
Vue锚点定位双向滚动是一种实现在页面中点击锚点链接时,页面能够滚动到对应位置的功能。这个功能可以通过监听滚动事件和使用ref属性来实现。首先,在左边的内容模块中给每个模块添加一个ref属性和相同的class类名,以便获取对应的DOM元素。然后,在内容区域添加滑动事件@scroll="handleScroll",当内容区域滑动时触发handleScroll方法。接下来,在右边的锚点链接中添加点击事件@click="goAnchor('anchor-' + index, index)",当点击锚点链接时,调用goAnchor方法获取对应的DOM元素。在goAnchor方法中,可以使用scrollIntoView方法将对应的DOM元素滚动到可视区域内实现双向滚动效果。这样,当点击锚点链接时,页面会滚动到对应的位置。 #### 引用[.reference_title] - *1* [vue实现锚点双向滚动/文章章节联动滚动效果](https://blog.csdn.net/qq_36604536/article/details/126936016)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^insert_down28v1,239^v3^insert_chatgpt"}} ] [.reference_item] - *2* [vue 双向锚点实现 简易版scrollIntoView)](https://blog.csdn.net/weixin_47978760/article/details/127808313)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^insert_down28v1,239^v3^insert_chatgpt"}} ] [.reference_item] [ .reference_list ]

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值