html+css+js实现转盘抽奖
•
前端


转盘 .zp { position: relative; border-radius: 100%; width: 300px; overflow: hidden; margin:0 auto; } .zp-panel { background: url(./assets/dial.png) no-repeat center center; background-size: 100%; width: 300px; height: 300px; position: relative; transform: rotate(25deg); transition: transform 3s ease-out; } .item { font-size: 12px; position: absolute; top: 50%; left: 0; right: 0; text-align: center; margin-top: -7px; line-height: 1; } .btn { position: absolute; background-color: green; width: 80px; height: 80px; border-radius: 100%; left: 50%; top: 50%; transform: translate(-50%, -50%); z-index: 2; display: flex; align-items: center; justify-content: center; color: #fff; } .btn:after { content: ''; position: absolute; height: 80px; width: 4px; background-color: green; left: 38px; bottom: 60px; } 开始 const arr = [ {name: ''}, {name: ''}, {name: ''}, {name: ''}, {name: ''}, {name: ''}, {name: ''}, {name: ''} ] const zp = document.querySelector('.zp-panel') const btn = document.querySelector('.btn') const d = 360 / arr.length zp.innerHTML = arr.map((v, i) => { // console.log(v, i); return `${v.name}` }).join('') btn.onclick = () => { // const zpIndex = arr.findIndex(v => v.is) const zpIndex = Math.random()*8 console.log(zpIndex); const deg = zpIndex * d + Math.random()*180 console.log(deg); // 清空上次抽奖,暂停动画效果 zp.style.transition = 'inherit' zp.style.transform = '' setTimeout(() => { // 重置transition,恢复动画效果 zp.style.transition = '' setTimeout(() => { // 触发动画 zp.style.transform = `rotate(${deg}deg)` }) }) }
本文来自网络,不代表协通编程立场,如若转载,请注明出处:https://net2asp.com/5ee47dc740.html
