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
}
}
})
如果路由有问题
就用方法
<div style="position:fixed;top:10%;right:13%;">
<div style="margin-bottom:15px"><a @click.prevent="custormAnchor('info1')" rel="external nofollow" class="topFloat">1</a></div>
<div style="margin-bottom:15px"><a @click.prevent="custormAnchor('info2')" rel="external nofollow" class="topFloat">2</a></div>
<div style="margin-bottom:15px"><a @click.prevent="custormAnchor('info3')" rel="external nofollow" class="topFloat">3</a></div>
<div style="margin-bottom:15px"><a @click.prevent="custormAnchor('info4')" rel="external nofollow" class="topFloat">4</a></div>
</div>
全局minx定义方法
custormAnchor(anchorName) {
// 找到锚点
let anchorElement = document.getElementById(anchorName);
// 如果对应id的锚点存在,就跳转到锚点
if(anchorElement) {
anchorElement.scrollIntoView()
}
},
vue锚点的更多相关文章
- vue 锚点定位
vue 锚点定位 <template> <div class="details"> <div class="wrapper w"& ...
- 锚点 , angular 锚点 vue锚点
因为最近在开发angular,自己有路由 用window.location跳到默认路由,查了半天用angular方式不好解决 ,so 原生走起 START scrollIntoView是一个与页面(容 ...
- Vue 爬坑之路(七)—— 监听滚动事件 实现动态锚点
前几天做项目的时候,需要实现一个动态锚点的效果 如果是传统项目,这个效果就非常简单.但是放到 Vue 中,就有两大难题: 1. 在没有 jQuery 的 animate() 方法的情况下,如何实现平滑 ...
- vue项目锚点的使用
在vue项目中如何使用锚点呢? 在vue-router中定义 scrollBehavior scrollBehavior (to, from, savedPosition) { if (savedPo ...
- vue中检测敏感词,锚点
当发布文章的时候,标题有敏感词 则检测有敏感词的接口成功的时候,写锚点 eg: _this .$alert("检测到标题有敏感词,请修改后再发布", "提示", ...
- Vue路由scrollBehavior滚动行为控制锚点
使用前端路由,当切换到新路由时,想要页面滚到顶部,或者是保持原先的滚动位置,就像重新加载页面那样. vue-router 能做到,而且更好,它让你可以自定义路由切换时页面如何滚动. 注意: 这个功能只 ...
- vue中的坑 --- 锚点与查询字符串
在vue中,由于是单页面SPA,所以需要使用锚点来定位,在vue的官方文档中提到过也可以不使用锚点的情况,就是在vue-router中使用history模式,这样,在url中就不会出现丑陋的#了,但是 ...
- vue中的锚点和查询字符串
1.什么是锚点 锚点(achor):其实就是超链接的一种. 如:普通的超链接 <a href="sub_task_detail.php">详细</a> 主 ...
- 超详细Vue实现导航栏绑定内容锚点+滚动动画+vue-router(hash模式可用)
超详细Vue实现导航栏绑定内容锚点+滚动动画+vue-router(hash模式可用) 转载自:https://www.jianshu.com/p/2ad8c8b5bf75 亲测有效~ <tem ...
随机推荐
- ISO/IEC 9899:2011 条款6.5.3——单目操作符
6.5.3 单目操作符 语法 1.unary-expression: postfix-expression ++ unary-expression -- unary-expression unar ...
- ROS tf广播编写
博客参考:https://www.ncnynl.com/archives/201702/1310.html ROS与C++入门教程-tf-编写tf broadcaster(广播) 说明: 介绍如何广播 ...
- 0.9.0.RELEASE版本的spring cloud alibaba sentinel+gateway网关实例
sentinel除了让服务提供方.消费方用之外,网关也能用它来限流.我们基于上次整的网关(参见0.9.0.RELEASE版本的spring cloud alibaba nacos+gateway网关实 ...
- 构造方法(和python初始化变量类似)
public class Demo1 { int name2=1; public Demo1(int name) { name=name2; } public Demo1() { } public ...
- Python机器学习实践指南pdf (中文版带书签)、原书代码、数据集
Python机器学习实践指南 目 录 第1章Python机器学习的生态系统 1 1.1 数据科学/机器学习的工作 流程 2 1.1.1 获取 2 1.1.2 检查和探索 2 1.1.3 清理和准备 3 ...
- 深入理解ElasticSearch(PDF版 内含目录)
深入理解ElasticSearch 介绍: 本书涵盖了Elasticsearch的许多中高级功能,并介绍了缓存.ApacheLucene库以及监控等模块的内部运作机制.其中,还涉及一些实用案例,比如配 ...
- centos7上安装mysql8(下)
1.修改root密码 MySQL8和5的密码加密方式不同,mysql_native_password是5的加密方式.mysql已经将之前的mysql_native_password认证,修改成了cac ...
- C# WebClient 无法设置超时时间的解决办法
http://www.lenashane.com/article/20150624-1021.html
- React:快速上手(8)——前后端分离的跨域访问与会话保持
React:快速上手(8)——前后端分离的跨域访问与会话保持 跨域访问 跨域是指从一个域名的网页去请求另一个域名的资源.比如从http://www.baidu.com/ 页面去请求http://www ...
- web端自动化——selenium3用法详解
selenium中文学习文档链接:https://selenium-python-zh.readthedocs.io/en/latest/getting-started.html selenium3+ ...