Vue 点名小页面
2020-07-14 06:20
120 查看
点名页面
这里放一张页面模板图
功能实现
样式啥的就不说了 各有喜好
名单打乱
created() { // 每次刷新页面打乱姓名排序 this.students.sort(() => 0.5 - Math.random()) // 当前名单的索引数组 this.studentsIndex = this.students.map((v, i) => i) }
我把名单数据放在students数组里,在vue的钩子函数里进行的打乱
开始点名
按钮的文字通过computed计算属性绑定(双向数据绑定真香)
btnText() { return this.intervalId ? '确定选择' : `点击随机抽取 ${this.num} 位` }
点击事件逻辑
selectStudents() { // 如果未开启定时器 if (!this.intervalId) { this.intervalId = setInterval(() => { // 获取到随机选中的学员索引数组 this.selectedStudentsIndex = this.studentsIndex.sort(() => 0.5 - Math.random()).slice(0, this.num) }, 100) } // 开启了定时器 else { clearInterval(this.intervalId) this.intervalId = null } }
确认结束点名
html结构和css也放在这里
<style> * { margin: 0; padding: 0; } body { background-color: #232323; color: #fff; } ul li { display: inline-block; padding: 10px; margin-right: 5px; border: 1px solid #fff; border-radius: 21px; color: #fff; } input { width: 45px; } button { position: relative; top: 0; left: 50%; width: 90px; height: 50px; transform: translateX(-50%); margin: 70px 0; border-radius: 25px; } .current { color: #000; background-color: #fff; transform: scale(1.2); transition: all .3s; } .selectBtn { width: 130px; border-color: #fff; background-color: #232323; color: #fff; } </style>
<div id="app"> <div> <p>总人数:{{students.length}},抽取<input v-model="num" />人</p> </div> <button :class="intervalId ? '' : 'selectBtn'" @click="selectStudents">{{btnText}}</button> <ul> <li v-for="(item, i) in students" :key="item.id" :class="{ current: selectedStudentsIndex.includes(i) }">{{item}}</li> </ul> </div>
相关文章推荐
- vue实现的上传图片到数据库并显示到页面功能示例
- VUE 实现tab切换页面效果
- Vue 2.x + Webpack 3.x + Nodejs 多页面项目框架(下篇——多页面VueSSR+热更新Server)
- 浅谈vue项目优化之页面的按需加载(vue+webpack)
- vue 单页面组件的2种传值方式
- 关于vue-print-nb插件,刷新页面后,@media print样式丢失解决方案
- 2019年 Vue 省市区联动方法,一秒生成,附带禁止页面跟随滚动效果
- vue开发多页面应用 - hash模式和history模式
- 在vue中嵌入iframe页面
- vue.js实现刷新当前页面的方法教程
- VUE 中 使用 iview Form组件 enter键防止页面刷新
- 解决vue路由跳转页面之后页面不从最顶端显示问题
- vue单页应用在页面刷新时保留状态数据的方法
- vue中使用swiper页面不滑动,swiper中的js不生效的问题解决
- (六)vue开发 - transition之页面切换过渡动画
- Vue中如何使用路由跳转页面及传递参数
- 解决使用Vue.js显示数据的时,页面闪现原始代码
- 关于Vue项目路由栏去除“#”的处理方法及其刷新页面时出现404问题
- vue-router进入页面时导航栏没有active效果
- vue学习【三】vue-router路由显示多页面