美文网首页
移动端锚点实现 scrollIntoView()

移动端锚点实现 scrollIntoView()

作者: 苏苡 | 来源:发表于2023-12-21 15:09 被阅读0次

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>

相关文章

  • 吸顶和vue实现锚点跳转

    另vue实现锚点跳转之scrollIntoView()方法滚动到某个特定元素 :scrollIntoView();...

  • vue中的锚点

    今天写搭建一个PC网站,用到返回顶点的功能,想到了锚点用到了scrollIntoView()方法 这样点击就能跳转...

  • 移动端 vue 中实现锚点定位

    期望效果:点击每个锚点,能够定位到锚点所在的位置。(代码规范采用的是eslint) 简单粗暴代码如下所示: ...

  • 锚点跳转

    1.锚点跳转 可使用scrollIntoView()方法将调用它的元素滚动到浏览器窗口的可见区域 2.语法 ele...

  • 移动端锚点定位

    参考: 去哪儿:https://touch.dujia.qunar.com/p/item/3807693735?e...

  • vue 滚动和过度动画

    1. 组件内滚动 组件内滚动到指定锚点 scrollIntoView在组件中 组件内滚动到指定位置初始化的化滚动,...

  • AI快捷键分享,看完果断收藏!

    工具箱 移动工具 【V】 直接选取工具、组选取工具 【A】 钢笔、添加锚点、删除锚点、改变路径角度 【P】 添加锚...

  • 锚点定位之-scrollIntoView

    1页面代码 2 js代码 样式(随意写了写):

  • iview 锚点爬坑 && vue 锚点

    在vue中实现锚点定位效果 iview的锚点 原生锚点实现 iview爬坑点 且需要把滚动容器的id或者class...

  • 鼠标控制旋转

    鼠标在PC端移动,实现对象的旋转;手指在安卓端移动,实现对象的旋转。

网友评论

      本文标题:移动端锚点实现 scrollIntoView()

      本文链接:https://www.haomeiwen.com/subject/gewxndtx.html