intro.js分步向导插件使用方法

前言

这里只介绍通过JSON配置的introJs用法

介绍

Intro.js 一个引导用户的js/css库, 没有任何依赖.

安装

npm install intro.js --save

Doc

文档地址: https://introjs.com/docs/

使用

页面引入

  import intro from 'intro.js';
  import 'intro.js/minified/introjs.min.css';

html

<template>
    <a-button type="primary" @click="handleStart">开始</a-button>
</template>

js

 function handleStart() {
        intro()
          .setOptions({
            steps: [
              {
                title: 'Welcome',
                intro: 'Hello World! 👋',
              },
              {
                title: 'Collapse Button',
                element: document.querySelector(`.${prefixVar}-layout-header-trigger`)!,
                intro: 'This is the menu collapse button.',
              },
              {
                title: 'User Action',
                element: document.querySelector(`.${prefixVar}-layout-header-action`)!,
                intro: 'This is the user function area.',
              },
            ],
          })
          .start();
      }

效果如下:
在这里插入图片描述

setOptions来设置当前实例,核心的部分就是设置steps,这里包含了所有引导的步骤,比较常用的属性如下:

interface StepsObj {
  // 标题
  title?: string;
  // 简介
  intro?: string;
  // 高亮的DOM元素
  element?: any;
  // 提示框位置
  position?: string;
}

setOptions除了steps属性外,还有其他属性可以配置提示框,这里提供部分属性以供参考:

 // 禁止元素互动(被引导的元素不能点击)
  disableInteraction: true,
  // 下一步按钮的名称
  nextLabel: '下一个',
  // 上一步按钮的名称
  prevLabel: '返回',
  // 跳过按钮的名称
  skipLabel: '跳过引导',
  // 结束按钮的名称
  doneLabel: '完成',
  // 为 intro 指定类名
  tooltipClass: 'toolTip',
  // 是否允许点击空白处退出
  exitOnOverlayClick: true,
  // 是否显示轮播点
  showBullets: true,
  // 是否使用进度条
  showProgress: true,
  // 显示步数  eg: 1/5
  showStepNumbers: true,
  // 是否允许键盘来操作
  keyboardNavigation: true,

完整属性移步官方文档: https://introjs.com/docs/intro/options

动态的steps

实际使用时遇到权限问题,不同用户权限不同,看到的页面也有所不同,从而引导的元素也会有差别,如果写死steps,会导致某些元素找不到而报错,引导也不能进行。一开始想的是根据权限进行判断来动态增减steps,但是权限实在复杂,而我们要做的其实很简单,找不到的元素就不引导!

配置文件中:

export function stepsOfProduct() {
  const stepsList = [
    {
      title: '查询',
      element: document.getElementsByClassName('search-box')[0].getElementsByTagName('BUTTON')[0],
      intro: '填写查询条件后,进行查询操作',
    },
    {
      title: '重置',
      element: document.getElementsByClassName('search-box')[0].getElementsByTagName('BUTTON')[1],
      intro: '清空查询条件',
    },
  ];
  // 权限
  // 判断DOM是否存在, ??的意思是不存在时设为'' 不会引起报错
  const addApplyBtnDOM = document.getElementsByClassName('add-apply')[0] ?? '';
  addApplyBtnDOM &&
    stepsList.push({
      title: '添加申请人',
      element: addApplyBtnDOM,
      intro: '新增申请人',
    });
  return stepsList;
}

前面的配置进行修改:

 function handleStart() {
        intro()
          .setOptions({
            steps: stepsOfProduct(),
          })
          .start();
      }

这样我们可以对页面所有有权限的元素进行动态引导

提示信息中使用icon

有时我们可能会需要这样的效果:
在这里插入图片描述
事实上,steps数组对象中的intro除了是文本之外,还可以是HTML字符串

  {
    title: 'Farewell!',
    element: document.querySelector('.card__image'),
    intro: '<img src="https://images.unsplash.com/photo-1608096299210-db7e38487075?ixid=MXwxMjA3fDB8MHxwaG90by1wYWdlfHx8fGVufDB8fHw%3D&ixlib=rb-1.2.1&auto=format&fit=crop&w=400&q=80" />'
  }]

效果:
在这里插入图片描述
对于icon我们可能经常使用的是组件来完成,但是icon组件的本质还是渲染一个SVG,浏览器中通过shift+ ctrl + c点击渲染的图标找到对应的svg复制粘贴即可
在这里插入图片描述

{
      title: '填写状态',
      element: fillStatusDOM,
      intro:
        '<div><svg width="1em" height="1em" aria-hidden="true" viewBox="64 64 896 896" focusable="false"><path fill="#55d187" d="M512 64C264.6 64 64 264.6 64 512s200.6 448 448 448 448-200.6 448-448S759.4 64 512 64zm193.5 301.7l-210.6 292a31.8 31.8 0 01-51.7 0L318.5 484.9c-3.8-5.3 0-12.7 6.5-12.7h46.9c10.2 0 19.9 4.9 25.9 13.3l71.2 98.8 157.2-218c6-8.3 15.6-13.3 25.9-13.3H699c6.5 0 10.3 7.4 6.5 12.7z"></path></svg>代表填写完整,<svg width="1em" height="1em" fill="currentColor" aria-hidden="true" viewBox="64 64 896 896" focusable="false"><path fill="#efbd47" d="M512 64C264.6 64 64 264.6 64 512s200.6 448 448 448 448-200.6 448-448S759.4 64 512 64zm-32 232c0-4.4 3.6-8 8-8h48c4.4 0 8 3.6 8 8v272c0 4.4-3.6 8-8 8h-48c-4.4 0-8-3.6-8-8V296zm32 440a48.01 48.01 0 010-96 48.01 48.01 0 010 96z"></path></svg>代表存在未填项</div>',
    })

以上!

  • 4
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Angular Intro.js 是一个 AngularJS 模块,用于集成 Intro.js 库,以提供网站或应用程序的新手引导和演示。下面是使用步骤: 1. 安装 Angular Intro.js:通过 npm 安装 Angular Intro.js 模块。 ``` npm install angular-intro.js --save ``` 2. 引入 Intro.js 库:在项目中引入 Intro.js 库,可以通过 npm 安装或直接使用 CDN 引入。 ``` <script src="https://cdnjs.cloudflare.com/ajax/libs/intro.js/2.9.3/intro.min.js"></script> ``` 3. 引入 Angular Intro.js 模块:在 AngularJS 应用程序中引入 Angular Intro.js 模块。 ``` angular.module('myApp', ['angular-intro']); ``` 4. 在 HTML 中使用:在 HTML 中使用指令来添加新手引导和演示。 ``` <div introjs intro-options="options"> <p>这是一个新手引导示例</p> <button ng-click="showStep2()">下一步</button> </div> ``` 上面的代码中,`introjs` 指令告诉 Angular Intro.js 应该在这个元素上启用引导,`intro-options` 属性包含 Intro.js 配置选项,`showStep2()` 是一个 AngularJS 控制器中的函数,用于显示下一个步骤。 5. 在控制器中配置选项和方法:在 AngularJS 控制器中配置 Intro.js 选项和方法。 ``` angular.module('myApp').controller('myCtrl', function($scope) { $scope.options = { steps: [ { element: document.querySelector('#step1'), intro: '这是第一步' }, { element: document.querySelector('#step2'), intro: '这是第二步' } ] }; $scope.showStep2 = function() { introJs().goToStep(2).start(); }; }); ``` 上面的代码中,`steps` 选项包含 Intro.js 步骤对象,每个步骤是一个包含 `element` 和 `intro` 属性的对象。`showStep2()` 函数使用 Intro.js 方法来显示第二步。 以上是 Angular Intro.js 的简单使用方法,可以根据需要进行配置和扩展。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值