当前位置:首页 > VUE

vue 实现引导页

2026-03-30 01:27:34VUE

使用Vue实现引导页的方法

使用第三方库vue-tour

安装vue-tour库:

npm install vue-tour

在main.js中引入并注册:

vue 实现引导页

import Vue from 'vue'
import VueTour from 'vue-tour'
import 'vue-tour/dist/vue-tour.css'

Vue.use(VueTour)

在组件中使用:

<template>
  <v-tour name="myTour" :steps="steps"></v-tour>
</template>

<script>
export default {
  data() {
    return {
      steps: [
        {
          target: '#step1',
          content: '这是第一步的说明'
        },
        {
          target: '.step2',
          content: '这是第二步的说明'
        }
      ]
    }
  },
  mounted() {
    this.$tours['myTour'].start()
  }
}
</script>

自定义实现引导页

创建引导组件:

vue 实现引导页

<template>
  <div class="guide-overlay" v-if="isShow">
    <div class="guide-mask"></div>
    <div class="guide-content" :style="contentStyle">
      <div class="guide-text">{{ currentStep.text }}</div>
      <button @click="nextStep">下一步</button>
    </div>
  </div>
</template>

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

<style>
.guide-overlay {
  position: fixed;
  top: 0;
  left: 0;
  width: 100%;
  height: 100%;
  z-index: 9999;
}
.guide-mask {
  position: absolute;
  width: 100%;
  height: 100%;
  background: rgba(0,0,0,0.5);
}
.guide-content {
  position: absolute;
  background: white;
  padding: 10px;
  border-radius: 4px;
}
</style>

使用Driver.js库

安装Driver.js:

npm install driver.js

在组件中使用:

<template>
  <button @click="startTour">开始引导</button>
</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: '#step1',
          popover: {
            title: '第一步',
            description: '这是第一步的说明',
            position: 'bottom'
          }
        },
        {
          element: '.step2',
          popover: {
            title: '第二步',
            description: '这是第二步的说明',
            position: 'top'
          }
        }
      ])
      driver.start()
    }
  }
}
</script>

注意事项

  • 确保引导元素在DOM中存在
  • 对于动态加载的内容,需要在内容加载完成后启动引导
  • 可以通过localStorage记录用户是否已完成引导
  • 移动端需要考虑响应式布局和触摸事件
  • 提供跳过和上一步的按钮增强用户体验

标签: vue
分享给朋友:

相关文章

vue全选实现

vue全选实现

Vue 全选实现方法 在 Vue 中实现全选功能通常涉及数据绑定和事件处理。以下是几种常见的实现方式: 基础实现方式 <template> <div> <i…

vue实现高亮

vue实现高亮

Vue 实现文本高亮的方法 在 Vue 中实现文本高亮可以通过多种方式完成,以下是几种常见的方法: 方法一:使用 v-html 指令 通过 v-html 指令可以动态插入 HTML 内容,将需要高…

vue SSG实现

vue SSG实现

Vue SSG 实现方法 Vue 的静态站点生成(SSG)可以通过多种工具和框架实现,以下是几种常见的方法: 使用 VuePress VuePress 是一个基于 Vue 的静态站点生成器,适合文档…

vue实现弹幕

vue实现弹幕

Vue 实现弹幕功能 弹幕功能通常包括动态生成、滚动显示、颜色和速度随机化等特性。以下是基于 Vue 3 的实现方法。 核心思路 使用 CSS 动画控制弹幕从右向左滚动。 动态生成弹幕数据,随机设置…

vue实现swiper

vue实现swiper

Vue 中实现 Swiper 的方法 安装 Swiper 依赖 在 Vue 项目中安装 Swiper 和相关依赖: npm install swiper vue-awesome-swiper 全局引…

vue实现导航

vue实现导航

使用 Vue Router 实现导航 Vue Router 是 Vue.js 官方提供的路由管理器,用于构建单页应用(SPA)的导航系统。以下是实现导航的基本步骤。 安装 Vue Router n…