我的图片有两个以下代码,问题是当点击时没有发生任何事情。
<div style="float: left;"><input onload="setValue()" type="image" src="img/board new/blank.png" name="saveForm" class="btTxt submit" id="spinner" onclick="spin()" /> </div>
这是我的完整代码。
<html>
<style>
body {
background-image: url(img/background4.png);
background-size: 100% 100%;
background-repeat: no-repeat;
}
</style>
<script>
var stop = false;
var spinResult;
var gameOver = false;
var oldHTML;
var newHTML;
function setValue() {
var rollResult = location.search.substr(location.search.indexOf("=")+1);
document.getElementById('own').innerHTML = "You own " + rollResult + " piece(s) on the board.";
}
function advanceSpinner(i) {
i = i || 1;
if (stop == false) {
if (i > 10)
i = 1; // change this to return if you don't want to run forever
document.getElementById("spinner").src = "img/board new/" + i + ".png";
spinResult = i;
setTimeout(function () { advanceSpinner(i + 1) }, 50);
}
}
function spin() {
var start = Math.floor(Math.random() * (10 - 1 + 1)) + 1
advanceSpinner(start);
setTimeout(function () {stop = true;}, 4000);
setTimeout(function () {checkWin();}, 4050);
}
function checkWin() {
/*
if (rollResult == 1 && spinResult == 1) {
alert("Win");
} else if (rollResult == 2 && spinResult == 1) {
alert("Win");
} else if (rollResult == 2 && spinResult == 3) {
alert("Win");
} else if (rollResult == 3 && spinResult == 1) {
alert("Win");
} else if (rollResult == 3 && spinResult == 3) {
alert("Win");
} else if (rollResult == 3 && spinResult == 6) {
alert("Win");
} else if (rollResult == 4 && spinResult == 1) {
alert("Win");
} else if (rollResult == 4 && spinResult == 3) {
alert("Win");
} else if (rollResult == 4 && spinResult == 6) {
alert("Win");
} else if (rollResult == 4 && spinResult == 8) {
alert("Win");
}
*/
var fadeEffect=function(){
return{
init:function(id, flag, target){
this.elem = document.getElementById(id);
clearInterval(this.elem.si);
this.target = target ? target : flag ? 100 : 0;
this.flag = flag || -1;
this.alpha = this.elem.style.opacity ? parseFloat(this.elem.style.opacity) * 100 : 0;
this.elem.si = setInterval(function(){fadeEffect.tween()}, 20);
},
tween:function(){
if(this.alpha == this.target){
clearInterval(this.elem.si);
}else{
var value = Math.round(this.alpha + ((this.target - this.alpha) * .05)) + (1 * this.flag);
this.elem.style.opacity = value / 100;
this.elem.style.filter = 'alpha(opacity=' + value + ')';
this.alpha = value
}
}
}
}();
</script>
<head>
<title>Power Up! - Spin</title>
</head>
<body>
<div style="float: left;"><input onload="setValue()" type="image" src="img/board new/blank.png" name="saveForm" class="btTxt submit" id="spinner" onclick="spin()" /> </div>
</br>
<h2 id="own"> </h1>
<h1 id='result'> </h1>
</body>
</html>
答案 0 :(得分:1)
我使用JSFiddle查看了你的源代码,我纠正了很多像“;”声明结束:
JS
function setValue() {
rollResult = location.search.substr(location.search.indexOf("=") + 1);
}
var stop = false;
var spinResult;
var gameOver = false;
var oldHTML;
var newHTML;
var rollResult;
var start;
function setValue() {
rollResult = location.search.substr(location.search.indexOf("=") + 1);
//document.getElementById('own').innerHTML = "You own " + rollResult + " piece(s) on the board.";
}
function advanceSpinner(i) {
console.log(i);
i = i || 1;
console.log(stop);
if (stop === false) {
if (i < 10) {
// i = 1;
document.getElementById("spinner").src = "img/board new/" + i + ".png";
spinResult = i;
setTimeout(function () {
advanceSpinner(i + 1);
}, 50);
}
}
}
function spin() {
alert("Test if being executed.");
start = Math.floor(Math.random() * (10 - 1 + 1)) + 1;
advanceSpinner(start);
setTimeout(function () {
stop = true;
}, 4000);
setTimeout(function () {
checkWin();
}, 4050);
}
function checkWin() {
/*
if (rollResult == 1 && spinResult == 1) {
alert("Win");
} else if (rollResult == 2 && spinResult == 1) {
alert("Win");
} else if (rollResult == 2 && spinResult == 3) {
alert("Win");
} else if (rollResult == 3 && spinResult == 1) {
alert("Win");
} else if (rollResult == 3 && spinResult == 3) {
alert("Win");
} else if (rollResult == 3 && spinResult == 6) {
alert("Win");
} else if (rollResult == 4 && spinResult == 1) {
alert("Win");
} else if (rollResult == 4 && spinResult == 3) {
alert("Win");
} else if (rollResult == 4 && spinResult == 6) {
alert("Win");
} else if (rollResult == 4 && spinResult == 8) {
alert("Win");
}
*/
}
var fadeEffect = function () {
return {
init: function (id, flag, target) {
this.elem = document.getElementById(id);
clearInterval(this.elem.si);
this.target = target ? target : flag ? 100 : 0;
this.flag = flag || -1;
this.alpha = this.elem.style.opacity ? parseFloat(this.elem.style.opacity) * 100 : 0;
this.elem.si = setInterval(function () {
fadeEffect.tween();
}, 20);
},
tween: function () {
if (this.alpha == this.target) {
clearInterval(this.elem.si);
} else {
var value = Math.round(this.alpha + ((this.target - this.alpha) * 0.05)) + (1 * this.flag);
this.elem.style.opacity = value / 100;
this.elem.style.filter = 'alpha(opacity=' + value + ')';
this.alpha = value;
}
}
};
}();
这是你想要的吗?
编辑:
我纠正了JSFiddle,你现在可以检查一下吗?
答案 1 :(得分:1)
不应该
if (i > 10)
是
if (i < 10)
您还需要更改
setTimeout(function () { advanceSpinner(i + 1); }, 50);
到
i++;
setTimeout(function () { advanceSpinner(i); }, 50);
否则它永远不会结束。
我还建议将i
更改为全局var spinCount
,否则您可能会将i
与其他变量混淆。
答案 2 :(得分:0)
使用 demo here 查看控制台
var stop = false;
function setValue() {
rollResult = location.search.substr(location.search.indexOf("=") + 1);
}
function spin() {
var start = Math.floor(Math.random() * (10 - 1 + 1)) + 1;
advanceSpinner(start);
setTimeout(function () {
stop = true;
}, 4000);
//setTimeout(function () {checkWin();}, 4050);
}
function advanceSpinner(i) {
i = i || 1;
if (!stop) {
if(i > 10)i = 1;
console.log("img/board new/" + i + ".png");
document.getElementById("spinner").src = "img/board new/" + i + ".png";
spinResult = i;
setTimeout(function () {
advanceSpinner(i + 1);
}, 50);
}
var spinResult;
var gameOver = false;
var oldHTML;
var newHTML;
var rollResult;
var start;
function setValue() {
rollResult = location.search.substr(location.search.indexOf("=") + 1);
//document.getElementById('own').innerHTML = "You own " + rollResult + " piece(s) on the board.";
}
}
function checkWin() {
/*
if (rollResult == 1 && spinResult == 1) {
alert("Win");
} else if (rollResult == 2 && spinResult == 1) {
alert("Win");
} else if (rollResult == 2 && spinResult == 3) {
alert("Win");
} else if (rollResult == 3 && spinResult == 1) {
alert("Win");
} else if (rollResult == 3 && spinResult == 3) {
alert("Win");
} else if (rollResult == 3 && spinResult == 6) {
alert("Win");
} else if (rollResult == 4 && spinResult == 1) {
alert("Win");
} else if (rollResult == 4 && spinResult == 3) {
alert("Win");
} else if (rollResult == 4 && spinResult == 6) {
alert("Win");
} else if (rollResult == 4 && spinResult == 8) {
alert("Win");
}
*/
}
var fadeEffect = function () {
return {
init: function (id, flag, target) {
this.elem = document.getElementById(id);
clearInterval(this.elem.si);
this.target = target ? target : flag ? 100 : 0;
this.flag = flag || -1;
this.alpha = this.elem.style.opacity ? parseFloat(this.elem.style.opacity) * 100 : 0;
this.elem.si = setInterval(function () {
fadeEffect.tween();
}, 20);
},
tween: function () {
if (this.alpha == this.target) {
clearInterval(this.elem.si);
} else {
var value = Math.round(this.alpha + ((this.target - this.alpha) * 0.05)) + (1 * this.flag);
this.elem.style.opacity = value / 100;
this.elem.style.filter = 'alpha(opacity=' + value + ')';
this.alpha = value;
}
}
};
}();