JavaScript 和 CSS3 做出轉盤抽獎效果

來源:互聯網
上載者:User

標籤:border   nts   style   order   20px   relative   css   random   text   

首先是HTML代碼

<p id="text">您還剩餘3次機會</p><div class="box"><img src="./img/lanren.png" ><div class="btn btn1"></div><div class="btn2 btn"></div></div>

 然後是CSS代碼

        body{
      padding: 0;
      margin: 0;
     } .box{
        width: 640px; height: 640px; border-radius: 50%; margin: 150px auto; position: relative; } img{ width: 100%; height: 100%; transform: rotateZ(0deg); transition: all 2s; } .btn{ position: absolute; width: 190px; height: 220px; background-image: url("./img/arrow.png"); background-repeat: no-repeat; background-position: -30px 0; top: 50%; left: 50%; margin-left: -95px; margin-top: -110px; } .btn2{ background-position: -281px 0; display: none; } p{ width: 100%; height: 40px; line-height: 40px; text-align: center; font-size: 20px; color: #facebf; }

最後加JS代碼

 1         //初始化一個角度 2         var x = 0; 3         //定義一共能抽獎多少次 4         var a = 3; 5         //擷取需要的dom元素 6         var text = document.getElementById("text"); 7         var btn1 = document.getElementsByClassName(‘btn1‘)[0]; 8         var btn2 = document.getElementsByClassName(‘btn2‘)[0]; 9         var img = document.getElementsByTagName(‘img‘)[0];10         //定義一個隨機數  在下面使用11         function random(a,b){12             return Math.floor(Math.random()*(b-a))+a;               13         }14         //點擊按鈕時  執行事件  首先a-1  然後再隨機得到一個角度數字  然後再賦值給img元素  最後再執行一個回呼函數 如果直接把回呼函數裡面的代碼將會出現BUG 但是我不知道什麼情況    由這個回呼函數來判斷還有幾次抽獎機會15         btn1.onclick=function(){16             a-=1;17             x += parseInt(random(10, 30) * 60);18       img.style.transform = "rotateZ(" + x + "deg)";19             qq(a);20 21         }22         function qq(x){23             text.innerHTML=`您還有${x}次機會`;24             if(x==0){  //如果次數等於0  則不執行任何事件25                 text.innerHTML=`您還有0次機會`;26                 btn1.onclick=function(){27                     return false;28                 }29             }30         }    

 

JavaScript 和 CSS3 做出轉盤抽獎效果

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.