element步骤条增加锚点的实现

element的步骤条默认样式是无法点击的,需求中有点击步骤条,页面滚动到特定锚点需求,实现如下

 <el-card shadow="never" :body-style="{ padding: '5px' }"  id="mySteps">
            <el-steps simple>
              <el-step v-for="(step,index) in laSteps" :title="step.title" :icon="step.icon"
                       :key="index"
                       :href="step.href" :status="step.status" v-if="step.isShow"></el-step>
            </el-steps>
          </el-card>
  1. 每个步骤条中 :href=“step.href” 锚点是通过一个href与一个id实现跳转,这里的href仅仅作为一种标识与需要跳转的点id相同即可
   $('.el-step__title').click(function () {
        var goHref = '#' + $(this).parent().parent().attr('href')
        var anchor = that.$el.querySelector(goHref)
        $('#djla').animate({
          scrollTop: anchor.offsetTop
        }, 500);
      });
  1. 获取到href的值,根据这个值决定跳转到什么地方.
  2. $(’#djla’)为jq选中整个需滚动区域
  • 1
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
在 Vue 中实现滚动监听到锚点,可以通过以下步骤: 1. 在需要监听滚动的容器上,绑定 `@scroll` 事件。 2. 通过 `getBoundingClientRect()` 方法获取每个锚点元素的位置信息,得到它们距离容器顶部的距离。 3. 在滚动事件处理函数中,获取当前容器滚动的距离,并与每个锚点元素的位置信息进行比较,判断当前容器滚动到了哪个锚点的位置。 4. 将滚动到的锚点信息保存在 Vue 实例的数据中,并在模板中根据该数据展示对应的内容。 以下是一个简单的示例代码: ```vue <template> <div class="scroll-container" @scroll="handleScroll"> <div class="section" v-for="(section, index) in sections" :key="index"> <h2>{{ section.title }}</h2> <p>{{ section.content }}</p> </div> </div> </template> <script> export default { data() { return { activeSection: 0, // 默认选中第一个锚点 sections: [ { title: "Section 1", content: "Content 1" }, { title: "Section 2", content: "Content 2" }, { title: "Section 3", content: "Content 3" }, { title: "Section 4", content: "Content 4" }, ], }; }, mounted() { this.calculatePositions(); }, methods: { calculatePositions() { // 获取每个锚点元素的位置信息 this.positions = this.sections.map((section) => { const element = document.getElementById(section.title); return { title: section.title, top: element.getBoundingClientRect().top + this.$refs.container.scrollTop, }; }); }, handleScroll() { const scrollTop = this.$refs.container.scrollTop; // 判断当前滚动到了哪个锚点 for (let i = 0; i < this.positions.length; i++) { const position = this.positions[i]; if (scrollTop >= position.top && scrollTop < position.top + 400) { this.activeSection = i; break; } } }, }, }; </script> ``` 在该示例中,使用 `getBoundingClientRect()` 方法获取每个锚点元素的位置信息,并保存在 `positions` 数组中。在滚动事件处理函数中,获取当前容器滚动的距离,并与每个锚点元素的位置信息进行比较,判断当前容器滚动到了哪个锚点的位置。最后将滚动到的锚点信息保存在 `activeSection` 中,用于在模板中展示对应的内容。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值