vue中锚点的三种方法

news/2024/7/10 0:22:17 标签: vue

第一种:

router.js中添加

mode: 'history',
   srcollBehavior(to,from,savedPosition){
    if(to.hash){
  return {
selector:to.hash
 }
  }
 }

组件:

<template>
<div>
<ul class="list">
<li><a href="#1" rel="external nofollow" >星期1</a></li>
<li><a href="#2" rel="external nofollow" >星期2</a></li>
<li><a href="#3" rel="external nofollow" >星期3</a></li>
<li><a href="#4" rel="external nofollow" >星期4</a></li>
<li><a href="#5" rel="external nofollow" >星期5</a></li>
<li><a href="#6" rel="external nofollow" >星期6</a></li>
<li><a href="#7" rel="external nofollow" >星期7</a></li>
</ul>
<div class="main_con" id="1">11111111111111111111111111111111</div>
<div class="main_con" id="2">22222222222222222222222222222222222</div>
<div class="main_con" id="3">33333333333333333333333333333333333333</div>
<div class="main_con" id="4">444444444444444444444444444444444444444</div>
<div class="main_con" id="5">555555555555555555555555555555555555555</div>
<div class="main_con" id="6">666666666666666666666666666666666666666</div>
<div class="main_con" id="7">7777777777777777777777777777777777777777</div>
</div>
</template>
<script>
export default {
data(){
return {
}
}
}
</script>
<style>
.list{
width: 100%;
height: 50px;
}
li{
width: 11%;
height: 50px;
line-height: 50px;
text-align: center;
border: 1px solid #ccc;
color: #ff6c00;
float: left;
list-style: none!important;
}
.main_con{
width: 100%;
height: 200px;
border: 1px solid #ccc;
line-height: 200px;
text-align: center;
color: blue;
}
</style>
//前端全栈学习交流圈:866109386
//面向1-3经验年前端开发人员
//帮助突破技术瓶颈,提升思维能力

第二种:

写一个方法 组件

<template>
 <div>
  <div><a href="javascript:void(0)" rel="external nofollow" rel="external nofollow" @click="goAnchor('#anchor-'+index)" v-for="index in 20"> {{index}} </a></div>
  <div :id="'anchor-'+index" class="item" v-for="index in 20">{{index}}</div>
</div>
  </template>
<script>
export default{
data(){
return {
}
},
methods: {
  goAnchor(selector) {
     var anchor = this.$el.querySelector(selector)
     document.documentElement.scrollTop = anchor.offsetTop
  }
 }
}
</script>
<style>
.item{
width: 100%;
height: 200px;
line-height: 200px;
text-align: center;
}
</style>
//前端全栈学习交流圈:866109386
//面向1-3经验年前端开发人员
//帮助突破技术瓶颈,提升思维能力

第三种: 自定义指令

<template>
<div>
  <div><a href="javascript:void(0)" rel="external nofollow" rel="external nofollow" v-anchor="index" v-for="index in 20"> {{index}} </a></div>
  <div :id="'anchor-'+index" class="item" v-for="index in 20" >{{index}}</div>
</div>
</template>
<script>
export default{
data(){
return {
}
}
}
</script>
<style>
.item{
width: 100%;
height: 200px;
line-height: 200px;
text-align: center;
}
</style>

main.js 定义全局指令 方便其他地方复用

Vue.directive('anchor',{
inserted : function(el,binding){
el.onclick = function(){
  document.documentElement.scrollTop = $('#anchor-'+binding.value).offset().top
}
}
})

http://www.niftyadmin.cn/n/1301038.html

相关文章

CocosCreator在电脑Web打印vConsole日志的问题

忘了什么时候开始&#xff0c;Web端的日志打印的文件输入信息全是vconsole.min.js文件&#xff0c;很纠结啊有木有&#xff0c;完全不知道日志的出处。 日志输入如以下图片&#xff1a; 官方还没有给出对于这个问题的配置是怎么样解决的&#xff0c;所以我们自己搞定&#xff…

using 模板的别名

#include "stdafx.h" #include <iostream> #include <type_traits> using namespace std; /* template (模板)是用来产生 template class (模板类型,是类型)。 在标准 C++,typedef 可定义类型的别名,但是不能够使用 typedef 来定义模板的别名…

Vue不能检测到Object/Array更新的情况的解决

前言 看文档不认真&#xff0c;开发也没有多注意&#xff0c;总是hack。忽悠忽悠就过去&#xff0c;但怎么说&#xff0c;歪门邪道还是不太好&#xff0c;现在就亡羊补牢&#xff0c;总结总结。 数组 索引 使用下标更新数组元素&#xff1b;使用赋值方式改变数组长度&#x…

CocosCreator修改js文件模板

正常我们在CocosCreator编辑器里新建的JS文件是这样的&#xff1a; 这上面有一部分我们是不需要的&#xff0c;所以每次新建文件的时候&#xff0c;都需要手动把他们删除掉&#xff0c;很尴尬啊有木有。 所以我们需要定制自己的JS文件模板&#xff1a; 首先&#xff0c;我们打…

ES6与CommonJS中的模块处理的区别

ES6和CommonJS都有自己的一套处理模块化代码的措施&#xff0c;即JS文件之间的相互引用。 为了方便两种方式的测试&#xff0c;使用nodejs的环境进行测试 CommonJS的模块处理 使用require来引入其他模块的代码&#xff0c;使用module.exports来引出 // exportDemo.js count…

指向类成员变量的指针int A:: * p

 #include "stdafx.h" #include <iostream> #include <type_traits> using namespace std; class A {public : double o; //制造一个偏移量 int a; }; class B: public A {public: int b; }; int main() { A a1 ; …

纹理压缩简介 DXT PVR ETC

文章转载自&#xff1a;https://www.jianshu.com/p/a0d7eed9f44d 在软件开发&#xff0c;特别是三维应用中&#xff0c;纹理随处可见&#xff0c;但受限于网络环境https://www.jianshu.com/p/a0d7eed9f44d和硬件能力&#xff0c;纹理也是一大瓶颈。而且在一般的三维应用中&…

Vue 在使用中的一些小技巧

在vue的使用过程中会遇到各种场景&#xff0c;当普通使用时觉得没什么&#xff0c;但是或许优化一下可以更高效更优美的进行开发。 1. 多图表resize事件去中心化 1.1 一般情况 有时候我们会遇到这样的场景&#xff0c;一个组件中有几个图表&#xff0c;在浏览器resize的时候…