Vue进阶(幺叁陆):el-steps实现页面内导航

No Silver Bullet 2020-11-13 07:25:38
vue 进阶 el 叁陆 el-steps


在前端开发过程中,当页面内容分为若干栏目后,通过增加导航栏位定位至相应栏目不失为一种良好的用户体验。

可通过el-steps实现页面内栏目跳转。el-steps基本用法如下:

<el-steps :active="active" finish-status="success">
<el-step title="步骤 1"></el-step>
<el-step title="步骤 2"></el-step>
<el-step title="步骤 3"></el-step>
</el-steps>
<el-button style="margin-top:12px;" @click="next">下一步</el-button>
data:{

active:0
},
methods:{

next(){

if(this.active++ >2){

this.active = 0
}
}
}

重点介绍el-steps的direction、space、active、process-status、align-center属性,截图如下:
在这里插入图片描述

通过查看Element手册,需要定宽的步骤条时,设置space属性即可,单位为px,如果不设置为自适应(若不确定步骤条中每一步骤中内容高度或宽度,可设置为自适应;为使界面美观,可修改el-step属性

.el-step__main {

width: 100%!important;
padding-left: 3%!important;
margin-bottom: 3%!important;
}


el-step属性如下图所示:
在这里插入图片描述

这里重点介绍下如何应用el-steps动态展示所需信息。demo如下:

<div>
<el-steps direction="vertical" :align-center="true" :active="arr.length" finish-status="success">
<el-step :title="item.title" :description="item.description" v-for="(item, index) in arr" :key="index" @click.native="onCLick(index)">
<template slot="description">
<!--表单内容-->
</template>
</el-step>
</el-steps>
</div>
<div>
<transition name="slide-fade">
<div v-if="shownav" class="rightList">
<span class="line_prev"></span>
<ul>
<li v-for="(item, index) in navArr" :key="index" @click="crollTo(index)" :class="{active: active====index}">
<img src="cue.png" v-if="active === index">
</li>
</ul>
<span class="line_next"></span>
</div>
</transition>
</div>

以上只是实现了步骤条内容的动态展示,若需实现根据用户浏览内容定位导航栏目或者根据导航栏目快速定位至相关栏目处,需通过动态监听页面内容实现。


<!--流程流转处理节点导航栏位-->
<div v-if="flowopinoin.length > 4" :class="[flowopinoin.length > 10 ? 'navConst' : 'nav']">
<div :class="[flowopinoin.length > 10 ? 'navStyleConst' : 'navStyle']">
<transition name="slide-fade">
<div v-if="shownav" class="rightList">
<span class="line_prev"></span>
<ul>
<li v-for="(item, index) in flowopinoin" :key="index" @click="scrollTo(index)" :class="{active: active===index}">
<img src="../../../assets/img/cur.png" width="20" v-if="active === index"/>
<span>{
{
item.nodename}}</span>
</li>
</ul>
<span class="line_next"></span>
</div>
</transition>
</div>
<el-button @click="shownav = !shownav" type="primary" :class="[flowopinoin.length > 10 ? 'shownavConst' : 'shownav']">
<i style="margin-left: -6px;" :class="[shownav ? 'el-icon-d-arrow-right': 'el-icon-d-arrow-left']"></i>
</el-button>
</div>
this.$nextTick(() => {

// 监听滚动事件
window.addEventListener('scroll', this.onScroll, false)
})
// 滚动监听器
onScroll () {

// 获取所有锚点元素
const navContents = document.querySelectorAll('.el-steps>.el-step')
// console.log('navContents:', navContents)
// 所有锚点元素的 offsetTop
const offsetTopArr = []
for (let i = 0; i < navContents.length; i++) {

offsetTopArr.push(navContents[i].offsetTop)
}
// navContents.forEach(item => {

// offsetTopArr.push(item.offsetTop)
// })
// 获取当前文档流的 scrollTop
const scrollTop = document.documentElement.scrollTop || document.body.scrollTop
// 定义当前点亮的导航下标
for (let n = 0; n < offsetTopArr.length; n++) {

// 如果 scrollTop 大于等于第n个元素的 offsetTop 则说明 n-1 的内容已经完全不可见
// 那么此时导航索引就应该是n了
// console.log('scrollTop:', scrollTop)
// console.log('offsetTopArr[n]:', offsetTopArr[n])
if (scrollTop >= offsetTopArr[n]) {

this.navIndex = n
}
}
this.active = this.navIndex
},
// 跳转到指定索引的元素
scrollTo (index) {

// 获取目标的 offsetTop
// css选择器是从 1 开始计数,我们是从 0 开始,所以要 +1
const targetOffsetTop = document.querySelector(`.el-steps>.el-step:nth-child(${
index + 1})`).offsetTop
// 获取当前 offsetTop
let scrollTop = document.documentElement.scrollTop || document.body.scrollTop
// 定义一次跳 50 个像素,数字越大跳得越快,但是会有掉帧得感觉,步子迈大了会扯到蛋
const STEP = 50
// 判断是往下滑还是往上滑
// console.log('scrollTop:', scrollTop)
// console.log('targetOffsetTop:', targetOffsetTop)
if (scrollTop >= targetOffsetTop) {

// 往上滑
smoothUp()
} else {

// 往下滑
smoothDown()
}
// 定义往下滑函数
function smoothDown () {

// 如果当前 scrollTop 小于 targetOffsetTop 说明视口还没滑到指定位置
if (scrollTop < targetOffsetTop) {

// 如果和目标相差距离大于等于 STEP 就跳 STEP
// 否则直接跳到目标点,目标是为了防止跳过了。
if (targetOffsetTop - scrollTop >= STEP) {

scrollTop += STEP
} else {

scrollTop = targetOffsetTop
}
document.body.scrollTop = scrollTop
document.documentElement.scrollTop = scrollTop
// 关于 requestAnimationFrame 可以自己查一下,在这种场景下,相比 setInterval 性价比更高
requestAnimationFrame(smoothDown)
}
}
// 定义往上滑函数
function smoothUp () {

if (scrollTop > targetOffsetTop) {

if (scrollTop - targetOffsetTop >= STEP) {

scrollTop -= STEP
} else {

scrollTop = targetOffsetTop
}
document.body.scrollTop = scrollTop
document.documentElement.scrollTop = scrollTop
requestAnimationFrame(smoothUp)
}
}
}
@charset "UTF-8";
.navConst {

position:fixed;
right: 3%;
top:15%;
}
.nav {

position:fixed;
right: 2%;
top:15%;
}
.navStyleConst {

position:fixed;
top:15%;
cursor: pointer;
right: 3%;
width: 7%;
height: auto;
max-height:800px;
overflow: scroll;
overflow-x: hidden;
}
.navStyle {

position:fixed;
top:51%;
cursor: pointer;
right: 2%;
width: 7%;
height: auto;
}
.shownavConst {

position:absolute;
right: -37px;
top: 50%;
width: 10px;
height: 100px;
}
.shownav {

position: fixed;
right:18px;
top: 57%;
}
.shownav2 {

position: fixed;
right:18px;
top: 38%;
}
ul {

list-style: none;
}
.active img {

position: absolute;
left: -26px;
top: 5px;
z-index: 2;
}
.line_prev{

display:inline-block;
width: 1px;
height:20px;
background:#ccc;
position:relative;
left:20px;
bottom:-8px;
}
.line_prev:before{

content: '';
position:absolute;
width:9px;
height:9px;
border-radius:50%;
border:2px solid #EDEDED;
z-index:1;
left:-4px;
top:-8px;
}
.rightList li{

position:relative;
line-height:25px;
cursor:pointer;
padding-bottom:16px;
}
.rightList li span{

padding-left:5px;
border-bottom: 1px solid #e3e5eb;
}
.rightList li:before{

content: '';
position:absolute;
width:9px;
height:9px;
left:-24px;
border-radius:50%;
background: #FFFFFF;
border: 2px solid #EDEDED;
top:20%;
z-index:2;
}
/*.rightList li:after{
*/
/*content: '';*/
/*position:absolute;*/
/*width:9px;*/
/*height:9px;*/
/*border-radius:50%;*/
/*background:#fff;*/
/*left:-2px;*/
/*top:33%;*/
/*z-index:1;*/
/*}*/
.rightList li.active{

font-family: PingFangSC-Medium;
color: #20A0FF;
text-align: left;
line-height: 22px;
}
.rightList ul{

position:relative;
}
.rightList{

background:#fff;
right:10px;
box-shadow:0 0 5px 5px rgba(170,167,167,0.1);
z-index:13;
}
.rightList ul:after{

content:'';
width:1px;
height:100%;
display:inline-block;
background:#ccc;
position:absolute;
top:0;
left:20px;
}
.rightList .line_next{

display:inline-block;
width: 1px;
height:20px;
background:#ccc;
position:relative;
left:20px;
top:-15px;
}
.rightList .line_next:after{

content: '';
position:absolute;
width:9px;
height:9px;
border-radius:50%;
border:2px solid #EDEDED;
z-index:1;
left:-4px;
bottom: -8px;
}
.shownav.el-button.el-button--primary{

height:100px;
padding: 0 5px;
}
.shownav2.el-button.el-button--primary{

height:100px;
padding: 0 5px;
}
.slide-fade-enter-active{

transition: all 0.3s ease;
}
.slide-fade-leave-active{

transition: all 0.5s ease;
}
.slide-fade-enter, .slide-fade-leave-to{

/*transform: translateX(10px);*/
opacity:0;
}

参考资料:

版权声明
本文为[No Silver Bullet]所创,转载请带上原文链接,感谢
https://shq5785.blog.csdn.net/article/details/109001296

  1. [front end -- JavaScript] knowledge point (IV) -- memory leakage in the project (I)
  2. This mechanism in JS
  3. Vue 3.0 source code learning 1 --- rendering process of components
  4. Learning the realization of canvas and simple drawing
  5. gin里获取http请求过来的参数
  6. vue3的新特性
  7. Get the parameters from HTTP request in gin
  8. New features of vue3
  9. vue-cli 引入腾讯地图(最新 api,rocketmq原理面试
  10. Vue 学习笔记(3,免费Java高级工程师学习资源
  11. Vue 学习笔记(2,Java编程视频教程
  12. Vue cli introduces Tencent maps (the latest API, rocketmq)
  13. Vue learning notes (3, free Java senior engineer learning resources)
  14. Vue learning notes (2, Java programming video tutorial)
  15. 【Vue】—props属性
  16. 【Vue】—创建组件
  17. [Vue] - props attribute
  18. [Vue] - create component
  19. 浅谈vue响应式原理及发布订阅模式和观察者模式
  20. On Vue responsive principle, publish subscribe mode and observer mode
  21. 浅谈vue响应式原理及发布订阅模式和观察者模式
  22. On Vue responsive principle, publish subscribe mode and observer mode
  23. Xiaobai can understand it. It only takes 4 steps to solve the problem of Vue keep alive cache component
  24. Publish, subscribe and observer of design patterns
  25. Summary of common content added in ES6 + (II)
  26. No.8 Vue element admin learning (III) vuex learning and login method analysis
  27. Write a mini webpack project construction tool
  28. Shopping cart (front-end static page preparation)
  29. Introduction to the fluent platform
  30. Webpack5 cache
  31. The difference between drop-down box select option and datalist
  32. CSS review (III)
  33. Node.js学习笔记【七】
  34. Node.js learning notes [VII]
  35. Vue Router根据后台数据加载不同的组件(思考-&gt;实现-&gt;不止于实现)
  36. Vue router loads different components according to background data (thinking - & gt; Implementation - & gt; (more than implementation)
  37. 【JQuery框架,Java编程教程视频下载
  38. [jQuery framework, Java programming tutorial video download
  39. Vue Router根据后台数据加载不同的组件(思考-&gt;实现-&gt;不止于实现)
  40. Vue router loads different components according to background data (thinking - & gt; Implementation - & gt; (more than implementation)
  41. 【Vue,阿里P8大佬亲自教你
  42. 【Vue基础知识总结 5,字节跳动算法工程师面试经验
  43. [Vue, Ali P8 teaches you personally
  44. [Vue basic knowledge summary 5. Interview experience of byte beating Algorithm Engineer
  45. 【问题记录】- 谷歌浏览器 Html生成PDF
  46. [problem record] - PDF generated by Google browser HTML
  47. 【问题记录】- 谷歌浏览器 Html生成PDF
  48. [problem record] - PDF generated by Google browser HTML
  49. 【JavaScript】查漏补缺 —数组中reduce()方法
  50. [JavaScript] leak checking and defect filling - reduce() method in array
  51. 【重识 HTML (3),350道Java面试真题分享
  52. 【重识 HTML (2),Java并发编程必会的多线程你竟然还不会
  53. 【重识 HTML (1),二本Java小菜鸟4面字节跳动被秒成渣渣
  54. [re recognize HTML (3) and share 350 real Java interview questions
  55. [re recognize HTML (2). Multithreading is a must for Java Concurrent Programming. How dare you not
  56. [re recognize HTML (1), two Java rookies' 4-sided bytes beat and become slag in seconds
  57. 【重识 HTML ,nginx面试题阿里
  58. 【重识 HTML (4),ELK原来这么简单
  59. [re recognize HTML, nginx interview questions]
  60. [re recognize HTML (4). Elk is so simple