移动端锚点实现 scrollIntoView()

vue2处理

<!-- vue tab切换 -->
<template>
  <div class="tabs">
    <div class="tab" :class="active == 1 ? 'tab-active' : ''" @click="tabClick(1, 'setRef1')">人员信息</div>
    <div class="tab" :class="active == 2 ? 'tab-active' : ''" @click="tabClick(2, 'setRef2')"> 事件信息 </div>
    <div class="tab" :class="active == 3 ? 'tab-active' : ''" @click="tabClick(3, 'setRef3')">办理记录</div>
    <div class="tab" :class="active == 4 ? 'tab-active' : ''" @click="tabClick(4, 'setRef4')"> 历次信访活动</div>
  </div>

  <div class="content">
    <div class="content-item" ref="setRef1"></div>
    <div class="content-item" ref="setRef2"></div>
    <div class="content-item" ref="setRef3"></div>
    <div class="content-item" ref="setRef4"></div>
 </div>
</template>
<script>

// vue2跳转处理
export default {
  data() {
    return {},
  },
  mounted() {},
  methods: {
    tabClick(val, formName) {
      this.active = val
      this.$refs[formName].scrollIntoView({
        block: 'start',
        behavior: 'smooth',
        inline: 'nearest',
     })
  },
}
</script>
<style>
// 锚点样式 $xxx  是封装的样式 自己看
.tabs {
  width: 100%;
  line-height: 50px;
  display: flex;
  justify-content: space-around;
  background-color: $cardBg;
  z-index: 2;
  .tab {
    line-height: 30px;
    color: $activeTitleFont;
  }
  .tab-active {
    font-weight: 500;
    color: $subMenuBg;
    border-bottom: 2.3px solid $subMenuBg;
  }
}
.content {
  margin: 50px 0 10px;
  .content-item {
    padding: 10px;
    background: $cardBg;
    margin-bottom: 5px;
  }
}
</style>

vue3处理

<!-- vue tab切换 -->
<template>
  <div class="tabs">
    <div class="tab" :class="active == 1 ? 'tab-active' : ''" @click="tabClick(1)">人员信息</div>
    <div class="tab" :class="active == 2 ? 'tab-active' : ''" @click="tabClick(2)"> 事件信息 </div>
    <div class="tab" :class="active == 3 ? 'tab-active' : ''" @click="tabClick(3)">办理记录</div>
    <div class="tab" :class="active == 4 ? 'tab-active' : ''" @click="tabClick(4)"> 历次信访活动</div>
  </div>

  <div class="content">
    <div class="content-item" ref="setRef1"></div>
    <div class="content-item" ref="setRef2"></div>
    <div class="content-item" ref="setRef3"></div>
    <div class="content-item" ref="setRef4"></div>
 </div>
</template>
<script setup>
import {  ref } from 'vue'

const setRef1 = ref(null)
const setRef2 = ref(null)
const setRef3 = ref(null)
const setRef4 = ref(null)

const tabClick = (val) => {
  state.active = val
  if (val == 1) {
      setRef1.value.scrollIntoView({ block: 'start', behavior: 'smooth', inline: 'nearest', })
  } else if (val == 2) {
    setRef2.value.scrollIntoView({ block: 'start', behavior: 'smooth', inline: 'nearest', })
  } else if (val == 3) { 
    setRef3.value.scrollIntoView({ block: 'start', behavior: 'smooth', inline: 'nearest', })
  } else if (val == 4) { 
    setRef4.value.scrollIntoView({ block: 'start', behavior: 'smooth', inline: 'nearest', })
  }
}
</script>
<style>
// 锚点样式 同上
</style>
©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

相关阅读更多精彩内容

友情链接更多精彩内容