我正在使用我找到的用于刮刮卡类型效果的脚本(来自http://beej.us/blog/data/html-canvas-scratcher-2/)。我需要添加什么代码,以便在图像的某个百分比(例如70%)被“划掉”时显示隐藏的div。 感谢。
**
* This file controls the page logic
*
* depends on jQuery>=1.7
*/
(function() {
/**
* Returns true if this browser supports canvas
*
* From http://diveintohtml5.info/
*/
function supportsCanvas() {
return !!document.createElement('canvas').getContext;
};
/**
* Handle scratch event on a scratcher
*/
function scratcher1Changed(ev) {
// Test every pixel. Very accurate, but might be slow on large
// canvases on underpowered devices:
//var pct = (scratcher.fullAmount() * 100)|0;
// Only test every 32nd pixel. 32x faster, but might lead to
// inaccuracy:
var pct = (this.fullAmount(32) * 100)|0;
$('#scratcher1Pct').html('' + pct + '%');
};
/**
* Reset all scratchers
*/
function onResetClicked(scratchers) {
var i;
for (i = 0; i < scratchers.length; i++) {
scratchers[i].reset();
}
return false;
};
/**
* Assuming canvas works here, do all initial page setup
*/
function initPage() {
var scratcherLoadedCount = 0;
var scratchers = [];
var i, i1;
// called each time a scratcher loads
function onScratcherLoaded(ev) {
scratcherLoadedCount++;
if (scratcherLoadedCount == scratchers.length) {
// all scratchers loaded!
// bind the reset button to reset all scratchers
$('#resetbutton').on('click', function() {
onResetClicked(scratchers);
});
// hide loading text, show instructions text
$('#loading-text').hide();
$('#inst-text').show();
}
};
// create new scratchers
var scratchers = new Array(1);
for (i = 0; i < scratchers.length; i++) {
i1 = i + 1;
scratchers[i] = new Scratcher('scratcher' + i1);
// set up this listener before calling setImages():
scratchers[i].addEventListener('imagesloaded', onScratcherLoaded);
scratchers[i].setImages('images/s' + i1 + 'bg.jpg',
'images/s' + i1 + 'fg.gif');
}
// get notifications of this scratcher changing
// (These aren't "real" event listeners; they're implemented on top
// of Scratcher.)
scratchers[0].addEventListener('reset', scratcher1Changed);
scratchers[0].addEventListener('scratch', scratcher1Changed);
// Or if you didn't want to do it every scratch (to save CPU), you
// can just do it on 'scratchesended' instead of 'scratch':
//scratchers[2].addEventListener('scratchesended', scratcher3Changed);
};
/**
* Handle page load
*/
$(function() {
if (supportsCanvas()) {
initPage();
} else {
$('#scratcher-box').hide();
$('#lamebrowser').show();
}
});
})();
答案 0 :(得分:0)
你已经有了这个:
<div class="hidden" id="show">This div to appear after 40% scracthed</div>
我将此添加到您的CSS:
.show{
display:block;
}
这是你的javascript:
function scratcher1Changed(ev) {
// Test every pixel. Very accurate, but might be slow on large
// canvases on underpowered devices:
//var pct = (scratcher.fullAmount() * 100)|0;
// Only test every 32nd pixel. 32x faster, but might lead to
// inaccuracy:
var pct = (this.fullAmount(32) * 100) | 0;
$('#scratcher1Pct').html('' + pct + '%');
//*******************
if(pct >= 40){
$('#show').addClass( "show" )
}
//*************** Added This...
};
编辑* * 的
重置FYI时,您还需要将课程设置回hidden
。