传统的锚点定位会与vue-router中的路由设置存在冲突,都是使用'#'进行的,所以这里使用一直方法来模拟锚点跳转,并使用tween.js达到动态的过度效果

不使用原生锚点,使用这种方式解决

import '../static/js/animation.js'
import '../static/js/tween.js' <a href="javascript:;" @click="anchor(dom)" class="title">xxxx</a> methods:{
anchor:function(e){
let id='anchor-'+e;
let anchor=document.getElementById(id);
let go=anchor.offsetTop; Math.animation(document.documentElement.scrollTop,go,600,'Quart.easeOut', function (value) {
document.documentElement.scrollTop = value; // animation为封装的方法
document.body.scrollTop = value; //兼容
});
}
}

animation.js

// 对运动方法进行封装
Math.animation = function (from, to, duration, easing, callback) {
var isUndefined = function (obj) {
return typeof obj == 'undefined';
};
var isFunction = function (obj) {
return typeof obj == 'function';
};
var isNumber = function(obj) {
return typeof obj == 'number';
};
var isString = function(obj) {
return typeof obj == 'string';
}; // 转换成毫秒
var toMillisecond = function(obj) {
if (isNumber(obj)) {
return obj;
} else if (isString(obj)) {
if (/\d+m?s$/.test(obj)) {
if (/ms/.test(obj)) {
return 1 * obj.replace('ms', '');
}
return 1000 * obj.replace('s', '');
} else if (/^\d+$/.test(obj)) {
return +obj;
}
}
return -1;
}; if (!isNumber(from) || !isNumber(to)) {
if (window.console) {
console.error('from和to两个参数必须且为数值');
}
return 0;
} // 缓动算法
var tween = Math.tween || window.Tween; if (!tween) {
if (window.console) {
console.error('缓动算法函数缺失');
}
return 0;
} // duration, easing, callback均为可选参数
// 而且顺序可以任意
var options = {
duration: 300,
easing: 'Linear',
callback: function() {}
}; var setOptions = function(obj) {
if (isFunction(obj)) {
options.callback = obj;
} else if (toMillisecond(obj) != -1) {
options.duration = toMillisecond(obj);
} else if (isString(obj)) {
options.easing = obj;
}
};
setOptions(duration);
setOptions(easing);
setOptions(callback); // requestAnimationFrame的兼容处理
if (!window.requestAnimationFrame) {
requestAnimationFrame = function (fn) {
setTimeout(fn, 17);
};
} // 算法需要的几个变量
var start = 0;
// during根据设置的总时间计算
var during = Math.ceil(options.duration / 17); // 当前动画算法
// 确保首字母大写
options.easing = options.easing.slice(0, 1).toUpperCase() + options.easing.slice(1);
var arrKeyTween = options.easing.split('.');
var fnGetValue; if (arrKeyTween.length == 1) {
fnGetValue = tween[arrKeyTween[0]];
} else if (arrKeyTween.length == 2) {
fnGetValue = tween[arrKeyTween[0]] && tween[arrKeyTween[0]][arrKeyTween[1]];
}
if (isFunction(fnGetValue) == false) {
console.error('没有找到名为"'+ options.easing +'"的动画算法');
return;
} // 运动
var step = function() {
// 当前的运动位置
var value = fnGetValue(start, from, to - from, during); // 时间递增
start++;
// 如果还没有运动到位,继续
if (start <= during) {
options.callback(value);
requestAnimationFrame(step);
} else {
// 动画结束,这里可以插入回调...
options.callback(to, true);
}
};
// 开始执行动画
step();
};

最后

大家好,这里是「 TaoLand 」,这个博客主要用于记录一个菜鸟程序猿的Growth之路。这也是自己第一次做博客,希望和大家多多交流,一起成长!文章将会在下列地址同步更新……

个人博客:www.yangyuetao.cn

小程序:TaoLand

vue-router和锚点冲突问题的更多相关文章

  1. 解决vue路由与锚点冲突

    传统的锚点定位会与vue-router中的路由设置存在冲突,解决方法自定义锚点跳转: html: <ul> <li><a href="" @click ...

  2. 深入浅出的webpack构建工具--webpack4+vue+router项目架构(十四)

    阅读目录 一:vue-router是什么? 二:vue-router的实现原理 三:vue-router使用及代码配置 四:理解vue设置路由导航的两种方法. 五:理解动态路由和命名视图 六:理解嵌套 ...

  3. [Vue 牛刀小试]:第十二章 - 使用 Vue Router 实现 Vue 中的前端路由控制

    一.前言 前端路由是什么?如果你之前从事的是后端的工作,或者虽然有接触前端,但是并没有使用到单页面应用的话,这个概念对你来说还是会很陌生的.那么,为什么会在单页面应用中存在这么一个概念,以及,前端路由 ...

  4. Vue躬行记(8)——Vue Router

    虽然Vue.js未提供路由功能,但是官方推出了Vue Router(即vue-router库),以插件的形式支持.它与Vue.js深度集成,可快速的创建单页应用(Single Page Applica ...

  5. 「vue基础」一篇浅显易懂的 Vue 路由使用指南( Vue Router 上)

    大家好,今天的内容,我将和大家一起聊聊 Vue 路由相关的知识,如果你以前做过服务端相关的开发,那你一定会对程序的URL结构有所了解,我没记错的话也是路由映射的概念,需要进行配置. 其实前端这些框架的 ...

  6. Vue Router详细教程

    1.什么是路由 1.1路由简介 说起路由你想起了什么?路由是一个网络工程里面的术语. 路由(routing)就是通过互联的网络把信息从源地址传输到目的地址的活动. --- 维基百科 额,啥玩意? 没听 ...

  7. Vue 2.0 + Vue Router + Vuex

    用 Vue.js 2.x 与相配套的 Vue Router.Vuex 搭建了一个最基本的后台管理系统的骨架. 当然先要安装 node.js(包括了 npm).vue-cli 项目结构如图所示: ass ...

  8. vue router 只需要这么几步

    <!DOCTYPE html><html lang="en"><head> <meta charset="UTF-8" ...

  9. Vue.js 2.x笔记:路由Vue Router(6)

    1. Vue Router简介与安装 1.1 Vue Router简介 Vue Router 是 Vue.js 官方的路由管理器.它和 Vue.js 的核心深度集成,构建单页面应用. Vue Rout ...

  10. Vue Router学习笔记

    前端的路由:一个地址对应一个组件 Vue Router中文文档 一.路由基本使用 第1步:导入Vue Router: <script src="https://unpkg.com/vu ...

随机推荐

  1. Android移动软件开发总结

    目录 Android实验参考目录 常用知识点总结 服务绑定bind Service ThreadService使用总结 Service用法总结 Broadcast Receiver用法 Intent使 ...

  2. 前端工程化 - gulp

    gulp是什么 gulp就是一个前端的自动化构建工具,在开发过程中很多重复的任务可以使用gulp和gulp插件自动完成.相比于grunt,gulp非常好上手,核心API只有4个,而且还有丰富的插件库. ...

  3. vs_u8前缀

    1.ZC: 个人测试下来,VS2015开始 支持 u8前缀. 2.What's New for Visual C++ in Visual Studio 2015 https://msdn.micros ...

  4. OpenGL超级宝典笔记——遮挡查询 [转]

    目录[-] 遮挡查询之前 包围体 遮挡查询 在一个场景中,如果有有些物体被其他物体遮住了不可见.那么我们就不需要绘制它.在复杂的场景中,这可以减少大量的顶点和像素的处理,大幅度的提高帧率.遮挡查询就是 ...

  5. 依据分辨率区分手机、平板、pc

    /*手机*/ @media screen and (max-width:600px){ #header,#content,#footer{width:400px;} .right,.center{ma ...

  6. AngularJS Toaster

    AngularJS Toaster 是 AngularJS 的扩展,实现了无堵塞消息通知.该项目对 Toastr 进行改造,原来的 Toastr 只支持 jQuery,而 AngularJS Toas ...

  7. 谈谈WPF中的CollectionView与CollectionViewSource

    https://www.cnblogs.com/zhouyinhui/archive/2007/12/07/987076.html

  8. UVA-1612 Guess (贪心)

    题目大意:考试共有三道题,n个人,每个人对每道题的可能得分已知,现在已知考后排名情况,问排名合不合理. 题目分析:贪心.贪心策略:每处理一个排名,都让他的得分尽量高. # include<ios ...

  9. 297. Serialize and Deserialize Binary Tree *HARD*

    Serialization is the process of converting a data structure or object into a sequence of bits so tha ...

  10. restframework api (二)权限

    一 添加权限 (1)API/utils文件夹下新建premission.py文件,代码如下: message是当没有权限时,提示的信息 # utils/permission.py class SVIP ...