- 相關(guān)推薦
如何使用JS實(shí)現(xiàn)PC端移動(dòng)端的刮卡效果
導(dǎo)語(yǔ):javascript是一門神奇的語(yǔ)言,可以實(shí)現(xiàn)諸多效果的設(shè)計(jì),下面小編要給大家提供的是如何使用JS實(shí)現(xiàn)PC端移動(dòng)端的刮卡效果,更多詳情請(qǐng)關(guān)注應(yīng)屆畢業(yè)生考試網(wǎng)。
具體代碼:
<!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>
//控制刮卡次數(shù)
var t = 0;
//初始化所有數(shù)據(jù)并且隨機(jī)產(chǎn)生獎(jiǎng)品
var initialize = function() {
//剩余刮卡次數(shù)
$('.num1').html(4 - t);
//隨機(jī)數(shù)
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 = "恭喜您,中獎(jiǎng)了!"
var ok = document.getElementById("ok");
ok.style.display = "block";
//點(diǎn)擊領(lǐng)取獎(jiǎng)品
ok.onclick = function() {
window.location.href = "prize.html"
};
} else {
document.getElementById("prompt").innerHTML = "很遺憾,未中獎(jiǎng)!"
document.getElementById("no").style.display = "block";
}
};
var c1; //畫(huà)布
var ctx; //畫(huà)筆
var ismousedown; //標(biāo)志用戶是否按下鼠標(biāo)或開(kāi)始觸摸
var isOk = 0; //標(biāo)志用戶是否已經(jīng)刮開(kāi)了一半以上
var fontem = parseInt(window.getComputedStyle(document.documentElement, null)["font-size"]); //這是為了不同分辨率上配合@media自動(dòng)調(diào)節(jié)刮的寬度
/* 頁(yè)面加載后開(kāi)始初始化畫(huà)布 */
window.onload = function() {
initialize();
c1 = document.getElementById("c1");
//這里很關(guān)鍵,canvas自帶兩個(gè)屬性width、height,我理解為畫(huà)布的分辨率,跟style中的width、height意義不同。
//最好設(shè)置成跟畫(huà)布在頁(yè)面中的實(shí)際大小一樣
//不然canvas中的坐標(biāo)跟鼠標(biāo)的坐標(biāo)無(wú)法匹配
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);
//移動(dòng)端的處理
c1.addEventListener('touchstart', eventDown, false);
c1.addEventListener('touchend', eventUp, false);
c1.addEventListener('touchmove', eventMove, false);
//初始化
initCanvas();
}
//初始化畫(huà)布,畫(huà)灰色的矩形鋪滿
function initCanvas() {
//網(wǎng)上的做法是給canvas設(shè)置一張背景圖片,我這里的做法是直接在canvas下面另外放了個(gè)p。
//c1.style.backgroundImage="url(中獎(jiǎng)圖片.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);
//把這個(gè)屬性設(shè)為這個(gè)就可以做出圓形橡皮擦的效果
//有些老的手機(jī)自帶瀏覽器不支持destination-out,下面的代碼中有修復(fù)的方法
ctx.globalCompositeOperation = 'destination-out';
}
//鼠標(biāo)按下 和 觸摸開(kāi)始
function eventDown(e) {
e.preventDefault();
ismousedown = true;
}
//鼠標(biāo)抬起 和 觸摸結(jié)束
function eventUp(e) {
e.preventDefault();
//得到canvas的全部數(shù)據(jù)
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++;
}
//當(dāng)被刮開(kāi)的區(qū)域等于一半時(shí),則可以開(kāi)始處理結(jié)果
if (j >= a.data.length / 8) {
isOk = 1;
}
ismousedown = false;
}
//鼠標(biāo)移動(dòng) 和 觸摸移動(dòng)
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;
//畫(huà)360度的弧線,就是一個(gè)圓,因?yàn)樵O(shè)置了ctx.globalCompositeOperation = 'destination-out';
//畫(huà)出來(lái)是透明的
ctx.beginPath();
ctx.arc(x, y, fontem * 1.2, 0, Math.PI * 2, true);
//下面3行代碼是為了修復(fù)部分手機(jī)瀏覽器不支持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";
}
}
//沒(méi)有中獎(jiǎng)再來(lái)一次
$("#no").click(function() {
if (t > 3) {
//因該彈出遮罩層提示您的次數(shù)已經(jīng)用完了
$('.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" />
<p class="ggl" id="top">
<p class="info" id="prize">
<span id="prompt"></span>
<span class="btn" id="ok">領(lǐng)取獎(jiǎng)品</span>
<span class="btn" id="no">再來(lái)一次</span>
</p>
<canvas id="c1" class="canvas"></canvas>
</p>
<p class="num">
您還有<span class="num1"></span>次刮卡機(jī)會(huì)
</p>
<img src="img/guize.png" class="guize" />
<!-- 遮罩層1抽獎(jiǎng)次數(shù)已經(jīng)用完-->
<p class="pop pop1">
<img src="img/pop1.png" />
</p>
<p class="pop pop2">
<img src="img/pop2.png" id="pop2" />
</p>
</body>
</html>
【如何使用JS實(shí)現(xiàn)PC端移動(dòng)端的刮卡效果】相關(guān)文章:
移動(dòng)端網(wǎng)頁(yè)設(shè)計(jì)方法03-14
如何使用javascript實(shí)現(xiàn)瀑布流及效果加載06-17
php上傳圖片客戶端和服務(wù)器端實(shí)現(xiàn)方法08-22
中國(guó)銀行專業(yè)人員繼續(xù)教育手機(jī)移動(dòng)端使用說(shuō)明08-16
javasocket服務(wù)端技巧10-22
如何實(shí)現(xiàn)JS仿QQ郵箱收件人選擇和搜索08-22
如何實(shí)現(xiàn)JavaScript的DIV塊來(lái)回滾動(dòng)效果06-30