最近做了一个新项目有关于新手引导,这里选择driver.js在vue项目中使用
涉及样式修改,重新演示功能。
github地址: https://github.com/kamranahmedse/driver.js
1、下载安装
yarn add driver.js
2、引入
import Driver from 'driver.js' // 引入driver.js插件
import 'driver.js/dist/driver.min.css' // driver样式
import driverList from './driver' // 引入数据
3、定义
computed: {
driver () {
const driver = new Driver({
animate: true, // 在更改突出显示的元素时设置动画
allowClose: false, // 禁止点击外部关闭
keyboardControl: false, // 禁止键盘控制
stageBackground: '#fff', // 引导对话的背景色
closeBtnText: '关闭', // 关闭文本
overlayClickNext: false, // 蒙层点击是否执行下一步
prevBtnText: '上一步', // 上一步文本
nextBtnText: '下一步', // 下一步按钮标题
doneBtnText: '确定', // 最后一步完成按钮展示文本
})
return driver
},
}
4、driverList数据
export default [
{
element: '#first', // 这是需要高亮的元素
popover: { // 引导展示数据
title: '标题', // 这是标题
description: '<div class="step_two"><div class="step_first">1</div>第一步</div>', // 这里是内容,支持HTML字符串
position: 'bottom-center' // 引导展示位置
}
},
{
element: '#second',
popover: {
title: '标题', // 这是标题
description: '<div class="step_two"><div class="step_first">1</div>第二步</div>', // 这里是内容,支持HTML字符串
position: 'bottom-center' // 引导展示位置,支持 top, left, bottom, right, center
}
},
{
element: '#third',
popover: {
title: '标题', // 这是标题
description: '<div class="step_two"><div class="step_first">1</div>第三步</div>', // 这里是内容,支持HTML字符串
position: 'bottom-center' // 引导展示位置,支持 top, left, bottom, right, center
}
},
{
element: '#fouth',
popover: {
title: '标题', // 这是标题
description: '<div class="step_two"><div class="step_first">1</div>第四步</div>', // 这里是内容,支持HTML字符串
position: 'bottom-center' // 引导展示位置,支持 top, left, bottom, right, center
}
},
]
5、页面元素
<div class="slide">
<div class="slide_part" id='first'>
<i class="el-icon-user-solid" style='font-size: 30px'></i>
<p>First</p>
</div>
<div class="slide_part" id='second'>
<i class="el-icon-s-tools" style='font-size: 30px'></i>
<p>Second</p>
</div>
<div class="slide_part" id='third'>
<i class="el-icon-picture" style='font-size: 30px'></i>
<p>Third</p>
</div>
<div class="slide_part" id='fouth'>
<i class="el-icon-s-cooperation" style='font-size: 30px'></i>
<p>Fourth</p>
</div>
</div>
6、开始新手引导
this.driver.defineSteps(driverList)
this.driver.start()
7、修改样式
下面是UI设计新手引导样式,可见没有标题,只有下一步按钮,下面我们修改样式,隐藏标题,关闭按钮,上一步按钮
检查元素发现,新手引导元素是建立在body下面,所以样式不能写在当前vue文件中的scoped中
<style lang="less">
div#driver-popover-item{ // 最外面样式
border-radius: 0;
padding: 0;
// width: 260px;
// height: 160px;
div.driver-popover-title {
display: none; // 隐藏标题
}
div.driver-popover-footer{
display: flex!important;
justify-content: center;
margin-bottom: 20px;
button.driver-close-btn {
display: none; // 隐藏关闭按钮
}
.driver-btn-group{
display: flex;
justify-content: center;
button.driver-prev-btn{
display: none !important; // 隐藏上一步按钮
}
button.driver-next-btn {
width: 74px;
height: 34px;
background: #345eef;
color: #fff;
text-shadow: none;
}
}
}
.driver-popover-description{ // 内容样式设置
font-size: 14px;
margin: 50px auto 29px;
.step_two {
display: flex;
align-items: center;
justify-content: center;
.step_first{
width: 20px;
height: 20px;
background: #345eef;
border-radius: 50%;
color: #fff;
line-height: 20px;
margin-right: 5px;
text-align: center;
}
}
}
}
</style>
8、结尾重新演示功能
到最后一步发现一个重新演示的功能,
onReset: (e) =>{
console.log('onReset', e)
}, // 元素高亮,凸显关闭时
onNext: (e) => {
console.log('onNext', e)
}, // 下一步需执行
onPrevious: (e) => {
console.log('onPrevious', e)
}, // 上一步需执行
本来想使用onReset事件去实现这一功能,但是发现最后一步点击关闭和确定都会执行onReset事件,由此不能把重新演示当成关闭按钮去执行,所以接下来使用onPrevious去实现。
8.1、修改driverList.js文件
只需要修改最后一个对象就可以了
{
element: '#fouth',
popover: {
title: '标题', // 这是标题
className: 'step_fourth', // 自定义类名设置
description: '<div class="step_two"><div class="step_first">1</div>第四步</div>', // 这里是内容,支持HTML字符串
position: 'bottom-center', // 引导展示位置,支持 top, left, bottom, right, center
prevBtnText: '重新演示'
}
},
8.2、修改最后一步样式
看driver的github就会发现每一个都可以设置指定的类名,我们设置自定义类名去修改最后一步引导样式
div#driver-popover-item.step_fourth div.driver-popover-footer {
justify-content: space-between;
width: 100%;
span.driver-btn-group{
width: 100%;
display: flex;
justify-content: space-around;
button.driver-prev-btn{
display: inline-block !important;
width: 74px;
height: 34px;
background: #dfdfdf;
color: #333;
font-size: 12px;
padding: 0;
border-radius: 0;
border: none;
}
}
}
8.3实现代码
onPrevious: (e) => {
this.driver.start(1); //可以指定新手引导从第几步开始
},
9、完整代码
driver.vue
<template>
<div class="driver">
<div class="dirver_main">
<div class="slide">
<div class="slide_part" id='first'>
<i class="el-icon-user-solid" style='font-size: 30px'></i>
<p>First</p>
</div>
<div class="slide_part" id='second'>
<i class="el-icon-s-tools" style='font-size: 30px'></i>
<p>Second</p>
</div>
<div class="slide_part" id='third'>
<i class="el-icon-picture" style='font-size: 30px'></i>
<p>Third</p>
</div>
<div class="slide_part" id='fouth'>
<i class="el-icon-s-cooperation" style='font-size: 30px'></i>
<p>Fourth</p>
</div>
</div>
</div>
</div>
</template>
<script>
import Driver from 'driver.js'
import 'driver.js/dist/driver.min.css' // driver样式
import driverList from './driver'
export default {
computed: {
driver () {
const driver = new Driver({
animate: true, // 在更改突出显示的元素时设置动画
allowClose: false, // 禁止点击外部关闭
keyboardControl: false, // 禁止键盘控制
stageBackground: '#fff', // 引导对话的背景色
closeBtnText: '关闭', // 关闭文本
overlayClickNext: false, // 蒙层点击是否执行下一步
prevBtnText: '上一步', // 上一步文本
nextBtnText: '下一步', // 下一步按钮标题
doneBtnText: '确定', // 最后一步完成按钮展示文本
onReset: (e) =>{
console.log('onReset', e)
}, // 元素高亮,凸显关闭时
onNext: (e) => {
}, // Called when moving to next step on any step 下一步需执行
onPrevious: (e) => {
this.driver.start(1); //可以指定新手引导从第几步开始
}, // Called when moving to next step on any step 上一步需执行
})
return driver
},
},
mounted () {
this.driver.defineSteps(driverList)
this.driver.start()
}
}
</script>
<style lang="less" scoped>
@headerHeight: 68px;
@color: #333;
.driver {
width: 100%;
height: 100%;
background-color: #eee;
.dirver_main{
width: 100%;
height: calc(100% - @headerHeight);
.slide{
width: 100%;
height: 88px;
background: #fff;
display: flex;
justify-content: space-around;
align-items: center;
.slide_part{
font-size: 14px;
font-weight: 400;
color: #333;
display: flex;
align-items: center;
justify-content: center;
flex-direction: column;
}
}
.main {
.btn {
width: 200px;
height: 48px;
background: #345eef;
border-radius: 0;
border: none;
font-size: 16px;
}
}
}
}
</style>
<style lang="less">
div#driver-popover-item{ // 最外面样式
border-radius: 0;
padding: 0;
// width: 260px;
// height: 160px;
div.driver-popover-title {
display: none; // 隐藏标题
}
div.driver-popover-footer{
display: flex!important;
justify-content: center;
margin-bottom: 20px;
button.driver-close-btn {
display: none; // 隐藏关闭按钮
}
.driver-btn-group{
display: flex;
justify-content: center;
button.driver-prev-btn{
display: none !important; // 隐藏上一步按钮
}
button.driver-next-btn {
width: 74px;
height: 34px;
background: #345eef;
color: #fff;
text-shadow: none;
border: none;
border-radius: 0;
}
}
}
.driver-popover-description{ // 内容样式设置
font-size: 14px;
margin: 50px auto 29px;
color: #333;
.step_two {
display: flex;
align-items: center;
justify-content: center;
.step_first{
width: 20px;
height: 20px;
background: #345eef;
border-radius: 50%;
color: #fff;
line-height: 20px;
margin-right: 5px;
text-align: center;
}
}
}
}
div#driver-popover-item.step_fourth div.driver-popover-footer {
justify-content: space-between;
width: 100%;
span.driver-btn-group{
width: 100%;
display: flex;
justify-content: space-around;
button.driver-prev-btn{
display: inline-block !important;
width: 74px;
height: 34px;
background: #dfdfdf;
color: #333;
font-size: 12px;
padding: 0;
border-radius: 0;
border: none;
}
}
}
</style>
driverList.js完整代码
export default [
{
element: '#first', // 这是需要高亮的元素
popover: { // 引导展示数据
title: '标题', // 这是标题
description: '<div class="step_two"><div class="step_first">1</div>第一步</div>', // 这里是内容,支持HTML字符串
position: 'bottom-center' // 引导展示位置
}
},
{
element: '#second',
popover: {
title: '标题', // 这是标题
description: '<div class="step_two"><div class="step_first">1</div>第二步</div>', // 这里是内容,支持HTML字符串
position: 'bottom-center' // 引导展示位置,支持 top, left, bottom, right, center
}
},
{
element: '#third',
popover: {
title: '标题', // 这是标题
description: '<div class="step_two"><div class="step_first">1</div>第三步</div>', // 这里是内容,支持HTML字符串
position: 'bottom-center' // 引导展示位置,支持 top, left, bottom, right, center
}
},
{
element: '#fouth',
popover: {
title: '标题', // 这是标题
className: 'step_fourth', // 自定义类名设置
description: '<div class="step_two"><div class="step_first">1</div>第四步</div>', // 这里是内容,支持HTML字符串
position: 'bottom-center', // 引导展示位置,支持 top, left, bottom, right, center
prevBtnText: '重新演示'
}
},
]