vue做的一個一點就轉的轉盤元件
阿新 • • 發佈:2019-02-20
優點,無須等待介面返回,一點就轉,資料返回才停到指定位置
待優化:目前轉完一次都要重置,望改正
<template> <div> <div class="luck-wheel"> <div id="luck-wheel" class="wheel" :style="rotateStyle"></div> <img class="control" :src="controlImg" @click="startRotate" /> </div> <h1 @click="isAllowClick = false">stop</h1> <h1>記錄轉動後的角度 可大於360度 = {{deg}}</h1> <h1>在360度內 最後指標停下的角度 = {{rotate}}</h1> </div> </template> <script> import startBtn from '../assets/img/btn_start_n.png' import rotateBtn from '../assets/img/btn_start_s.png' let t1 = 0 let t2 = 0 export default { props: { endrotate: { type: Number, default: 0 } }, data() { return { // t1: 0, // t2: 0, divide: 90, // 平分度 注:必須修改 speed: 0, // 轉的幅度 預設勻速 最好為兩個角度間 90/10 的十分之一 注:必須修改 deg: 0, // 記錄轉動後的角度 可大於360 rotate: 0, // 在360度內 最後指標停下的角度 // endrotate: false, // 傳入endrotate isAllowClick: true, // 轉動轉盤的開關 isAllowClick2: true, // 轉動轉盤的開關 time: 100, controlImg: startBtn, rotateStyle: '-webkit-transform: rotate(' + this.deg + 'deg);transform: rotate(' + this.deg + 'deg);' } }, watch: { endrotate(val) { if (val == 0) { this.reset() } } }, methods: { reset() { t1 = 0 t2 = 0 this.speed = 0 // 轉的幅度 預設勻速 最好為兩個角度間 90/10 的十分之一 注:必須修改 this.deg = 0 // 記錄轉動後的角度 可大於360 this.rotate = 0 // 在360度內 最後指標停下的角度 this.rotateStyle = '-webkit-transform: rotate(' + this.deg + 'deg);transform: rotate(' + this.deg + 'deg);' }, startRotate() { let that = this that.isAllowClick = true that.controlImg = rotateBtn that.turntable() }, endfuc() { let that = this if (that.endrotate) { let com = that.endrotate - that.rotate console.log(com) let comdeg = that.deg + com + 720 // -----------------------計數時有問題-------------------------- // let x = com * 1 + 720 // let a = that.speed * that.speed / (2 * x) // t2 = setInterval(function() { // if (that.isAllowClick2) { // that.speed = that.speed <= 10 ? that.speed : that.speed - a // that.deg = that.deg + that.speed // that.rotate = that.deg % 360 // console.log('最終度數2') // console.log(that.speed) // if (that.deg > comdeg) { // that.isAllowClick2 = false // } else { // that.rotateStyle = // '-webkit-transform: rotate(' + // that.deg + // 'deg);transform: rotate(' + // that.deg + // 'deg);' // console.log('最終度數') // console.log(comdeg) // } // } else { // for (let i = 0; i <= t2; i++) { // clearInterval(i) // } // return // } // }, that.time) //-------------------------END--------------------------- //-------------------目前無bug------------------------ that.rotateStyle = '-webkit-transform: rotate(' + comdeg + 'deg);transform: rotate(' + comdeg + 'deg);transition:all 2s ease-out' console.log('最終度數') console.log(comdeg) //------------------END--------------------- that.isAllowClick = false that.controlImg = startBtn return } }, turntable() { let that = this that.$emit('endlottery') t1 = setInterval(function() { console.log('最終度數') if (that.isAllowClick) { that.speed = that.speed >= 120 ? that.speed : that.speed + 10 that.deg = that.deg + that.speed that.rotateStyle = '-webkit-transform: rotate(' + that.deg + 'deg);transform: rotate(' + that.deg + 'deg);transition:all 0.2s linear' that.rotate = that.deg % 360 // console.log(that.deg) // console.log(that.deg % 360) that.endfuc() } else { for (let i = 0; i <= t1; i++) { clearInterval(i) } } }, that.time) } }, mounted() {} } </script> <style lang="less" scoped> .luck-wheel { position: relative; width: 6rem; height: 6rem; background-size: cover; .wheel { width: 100%; height: 100%; background-image: url('../assets/img/lotterydial.png'); background-size: cover; } .control { position: absolute; top: 1.95rem; left: 2rem; width: 2rem; height: 2.1rem; } } </style>
注:尊重原創,轉發請說明。謝謝