通常,在产品发布新版本或者有新功能上线时,都会开发一个新手引导功能来引导用户了解应用的功能。在前端开发中,如何快速地开发新手引导功能呢,下面介绍几个开箱即用的新手引导组件库。
Intro.js是一个使用广泛的产品引导库,在Github上拥有超过21.7k的Star。具有以下特点:
和其他组件库的使用流程一样,需要先在项目中使用以下命令来安装 Intro.js库。
npm install intro.js - save
按照如下的步骤开发引导功能:
接着,调用以下JavaScript 函数,启动Intro.js。
introJs().start();
然后,使用以下附加参数在特定元素或类上调用 Intro.js启动。
introJs(".introduction-farm").start();
项目链接:https://github.com/usablica/intro.js
Shepherd是一个前端JavaScript引导库,在Github上拥有10.8kGitHub Star,支持在 React、Vue、Angular 等多个前端框架中开箱即用,其具有以下特点:
使用Shepherd之前,我们可以使用以下命令来安装 shepherd.js。
npm install shepherd.js -save
npm install react-shepherd --save
npm install vue-shepherd --save
npm install angular-shepherd --save
安装完成之后,我们需要使用shepherd组件来嵌套引导页面。
import React, { Component, useContext } from 'react'
import { ShepherdTour, ShepherdTourContext } from 'react-shepherd'
import newSteps from './steps'const tourOptions = {defaultStepOptions: {cancelIcon: {enabled: true}},useModalOverlay: true
};function Button() {const tour = useContext(ShepherdTourContext);return ();
}class App extends Component {render() {return ( );}
}
为了支持React、Vue、Angular等不同的框架,Shepherd提供了不同的地址:
shepherd:https://github.com/shipshapecode/shepherd
react-shepherd:https://github.com/shipshapecode/react-shepherd
vue-shepherd:https://github.com/shipshapecode/vue-shepherd
angular-shepherd:https://github.com/shipshapecode/angular-shepherd
React Joyride 是一款用于向用户介绍新功能的React项目新手引导库,在GitHub上拥有超过5.1k的Star,具有以下特点:
在项目中使用 react-joyride之前,需要使用以下命令来安装 react-joyride。
npm i react-joyride
然后,在引导页面使用以下方式来在 React 中使用 react-joyride。
import Joyride from 'react-joyride';export class App extends React.Component {state = {steps: [{target: '.my-first-step',content: 'This is my awesome feature!',},{target: '.my-other-step',content: 'This another awesome feature!',},...]};render () {const { steps } = this.state;return ( ...);}
}
项目链接:https://github.com/gilbarbara/react-joyride
Vue Tour是一个轻巧、简单且可自定义的新手指引组件,专门为Vue.js 定制,它提供了一种快速简便的方法来指导用户使用应用,目前它在 Github 上拥有 2.12k Star。
首先,我们需要通过以下命令来安装Vue Tour。
npm install vue-tour
然后,在应用入口文件(通常是 main.js)中导入插件,并在 Vue 中注册它,可以添加默认提供的样式或根据自己的喜好自定义它们。
import Vue from 'vue'
import App from './App.vue'
import VueTour from 'vue-tour'require('vue-tour/dist/vue-tour.css')Vue.use(VueTour)new Vue({render: h => h(App)
}).$mount('#app')
最后,再将 v-tour 组件放入模板中的任何位置(通常在 App.vue 中),并向其传递一系列步骤,每个步骤的 target 属性可以将应用的任何组件中的 DOM 元素作为 target。
A DOM element on your page. The first step will pop on this element because its ID is 'v-step-0'.A DOM element on your page. The second step will pop on this element because its ID is 'v-step-1'.A DOM element on your page. The third and final step will pop on this element because its ID is 'v-step-2'.
项目链接:https://github.com/pulsardev/vue-tour
Reactour 是一个广泛使用的React 应用引导库,在GitHub上拥有3.2K的Star,它提供了一种简单的方式来引导用户浏览网站和应用。首先,需要通过以下命令来安装 reactour。
npm i -S @reactour/tour
安装完成之后,在应用的根组件添加 TourProvider,传递元素的步骤以在浏览期间突出显示。
import { TourProvider } from '@reactour/tour'ReactDOM.render( ,document.getElementById('root')
)const steps = [{selector: '.first-step',content: 'This is my first Step',},// ...
]
然后,在应用树中的某个地方,使用 useTour hook 来控制 Tour。
import { useTour } from '@reactour/tour'function App() {const { setIsOpen } = useTour()return (<>Lorem ipsum dolor sit amet, consectetur adipiscing elit. Praesent atfinibus nulla, quis varius justo. Vestibulum lorem lorem, viverra portametus nec, porta luctus orci
>)
}
项目链接:https://github.com/elrumordelaluz/reactour
上一篇:操作系统