国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 編程 > JavaScript > 正文

js實現PC端和移動端刮卡效果

2019-11-19 17:38:12
字體:
來源:轉載
供稿:網友

本文實例為大家分享了js刮卡效果的具體代碼,供大家參考,具體內容如下

效果圖:

具體代碼:

<!DOCTYPE html><html>  <head>    <meta charset="utf-8">    <meta name="viewport" content="initial-scale=1.0, maximum-scale=1.0, user-scalable=no" />    <title>小月博客刮刮卡案例分享</title>    <script src="http://cdn.bootcss.com/jquery/2.1.4/jquery.min.js"></script>    <style type="text/css">      * {        padding: 0;        margin: 0;        list-style: none;      }      body {        background: #E34830;        position: relative;      }      .banner1 {        display: block;        width: 100%;        /*height: auto;*/        overflow: hidden;      }      .ggl {        position: relative;        width: 85.6%;        height: 90px;        margin: -5px auto;        background: url(img/ggl.png) no-repeat center center;        background-size: 100% 100%;        border: 1px solid blue;      }      .canvas {        position: absolute;        top: 2px;        left: 2.5%;        width: 95%;        height: 82px;        line-height: 82px;        text-align: center;        z-index: 2;        border: 1px solid black;      }      .info {        position: absolute;        top: 2px;        left: 2.5%;        width: 95%;        height: 82px;        text-align: center;      }      .info span {        display: block;        font-size: 18px;      }      #prompt {        line-height: 40px;      }      .btn {        position: relative;        width: 50%;        height: 35px;        line-height: 35px;        background: #df412b;        color: #fff;        border-radius: 5px;        margin: 0 auto;        z-index: 1;      }      .guize {        display: block;        width: 85.6%;        height: auto;        margin: 5% auto 10% auto;        border-radius: 5px;        border: 1px solid black;      }      .num {        width: 90%;        margin: 0 auto;        height: 30px;        line-height: 30px;        text-align: center;        font-size: 14px;        margin-top: 5%;        border: 1px solid black;      }      #ok,      #no {        display: none;      }      .pop {        position: fixed;        left: 0;        top: 0;        z-index: 3;        width: 100%;        height: 100%;        background: rgba(0, 0, 0, 0.6);        display: none;      }      .pop img {        width: 100%;        height: auto;        overflow: hidden;        margin: 15% auto;      }    </style>    <script>      //控制刮卡次數      var t = 0;      //初始化所有數據并且隨機產生獎品      var initialize = function() {        //剩余刮卡次數        $('.num1').html(4 - t);        //隨機數        function getRandomNum(lbound, ubound) {          return (Math.floor(Math.random() * (ubound - lbound)) + lbound);        }        var r = getRandomNum(1, 100);        var btn = document.getElementsByClassName("btn");        for (var i = 0; i < btn.length; i++) {          btn[i].style.zIndex = '1';        }        document.getElementById("no").style.display = "none";        document.getElementById("ok").style.display = "none";        //初始化涂抹面積        isOk = 0;        if (r < t * 33) {          document.getElementById("prompt").innerHTML = "恭喜您,中獎了!"          var ok = document.getElementById("ok");          ok.style.display = "block";          //點擊領取獎品          ok.onclick = function() {            window.location.href = "prize.html"          };        } else {          document.getElementById("prompt").innerHTML = "很遺憾,未中獎!"          document.getElementById("no").style.display = "block";        }      };      var c1; //畫布      var ctx; //畫筆      var ismousedown; //標志用戶是否按下鼠標或開始觸摸      var isOk = 0; //標志用戶是否已經刮開了一半以上      var fontem = parseInt(window.getComputedStyle(document.documentElement, null)["font-size"]); //這是為了不同分辨率上配合@media自動調節刮的寬度      /* 頁面加載后開始初始化畫布 */      window.onload = function() {        initialize();        c1 = document.getElementById("c1");        //這里很關鍵,canvas自帶兩個屬性width、height,我理解為畫布的分辨率,跟style中的width、height意義不同。        //最好設置成跟畫布在頁面中的實際大小一樣        //不然canvas中的坐標跟鼠標的坐標無法匹配        c1.width = c1.clientWidth;        c1.height = c1.clientHeight;        ctx = c1.getContext("2d");        //PC端的處理        c1.addEventListener("mousemove", eventMove, false);        c1.addEventListener("mousedown", eventDown, false);        c1.addEventListener("mouseup", eventUp, false);        //移動端的處理        c1.addEventListener('touchstart', eventDown, false);        c1.addEventListener('touchend', eventUp, false);        c1.addEventListener('touchmove', eventMove, false);        //初始化        initCanvas();      }      //初始化畫布,畫灰色的矩形鋪滿      function initCanvas() {        //網上的做法是給canvas設置一張背景圖片,我這里的做法是直接在canvas下面另外放了個div。        //c1.style.backgroundImage="url(中獎圖片.jpg)";        ctx.globalCompositeOperation = "source-over";        ctx.fillStyle = '#aaaaaa';        ctx.fillRect(0, 0, c1.clientWidth, c1.clientHeight);        ctx.fill();        ctx.font = "Bold 30px Arial";        ctx.textAlign = "center";        ctx.fillStyle = "#999999";        ctx.fillText("刮一刮", c1.width / 2, 50);        //把這個屬性設為這個就可以做出圓形橡皮擦的效果        //有些老的手機自帶瀏覽器不支持destination-out,下面的代碼中有修復的方法        ctx.globalCompositeOperation = 'destination-out';      }      //鼠標按下 和 觸摸開始      function eventDown(e) {        e.preventDefault();        ismousedown = true;      }      //鼠標抬起 和 觸摸結束      function eventUp(e) {        e.preventDefault();        //得到canvas的全部數據        var a = ctx.getImageData(0, 0, c1.width, c1.height);        var j = 0;        for (var i = 3; i < a.data.length; i += 4) {          if (a.data[i] == 0) j++;        }        //當被刮開的區域等于一半時,則可以開始處理結果        if (j >= a.data.length / 8) {          isOk = 1;        }        ismousedown = false;      }      //鼠標移動 和 觸摸移動      function eventMove(e) {        e.preventDefault();        if (ismousedown) {          if (e.changedTouches) {            e = e.changedTouches[e.changedTouches.length - 1];          }          var topY = document.getElementById("top").offsetTop;          var oX = c1.offsetLeft,            oY = c1.offsetTop + topY;          var x = (e.clientX + document.body.scrollLeft || e.pageX) - oX || 0,            y = (e.clientY + document.body.scrollTop || e.pageY) - oY || 0;          //畫360度的弧線,就是一個圓,因為設置了ctx.globalCompositeOperation = 'destination-out';          //畫出來是透明的          ctx.beginPath();          ctx.arc(x, y, fontem * 1.2, 0, Math.PI * 2, true);          //下面3行代碼是為了修復部分手機瀏覽器不支持destination-out          //我也不是很清楚這樣做的原理是什么          c1.style.display = 'none';          c1.offsetHeight;          c1.style.display = 'inherit';          ctx.fill();        }        if (isOk) {          var btn = document.getElementsByClassName("btn");          for (var i = 0; i < btn.length; i++) {            btn[i].style.zIndex = '3';          }          document.getElementsByClassName("btn")[0].style.zIndex = "3";        }      }      //沒有中獎再來一次      $("#no").click(function() {        if (t > 3) {          //因該彈出遮罩層提示您的次數已經用完了          $('.pop1').show();          $('.pop1 img').click(function() {            $('.pop1').hide();          })        } else {          t++;          //初始化按鈕          document.getElementById("no").style.display = "none";          document.getElementById("ok").style.display = "none";          window.onload();          initCanvas();        }      });    </script>  </head>  <body>    <img src="img/banner1.png" class="banner1" />    <div class="ggl" id="top">      <div class="info" id="prize">        <span id="prompt"></span>        <span class="btn" id="ok">領取獎品</span>        <span class="btn" id="no">再來一次</span>      </div>      <canvas id="c1" class="canvas"></canvas>    </div>    <div class="num">      您還有<span class="num1"></span>次刮卡機會    </div>    <img src="img/guize.png" class="guize" />    <!-- 遮罩層1抽獎次數已經用完-->    <div class="pop pop1">      <img src="img/pop1.png" />    </div>    <div class="pop pop2">      <img src="img/pop2.png" id="pop2" />    </div>  </body></html>

源碼下載:js刮卡效果

以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持武林網。

發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 安多县| 大竹县| 尼木县| 宣城市| 靖边县| 博湖县| 临汾市| 丽江市| 麻栗坡县| 堆龙德庆县| 诸城市| 贵港市| 天柱县| 会理县| 万源市| 翼城县| 资讯 | 青岛市| 邢台市| 定西市| 定兴县| 榆树市| 特克斯县| 柳州市| 南投市| 泉州市| 六盘水市| 梁河县| 宁强县| 舒兰市| 永登县| 信阳市| 临海市| 罗山县| 凤城市| 高雄市| 兰考县| 诏安县| 明星| 白沙| 乌兰浩特市|