当前位置: 代码网 > it编程>前端脚本>Vue.js > vue关于锚点定位、跳转到指定位置实现方式

vue关于锚点定位、跳转到指定位置实现方式

2024年05月26日 Vue.js 我要评论
vue锚点定位、跳转到指定位置直接上代码<template> <div> <nav class="nav"> <button @click=

vue锚点定位、跳转到指定位置

直接上代码

<template>
  <div>
    <nav class="nav">
      <button @click="go('#m1')">昨天</button>
      <button @click="go('#m2')">今天</button>
      <button @click="go('#m3')">明天</button>
    </nav>

    <div style="height: 50px;"></div>

    <div id="m1">
      <h1>昨天</h1>
      <div v-for="item in 50">昨天</div>
    </div>

    <div id="m2">
      <h1>今天</h1>
      <div v-for="item in 50">今天</div>
    </div>

    <div id="m3">
      <h1>明天</h1>
      <div v-for="item in 50">明天</div>
    </div>
  </div>
</template>

<script>
export default {

  data() { return {}; },
  methods: {
    go(selector) {
      document.queryselector(selector).scrollintoview({
        // 不想要滚动动画,则设置为"instant"
        behavior: 'smooth', // 滚动动画
        block: 'center'
      });
    }
  }
};
</script>

<style scoped>
.nav {
 text-align: center;
 position: fixed;
 top: 0px;
 width: 100%;
 height: 50px;
 box-shadow: 0 2px 12px 0 rgba(0, 0, 0, 0.1);
 background: #fff;
}
</style>

vue中锚点定位scrollintoview

 <el-card class="maodian">
            <div slot="header" class="clearfix">
              <span>maodian</span>
            </div>
           
</el-card>
 
 <el-button
                   
                      type="text"
                      @click="handlejump('.maodian')"
                      >maodian</el-button>
 
    jump(val) {
      const dom = document.queryselector(val);
      if (dom) {
        dom.scrollintoview(true);
      }
    },

使用element.scrollintoview跳转

总结

以上为个人经验,希望能给大家一个参考,也希望大家多多支持代码网。

(0)

相关文章:

版权声明:本文内容由互联网用户贡献,该文观点仅代表作者本人。本站仅提供信息存储服务,不拥有所有权,不承担相关法律责任。 如发现本站有涉嫌抄袭侵权/违法违规的内容, 请发送邮件至 2386932994@qq.com 举报,一经查实将立刻删除。

发表评论

验证码:
Copyright © 2017-2025  代码网 保留所有权利. 粤ICP备2024248653号
站长QQ:2386932994 | 联系邮箱:2386932994@qq.com