纯js实现的刮刮卡效果,用例见 scratchCard.html
./libs/scratchCard.js
页面html元素
<div class="scratch">
<div class="card">
<img src="./image/scratchCard-example1.png" alt="" />
</div>
</div>
CSS样式
.scratch { position: relative; }
.cover { position: absolute; top: 0; left: 0; }
.card { -webkit-user-select: none; user-select: none; background: #FAFAFA; }
.card img { width: 100%; height: 100%; }
.scratch { width: 300px; margin: 20px auto 0; border: 1px solid #ccc; }
引入插件文件,初始化页面元素
<script type="text/javascript" src="./libs/scratchCard.js"></script>
scratchCard.case({params}, callback())
scratch
card
coverImg
回调方法,刮出奖品执行此方法
scratchCard.case({
scratch: '.scratch',
card: '.card',
coverImg: './image/example.png',
callback: function() {
alert('中奖啦!');
//清除掉刮开层的所有像素
this.clearCover();
}
});