js轉盤抽獎

來源:互聯網
上載者:User

標籤:html   背景圖片   圖片   就會   rand   onload   clear   asc   code   

這個是很簡易的轉盤,只用了html,css,js

通過css產生一個轉盤上的指標,用js動態改變css中的transparent改變指標的角度。再添加一個背景圖片類似於獎項的轉盤

<!DOCTYPE html><html lang="en"><head>    <meta charset="UTF-8">    <title>dial</title>    <link rel="stylesheet" type="text/css" href="dial.css">    <script type="text/javascript" src="dial.js"></script></head><body><div id="sum">    <div id="content">    <div id="tri">    </div>    <div id="center"><h1>抽獎</h1></div></div></div></body></html>

背景圖片放在sum中,tri是指標上的三角形,center是指標上的圓,三角形和圓都在content中以便一起運動。

*{    margin: 0px;    padding:0px;}#sum{    height: 550px;    width: 700px;    background-image: url(dial.jpg);    position: relative;}#content{    padding: 0px;    height: 140px;    width: 140px;    left: 289px;    top: 109px;    margin: 120px auto 0px;    transform:rotate(0deg);    position: absolute;}#center{    height: 140px;    width: 140px;    border-radius: 140px;    background-color: #EE4F10;    color: white;    line-height: 140px;    font-weight: bold;    font-size: 30px;    text-align: center;    z-index: 2;    position:relative;}#tri{    height: 0px;    width: 0px;    top: -27px;    left: 26px;    border-left: 45px solid transparent;    border-right:45px solid transparent;    border-bottom: 45px solid #EE4F10;    z-index: -1;    position: absolute;}
window.onload=function(){    var data=[320,260,180,620,760,380];    var content=document.getElementById("content");    var center=document.getElementById("center");    center.onclick=play;    var timer=null;    var count=0;    var degv=0;    var random=0;    function play(){    count=0;    random=Math.floor(Math.random()*6);    clearInterval(timer);    timer=setInterval(function(){    roll();    },30);    }    function roll(){        degv=count++;    degv=degv*20;    if(degv==data[random])    {        clearInterval(timer);}    content.style.webkitTransform="rotate("+degv+"deg)";    content.style.msTransform="rotate("+degv+"deg)";    content.style.MozTransform="rotate("+degv+"deg)";    content.style.OTransform="rotate("+degv+"deg)";    content.style.transform="rotate("+degv+"deg)";    }           }

設定一個定時器不斷的調用roll方法不斷的加指標旋轉的角度,直到等於數組中隨機選中的角度就會停下來。

 

js轉盤抽獎

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.