javascript - 使用vue做个抽奖问题
问题描述
代码:
<!DOCTYPE html><html><head> <meta charset='UTF-8'> <title></title> <script src='https://cdn.bootcss.com/vue/2.3.4/vue.min.js'></script> <style>* { margin: 0; padding: 0;}#app ul li { width: 100px; height: 100px; box-sizing: border-box; background: #ccc; float: left;}#app ul li.active { border: 3px solid red;} </style></head><body><p id='app'> <ul><li v-for='(item, index) of items' :class='{active: index == actvieIndex}'>{{ item.name }}</li> </ul></p><script>var app = new Vue({ el: ’#app’, data () {return { actvieIndex: 0, items: [{name: ’1’},{name: ’2’},{name: ’3’},{name: ’4’},{name: ’5’}, // 这些都假设是一些用户名字{name: ’6’},{name: ’7’},{name: ’8’},{name: ’9’},{name: ’10’},{name: ’11’},{name: ’12’},{name: ’13’},{name: ’14’},{name: ’15’},{name: ’16’},{name: ’17’}, ]} }, mounted () {setInterval(() => { this.actvieIndex++; if(this.actvieIndex >= this.items.length ) this.actvieIndex = 0;}, 50); }})</script></body></html>
比如10秒就要停止显示, 快到10秒了,就应该慢慢的停止。 这样用户才会有那种激动感。
相信大家都玩过转盘,就跟那个类似。 但是不知道怎么做。
问题解答
回答1:简单的做了一个 demo。
<!DOCTYPE html><html><head> <meta charset='UTF-8'> <title></title> <script src='https://cdn.bootcss.com/vue/2.3.4/vue.min.js'></script> <style>* { margin: 0; padding: 0;}#app ul li { width: 100px; height: 100px; box-sizing: border-box; background: #ccc; float: left;}#app ul li.active { border: 3px solid red;} </style></head><body><p id='app'> <ul><li v-for='(item, index) of items' :class='{active: index == actvieIndex}'>{{ item.name }}</li> </ul></p><script>var app = new Vue({ el: ’#app’, data () {return { actvieIndex: 0, i: 0, count: 0, items: [{name: ’1’},{name: ’2’},{name: ’3’},{name: ’4’},{name: ’5’}, // 这些都假设是一些用户名字{name: ’6’},{name: ’7’},{name: ’8’},{name: ’9’},{name: ’10’},{name: ’11’},{name: ’12’},{name: ’13’},{name: ’14’},{name: ’15’},{name: ’16’},{name: ’17’}, ]} }, methods: { go(seconds) {const SECONDS = seconds * 1000;const HALF_SECONDS = SECONDS / 2;const FREQUENCY = 50;setTimeout(() => { console.log(this.count) this.actvieIndex++; if(this.actvieIndex >= this.items.length ) this.actvieIndex = 0; if (this.count >= HALF_SECONDS) { this.i+= 10; this.count += 50 + this.i; } else { this.count += 50; } if (this.count <= SECONDS) { this.go(seconds) }}, FREQUENCY + this.i); } }, mounted () { this.go(5) }})</script></body></html>回答2:
那你的定时器可以分两个5s执行啊,前5s快,后5s慢。
相关文章:
1. angular.js - ng-grid 和tabset一起用时,grid width默认特别小2. python for循环中的函数只能运行一次?3. css - transform-origin 旋转参考点4. html5 - 在一个页面中 初始了两个swiper 不知道哪里错了 一直不对5. selenium-基于python2-126邮箱登陆问题6. python的文件读写问题?7. 能用Nginx服务小型购物网站的web吗?8. python - pyspider爬取了接近1G的数据,无法导出csv?9. android - 美团筛选处筛选条件停靠+条件点击滑动到顶部。10. 请问是对象还是数组
