新手引导功能能够提供用户友好的导航和操作指导,帮助用户快速熟悉应用程序或网站的界面和功能,并提供必要的提示和帮助,以提升用户体验和应用程序的可用性。
Intro.js:
Intro.js 是一个流行的开源 JavaScript 库,提供了丰富的功能来创建交互式的网页新手引导。它支持高亮显示特定元素、添加提示文本和步骤导航等功能,并提供灵活的配置选项,内置了好几种主题可以使用。商用收费,可以去官网了解下(Lifetime license. Once-off payment.一生的许可证。一旦付款。基础版本一个项目9.99刀等服务)。
/** * 堆代码 duidaima.com * 引入依赖 js、css * <link rel="stylesheet" href="https://unpkg.com/intro.js/introjs.css"/> * <script src="https://unpkg.com/intro.js/intro.js"></script> * */  introJs().setOptions({ steps: [{ intro: "Hello world!" }, { element: document.querySelector('#login'), intro: "Click here to login!" }] }).start();
Shepherd:
Shepherd 是一个基于 JavaScript 的插件,专注于为网页提供新手引导功能。它提供了易于使用的 API,允许开发人员定义引导序列、高亮显示 DOM 元素、添加提示和按钮等。
/** * 引入依赖 js、css * <link rel="stylesheet" href="shepherd.js/dist/css/shepherd.css"/> * <script src="shepherd.js/dist/js/shepherd.min.js"></script> * */ const tour = new Shepherd.Tour({ defaultStepOptions: { cancelIcon: { enabled: true }, classes: 'class-1 class-2', scrollTo: { behavior: 'smooth', block: 'center' } } }); tour.addStep({ title: 'Creating a Shepherd Tour', text: `Creating a Shepherd tour is easy. too!\ Just create a \`Tour\` instance, and add as many steps as you want.`, attachTo: { element: '.hero-example', on: 'bottom' }, buttons: [ { action() { return this.back(); }, classes: 'shepherd-button-secondary', text: 'Back' }, { action() { return this.next(); }, text: 'Next' } ], id: 'creating' }); tour.start();
Driver.js:
Driver.js 是一个轻量级的、无依赖的 JavaScript 库,用于实现网页的新手引导。它具有简单的API,支持高亮显示元素、添加提示文本、自动导航等功能,同时提供了自定义主题的能力。
const driver = new Driver(); // Define the steps for introduction driver.defineSteps([ { element: '#first-element-introduction', popover: { className: 'first-step-popover-class', title: 'Title on Popover', description: 'Body of the popover', position: 'left' } }, { element: '#second-element-introduction', popover: { title: 'Title on Popover', description: 'Body of the popover', position: 'top' } }, { element: '#third-element-introduction', popover: { title: 'Title on Popover', description: 'Body of the popover', position: 'right' } }, ]); // Start the introduction driver.start();怎么实现新手引导
class CustomDriver { currentStep = 0; steps = []; constructor() { } defineSteps(steps = []) { // 存放一下 steps this.steps = steps; } setDomZIndex() { } scrollToCenter() { } setDomBg() { } setPopover() { } start() { // 开始 // 1. 新建遮罩层dom插入body // 2. 新建白色底块 // 3. 新建popover 插入body this.next(); } next() { //1. 给当前引导dom设置定位加权重; this.setDomZIndex(); //2. 使用`getBoundingClientRect()`获取到需要引导得dom得位置信息 x、y、top、left、right、bottom等信息 //3. 需要计算是否有滚动轴,把中焦点定位到当前屏幕得中间 this.scrollToCenter(); //4. 设置白色底块大小和位置 this.setDomBg(); //5. 动态设置popover内信息 this.setPopover(); this.currentStep++; } prev() { this.currentStep--; if(this.currentStep < 0) return; this.setDomZIndex(); this.scrollToCenter(); this.setDomBg(); this.setPopover(); } done() { // 删除创建得dom、popover等定位相关属性 } } const customDriver = new CustomDriver(); customDriver.defineSteps([ { element: '#first-element-introduction', popover: { className: 'first-step-popover-class', title: 'Title on Popover', description: 'Body of the popover', position: 'left' } },]); // Start the introduction customDriver.start();给白色高亮块设置过渡值就能达到平移效果了,还需要再考虑下一个点就是有些出现滚动条并不是再body,他可能再某个块div里面我们需要先把这个元素可见,那就是一直以当前元素递归到body设置到元素可见。 以上就是一个简单得实现方案。
第二种使用一个css属性box-shadow,设置原引导得dombox-shadow: 0 0 0 5000px rgba(0, 0, 0, 0.5);,好处就是不用设置元素得定位属性、遮罩层以及背景高亮色了,可以直接设置box-shadow 达到效果,其他还是要自己写popover等。可以参考以下一篇文章利用这个css属性,你也能轻松实现一个新手引导库。