当前位置:首页 > VUE

vue 实现引导页面

2026-02-18 22:08:00VUE

Vue 实现引导页面的方法

使用 vue-tour 插件

安装 vue-tour 插件:

npm install vue-tour

在 main.js 中引入并注册:

import Vue from 'vue'
import VueTour from 'vue-tour'

Vue.use(VueTour)
require('vue-tour/dist/vue-tour.css')

在组件中使用:

vue 实现引导页面

<template>
  <div>
    <v-tour name="myTour" :steps="steps"></v-tour>
    <button @click="$tours['myTour'].start()">Start Tour</button>
  </div>
</template>

<script>
export default {
  data() {
    return {
      steps: [
        {
          target: '#element1',
          content: 'This is the first step'
        },
        {
          target: '#element2',
          content: 'This is the second step'
        }
      ]
    }
  }
}
</script>

使用 driver.js 库

安装 driver.js:

npm install driver.js

在组件中使用:

vue 实现引导页面

<template>
  <div>
    <button @click="startTour">Start Tour</button>
  </div>
</template>

<script>
import Driver from 'driver.js'
import 'driver.js/dist/driver.min.css'

export default {
  methods: {
    startTour() {
      const driver = new Driver()
      driver.defineSteps([
        {
          element: '#element1',
          popover: {
            title: 'Title',
            description: 'Description',
            position: 'bottom'
          }
        },
        {
          element: '#element2',
          popover: {
            title: 'Title',
            description: 'Description',
            position: 'top'
          }
        }
      ])
      driver.start()
    }
  }
}
</script>

自定义实现引导页面

创建引导组件:

<template>
  <div v-if="isActive" class="tour-overlay">
    <div class="tour-step" :style="stepStyle">
      <div class="tour-content">
        <h3>{{ currentStep.title }}</h3>
        <p>{{ currentStep.content }}</p>
        <button @click="nextStep">Next</button>
      </div>
    </div>
  </div>
</template>

<script>
export default {
  props: {
    steps: Array,
    isActive: Boolean
  },
  data() {
    return {
      currentIndex: 0
    }
  },
  computed: {
    currentStep() {
      return this.steps[this.currentIndex]
    },
    stepStyle() {
      const element = document.querySelector(this.currentStep.target)
      if (element) {
        const rect = element.getBoundingClientRect()
        return {
          top: `${rect.top}px`,
          left: `${rect.left}px`,
          width: `${rect.width}px`,
          height: `${rect.height}px`
        }
      }
      return {}
    }
  },
  methods: {
    nextStep() {
      if (this.currentIndex < this.steps.length - 1) {
        this.currentIndex++
      } else {
        this.$emit('complete')
      }
    }
  }
}
</script>

<style>
.tour-overlay {
  position: fixed;
  top: 0;
  left: 0;
  right: 0;
  bottom: 0;
  background-color: rgba(0, 0, 0, 0.5);
  z-index: 9999;
}
.tour-step {
  position: absolute;
  box-shadow: 0 0 0 9999px rgba(0, 0, 0, 0.5);
}
.tour-content {
  position: absolute;
  background: white;
  padding: 20px;
  border-radius: 5px;
}
</style>

在父组件中使用:

<template>
  <div>
    <Tour :steps="steps" :isActive="isTourActive" @complete="isTourActive = false" />
    <button @click="isTourActive = true">Start Tour</button>
  </div>
</template>

<script>
import Tour from './Tour.vue'

export default {
  components: { Tour },
  data() {
    return {
      isTourActive: false,
      steps: [
        {
          target: '#element1',
          title: 'Step 1',
          content: 'This is the first step'
        },
        {
          target: '#element2',
          title: 'Step 2',
          content: 'This is the second step'
        }
      ]
    }
  }
}
</script>

注意事项

确保引导步骤中的目标元素在 DOM 中存在并且可见 考虑移动端响应式设计 提供跳过或关闭引导的选项 引导步骤不宜过多,保持简洁明了 可以在 localStorage 中存储用户是否已完成引导的状态

标签: 页面vue
分享给朋友:

相关文章

vue实现添加用户

vue实现添加用户

Vue 实现添加用户功能 数据绑定与表单设计 在 Vue 中实现添加用户功能,首先需要设计一个表单,用于收集用户输入的数据。通过 v-model 实现双向数据绑定,确保表单数据与 Vue 实例中的数据…

vue 组件实现

vue 组件实现

Vue 组件实现方法 单文件组件 (SFC) 使用 .vue 文件格式,包含模板、脚本和样式三部分: <template> <div class="example">{{…

实现vue datepicker

实现vue datepicker

安装依赖 使用 vue-datepicker 或第三方库如 v-calendar 或 vue2-datepicker。以 vue2-datepicker 为例: npm install vue2…

vue 实现流程

vue 实现流程

Vue 实现流程 Vue.js 是一个渐进式 JavaScript 框架,用于构建用户界面。以下是 Vue 实现的基本流程: 安装 Vue.js 通过 CDN 引入或使用 npm/yarn 安装:…

vue环境实现

vue环境实现

Vue 环境搭建 安装 Node.js 和 npm Node.js 是运行 Vue 的基础环境,npm 是包管理工具。从 Node.js 官网 下载并安装最新稳定版本。安装完成后,在终端运行以下命令验…

vue实现截屏怎么实现

vue实现截屏怎么实现

Vue实现截屏的方法 使用html2canvas库 html2canvas是一个流行的JavaScript库,可以将网页元素转换为Canvas图像。在Vue项目中安装html2canvas: npm…