选择工具

经过查找试用对比,发现直接使用shepherd比较能满足个人的需求。

安装使用

  • 安装 npm install shepherd.js --save
  • 代码如下,使用的
<script>
import { getUserInfo, updateInfo } from '@/api/system/user.js'
import Shepherd from 'shepherd.js' 
import { offset, flip } from '@floating-ui/dom' // offset,flip 需要另外引入才能使用
import 'shepherd.js/dist/css/shepherd.css'
export default {
  name: 'ShepherdVue',
  data() {
    return {
      defaultConfig: {
        // 是否显示黑色遮罩层
        useModalOverlay: true,
        // 键盘按钮控制步骤
        keyboardNavigation: true,
        defaultStepOptions: {
          // 显示关闭按钮
          cancelIcon: {
            enabled: false
          },
          classes: 'custom',
          scrollTo: { behavior: 'smooth', block: 'center' },
          // 高亮元素四周要填充的空白像素
          modalOverlayOpeningPadding: 0,
          // 空白像素的圆角
          modalOverlayOpeningRadius: 10,
          buttons: [{
            action() {
              return this.back()
            },
            text: '上一步'
          }, {
            action() {
              return this.next()
            },
            text: '下一步'
          }],
          floatingUIOptions: {
            placement: 'auto', // 自动选择最佳位置
            // strategy: 'fixed', // 使用固定定位
            middleware: [
              offset(12),
              flip({
                fallbackPlacements: ['top', 'bottom', 'left', 'right'] // 启用自动翻转
              })
            ],
          },
        },
      },
      step: 0,
      detail: {},
    }
  },
  mounted() {
    this.getUserInfo()
  },
  methods: {
    handleChange(val) {
      console.log(val)
      this.detail.webNavigation = val
      updateInfo({ webNavigation: val })
    },
    async getUserInfo() {
      const res = await getUserInfo()
      this.detail = res.data.data
      this.step = res.data.data.webNavigation | 0
      if (this.step < 2) this.show(this.step)
      // this.show(this.step)
    },
    show(step) {
      this.$nextTick(() => {
        const driver = new Shepherd.Tour(this.defaultConfig)
        driver.addSteps([{
          canClickTarget: false,
          attachTo: {
            element: '.menu-wrapper .step1',
            on: 'bottom',
            scrollTo: true
          },
          title: ' ',
          classes: 'custom custom0',
          text: '在<网站素材管理-名片素材管理>可完善小程序端名片页的公司介绍。',
          buttons: [{
            action: () => {
              this.handleChange(1)
              return driver.next()
            },
            text: '下一步'
          }]
        }, {
          canClickTarget: false,
          attachTo: {
            element: '.menu-wrapper .step2',
            on: 'bottom'
          },
          title: ' ',
          classes: 'custom custom1',
          text: '在<内容管理>可上传企业产品、标杆案例和解决方案。',
          buttons: [{
          //   action: () => {
          //     this.handleChange(0)
          //     return driver.back()
          //   },
          //   text: '上一步'
          // }, {
            action: () => {
              this.handleChange(2)
              return driver.next()
            },
            text: '下一步'
          }
          ],
        }, {
          attachTo: {
            element: '.top-bar__item.step3',
            on: 'bottom'
          },
          title: ' ',
          classes: 'custom custom2',
          text: '完成企业主体认证,我们将为您免费提供企业功能,请尽快完成。',
          buttons: [{
          //   action: () => {
          //     this.handleChange(1)
          //     return driver.back()
          //   },
          //   text: '上一步'
          // }, {
            action: () => {
              this.handleChange(3)
              return driver.cancel()
            },
            text: '知道了'
          }, {
            action: () => {
              this.handleChange(3)
              this.$nextTick(() => {
                this.$router.push('/enterprise-authentication')
              })
              return driver.cancel()
            },
            text: '立即认证'
          }]
        }])
        driver.start()
        driver.show(step)
      })
    }
  },
}
</script>
<style lang="scss">
.custom {
  color: #fff;
  background: #1d61f4;
  border-radius: 10px;
  .shepherd-content {
    .shepherd-header {
      background-color: #1d61f4;
      background-size: 100%;
      height: 100px;
      margin: 20px;
    }
    .shepherd-text {
      color: #fff;
      margin: 20px;
    }
    .shepherd-footer {
      .shepherd-button {
        background: #fff;
        color:#1d61f4;
      }
      .shepherd-button:not(:disabled):hover {
        background: #d7dfea;
        color:#1d61f4;
      }
    }
  }
  .shepherd-arrow::before {
    background: #1d61f4 !important;
  }
}
.custom0 {
  .shepherd-content .shepherd-header {
    background-image: url('~@/assets/img/shepherd/step0.png');
  }
}
.custom1 {
  .shepherd-content .shepherd-header {
    background-image: url('~@/assets/img/shepherd/step1.png');
  }
}
.custom2 {
  .shepherd-content .shepherd-header {
    background-image: url('~@/assets/img/shepherd/step2.png');
  }
}
</style>

参考链接

Last Updated:
Contributors: haizhiguang