…">
当前位置:首页 > VUE

vue实现随机点名

2026-03-09 16:56:10VUE

vue实现随机点名

vue实现随机点名

实现随机点名的Vue组件

创建Vue组件需要定义数据和方法,以下是一个简单的随机点名实现方案:

<template>
  <div class="random-call">
    <h3>随机点名系统</h3>
    <textarea v-model="nameList" placeholder="请输入名单,每行一个名字"></textarea>
    <button @click="shuffleNames">开始随机</button>
    <button @click="stopShuffle" v-if="isShuffling">停止</button>
    <div class="result" v-if="selectedName">
      选中: <span class="name">{{ selectedName }}</span>
    </div>
    <div class="shuffling" v-if="isShuffling && !selectedName">
      随机中: {{ currentShuffleName }}
    </div>
  </div>
</template>

组件逻辑实现

<script>
export default {
  data() {
    return {
      nameList: '',
      selectedName: '',
      currentShuffleName: '',
      isShuffling: false,
      shuffleInterval: null
    }
  },
  methods: {
    shuffleNames() {
      const names = this.nameList.split('\n').filter(name => name.trim())
      if (names.length === 0) return

      this.isShuffling = true
      this.selectedName = ''

      let counter = 0
      this.shuffleInterval = setInterval(() => {
        this.currentShuffleName = names[Math.floor(Math.random() * names.length)]
        counter++

        if (counter > 30) {
          this.stopShuffle()
        }
      }, 100)
    },
    stopShuffle() {
      clearInterval(this.shuffleInterval)
      this.isShuffling = false
      this.selectedName = this.currentShuffleName
      this.currentShuffleName = ''
    }
  }
}
</script>

样式设计

<style scoped>
.random-call {
  max-width: 500px;
  margin: 0 auto;
  padding: 20px;
  text-align: center;
}

textarea {
  width: 100%;
  height: 200px;
  margin: 10px 0;
  padding: 8px;
}

button {
  padding: 8px 16px;
  margin: 0 5px;
  background-color: #42b983;
  color: white;
  border: none;
  border-radius: 4px;
  cursor: pointer;
}

button:hover {
  background-color: #369f6e;
}

.name {
  font-size: 24px;
  color: #e74c3c;
  font-weight: bold;
}

.shuffling {
  margin-top: 20px;
  font-size: 18px;
  color: #3498db;
}
</style>

功能扩展建议

  1. 增加历史记录功能,保存已点名的学生
  2. 添加音效增强体验感
  3. 实现排除已点名学生的功能
  4. 增加名单导入导出功能
  5. 添加动画效果使随机过程更生动

使用方法

  1. 在文本框中输入名单,每行一个名字
  2. 点击"开始随机"按钮开始随机选择
  3. 随机过程中会快速显示不同名字
  4. 点击"停止"按钮或等待自动停止后显示最终结果

该组件可以单独使用,也可以集成到更大的Vue应用中。根据需求可以调整随机速度和动画效果。

标签: vue
分享给朋友:

相关文章

vue实现关键词轮播

vue实现关键词轮播

Vue 实现关键词轮播 基础实现方案 使用 Vue 的 v-for 和 setInterval 实现基础轮播效果: <template> <div class="keyword…

vue实现全景

vue实现全景

Vue 实现全景效果的方法 使用 Vue 实现全景效果通常需要结合第三方库或框架,以下是几种常见的方法: 使用 Three.js 和 Vue-Three.js Three.js 是一个强大的 3D…

vue实现通知

vue实现通知

Vue 实现通知功能的方法 使用 Vue 实现通知功能可以通过多种方式实现,以下是一些常见的方法: 使用 Vue 的全局事件总线 在 Vue 中可以通过事件总线实现通知功能。创建一个全局事件总线,然…

vue实现树

vue实现树

Vue 实现树形结构的方法 使用递归组件实现树形结构 递归组件是Vue中实现树形结构的常见方式。通过组件调用自身,可以轻松构建多层嵌套的树形数据。 <template> <ul…

利用vue 实现

利用vue 实现

以下是利用 Vue 实现常见功能的几种方法,分为不同场景和技术要点: 数据绑定与响应式更新 Vue 的核心特性是数据绑定,通过 v-model 实现双向绑定: <template>…

vue实现CRUD

vue实现CRUD

Vue 实现 CRUD 操作 Vue 提供了响应式数据绑定和组件化开发能力,结合后端 API 可以轻松实现 CRUD(增删改查)功能。以下是基于 Vue 3 和 Axios 的完整实现方案。 安装依…