- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我正在为 children 做一个数学测验。我有一个功能,可以在页面加载时旋转一组图像。我的目标是,当答案正确时,它将在一组不同的图像中旋转一次,然后再恢复到原始图像组。我已经设法让它变成第二组,但我一直试图让它在恢复到原始图像之前只旋转一次这些图像。我尝试了几件事,包括:1. 使用 if 语句在两个数组之间进行更改,但这完全没有响应,根本没有更改图片。2. 我创建了一个新函数来更改第一个数组的实际内容,但在它遍历图像一次后我无法将其更改回来。
JavaScript 的代码如下(抱歉,我尝试了所有不同的东西,结果很乱):
// Starting animation
window.addEventListener("load", normalAnimation);
var images = ['01.png', '02.png', '01.png', '02.png', '03.png', '01.png', '02.png', '01.png','04.png'];
var images2 = ['catchfish01.png', 'catchfish02.png', 'catchfish03.png', 'catchfish04.png', 'catchfish05.png', 'catchfish06.png','catchfish07.png'];
var imageNumber = 0;
var pic1 = document.getElementById('pic1');
var normalAnimation = function() {
//Here I attempted to change the images used but only initiates the code in the else part. The actual change of images used is done with the catchFish function below.
var feedBack = document.getElementById("feedBack");
if(feedBack.textContent == "Well Done!"){
setInterval(function () {
pic1.src = images2[imageNumber];
imageNumber++;
if (imageNumber > images2.length-1) {
pic1.src = images[imageNumber];
imageNumber++;}
},2000);
}
else {setInterval(function () {
pic1.src = images[imageNumber];
imageNumber++;
if (imageNumber > images.length -1) {
imageNumber = 0;}
},2000);
}
}
requestAnimationFrame(normalAnimation);
// Making the question
document.getElementById("button1").addEventListener("click", question);
var plusMinus = document.getElementById("plusMinus");
var counter = 0;
function question(){
var startButton = document.getElementById("button1");
var number1 = document.getElementById("num1");
var number2 = document.getElementById("num2");
var decider = Math.random();
var answer = document.getElementById("answer");
counter ++;
if(decider<0.5){
plusMinus.textContent = "+"
}
else{plusMinus.textContent = "-"};
startButton.textContent = "Round" + " " + counter;
number1.textContent = Math.floor(Math.random()*11);
number2.textContent = Math.floor(Math.random()*11);
equals.textContent = "=";
if(plusMinus.textContent == "-" && parseInt(number2.textContent) > parseInt(number1.textContent)) {
console.log('swapped');
var a = number2.textContent;
number2.textContent = number1.textContent;
number1.textContent = a;
};
if(startButton.textContent == "Round" + " " + 11){
startButton.textContent = "Start Again";
counter = 0;
number1.textContent = " ";
plusMinus.textContent = " ";
number2.textContent = " ";
}
answer.textContent = " ";
};
// Answering the question
document.getElementById("button2").addEventListener("click", answer);
var totalScore = 0;
function answer(){
var num1 = parseInt(document.getElementById("num1").textContent, 10);
var num2 = parseInt(document.getElementById("num2").textContent, 10);
var answer = parseInt(document.getElementById("answer").value, 10);
var feedBack = document.getElementById("feedBack");
var scoreReport = document.getElementById("score");
if (plusMinus.textContent == "+"){
if(answer == num1 + num2) {
feedBack.textContent = "Well Done!";
totalScore = totalScore + 10;
if(feedBack.textContent== "Well Done!"){
setTimeout(function () {feedBack.textContent = " ";
}, 2000);
}
//Used to call the function that changes the array contents
catchFish();
} else {
feedBack.textContent = "Try again!";
if(feedBack.textContent == "Try Again!"){
setTimeout(function () {feedBack.textContent = " "
}, 2000);
}
}
} else {
if(answer == num1 - num2){
feedBack.textContent = "Well Done!";
if(feedBack.textContent== "Well Done!"){
setTimeout(function () {feedBack.textContent = " ";
}, 2000);}
totalScore = totalScore +10;
} else {
feedBack.textContent = "Try Again!";
if(feedBack.textContent == "Try Again!"){
setTimeout(function () {feedBack.textContent = " "
}, 2000);
}
}
}
scoreReport.textContent = "Score:" + totalScore;
};
//This function changes the contents of the array to the second set of images
function catchFish() {
imageNumber = 0;
images = ['catchfish01.png', 'catchfish02.png', 'catchfish03.png', 'catchfish04.png', 'catchfish05.png', 'catchfish06.png','catchfish07.png'];
if(pic1.src == 'catchfish07.png'){
images = ['01.png', '02.png', '01.png', '02.png', '03.png', '01.png', '02.png', '01.png','04.png'];
}
}
在此先感谢您的帮助。
最佳答案
可能最干净的解决方案是维护一个 queue接下来要显示的图像。在每个时间间隔,你 shift第一个图像离开队列,如果队列为空,则将一组新图像添加到队列中:
var queue = [];
setInterval( function () {
if (queue.length < 1) {
queue = ['01.png', '02.png', '01.png', '02.png', '03.png', '01.png', '02.png', '01.png', '04.png'];
}
pic1.src = queue.shift();
}, 2000 );
然后,要临时切换到一组不同的图像,您只需替换队列的内容即可:
function catchFish() {
queue = ['catchfish01.png', 'catchfish02.png', 'catchfish03.png', 'catchfish04.png', 'catchfish05.png', 'catchfish06.png', 'catchfish07.png'];
}
现在 setInterval()
例程将遍历队列的新内容,一旦到达末尾,它将返回到原始图像旋转。
附言。提示:如果您更愿意在函数外定义图像列表,您可以使用 slice()
将它们复制到队列中,如下所示:
var images = ['01.png', '02.png', '01.png', '02.png', '03.png', '01.png', '02.png', '01.png', '04.png'];
var images2 = ['catchfish01.png', 'catchfish02.png', 'catchfish03.png', 'catchfish04.png', 'catchfish05.png', 'catchfish06.png', 'catchfish07.png'];
var queue = [];
setInterval( function () {
if (queue.length < 1) queue = images.slice();
pic1.src = queue.shift();
}, 2000 );
function catchFish() {
queue = images2.slice();
}
(如果您只是执行 queue = images;
而没有使用 .slice()
,这将使 queue
成为 的别名images
,所以 queue.shift()
最终会修改原始的 images
数组。使用 .slice()
会给出你是 images
数组的副本,它可以安全地分配给 queue
。有关如何操作 JavaScript 数组的更多示例,请参阅 Array class documentation on MDN。)
关于javascript - vanilla javascript - 在通过函数旋转的两个数组之间切换,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34473852/
它是链接中的框架 https://vanillaforums.com/en/software/ ? 解释是什么?有哪些链接? 最佳答案 Vanilla PHP 就是 PHP 本身。 一般来说,术语va
如何使用 vanilla javascript 将按键模拟为输入? 我已经在 SO 和其他地方测试了所有可能的答案,但它在 Chrome 或 Firefox 上不起作用。 例如,假设我们有一个表单:
This answer作者 Jan Burse 展示了 Prolog 中元解释器的最简单实现之一: solve(true) :- !. solve((A,B)) :- !, solve(A), sol
赏金 我在谷歌上搜索它以找到类似的库,但我没有找到......如果有人知道你可以给出答案(仅限 Vanilla js 库和麻省理工学院许可证)...... 否则不要更改 html 结构...修复我的代
我是Rust的新手,试图了解基本的目录遍历。我发现的几乎所有示例都利用了walkdir或glob库,这些库我都取得了很好的成功。但是,我现在正尝试仅使用std lib来执行此操作。 标准lib文档中有
我正在尝试通过事件监听器只需单击一次即可触发函数。但如果我在一次单击后不执行 F5 触发操作,则会在两次单击(第一次)后发生。 代码: HTML ....
我的单页网站在 vanilla js 中有一个平滑的滚动,我尝试在没有 jquery 的情况下实现它,我想添加一个像 cubic bezier 这样的计时函数。有没有办法在 javascript 中做
对于一个小项目,我想制作一个基于浏览器引擎检测浏览器的小型库: var engine = { opera: function(){ var ua = navigator.userAgent.t
嘿,我想弄清楚如何让我的脚本在加载元素时等待执行它的一部分,但目前它主要只是传递它,而不是等待它。在 vanilla js 中最好的方法是什么? 这是代码 this.heading = documen
我正在复制一个 simon 游戏原型(prototype),我正在尝试在 vanilla js 中为按钮闪烁编写一个函数。 目前我有以下功能: function blinkColor(color) {
我正在构建一个类似于 eclipse 刻草图的交互式网格。我已经设置了网格,现在正在尝试设置“悬停”效果,以便当鼠标经过网格 div 时改变颜色,从而像钢笔一样在网格中留下(像素化)轨迹。但我希望颜色
我正在为我在大学的一个科目开发一个元素,包括用纯 javascript 做一个下拉列表过滤器,以过滤 html/css 中的图像网格。导致我出现问题的是这个过滤器需要同时处理 3 个下拉列表。例如,如
我有一个带有主要 div 的简单旋转木马。在主 div 中有一个宽度为 6000px 的 ul,每当我单击向右或向左箭头时,它就会滑动。 我将事件监听器附加到两个箭头,所以当我单击左箭头时,我将 30
我正在研究构造 Vanilla 网络组件。我以前使用过 Polymer,我喜欢这样一个事实,即您可以在一个文件中为您的组件提供模板、样式和 JavaScript。如果可能的话,我想用“ Vanilla
我找到了这个粘性 div 代码段并对其进行了修改,使其在 div 到达页面顶部时看起来粘在窗口底部。我只是好奇是否有某种方法可以抵消底部。这是代码:- let Sticky = (function()
所以我遇到了这个问题,我需要根据一些变量为复选框设置一个值。 问题是我在将要使用的 HTML 上遇到了下一个命名约定: 要决定选择哪个输入,我通常会这样做: document.getEleme
我正在练习 VHDL,我有一个关于不需要过程的“简单”语句的基本问题。 我想知道两者的区别 c <= a and b; 语句不在进程内,只是在架构开始之后编写,并且 process(a,b) begi
在客户处,我们的一个 WPF 应用程序开始挂起。在尝试使用最小的工作示例重现问题时,我发现即使是最基本的(非平凡的)WPF 应用程序也会卡在该机器上。 示例 A : 在 Visual Studio 2
我按照教程制作了一个使用 localstorage 的购物车。现在我正在尝试对其进行调整以适应我自己的需要,以扩展我对 JavaScript 的知识并给自己更多的练习。讲师为每个单独的项目总计做了一个
我需要修改 Vanilla 元解释器,以便进行有限深度的搜索。我正在使用以下代码来测试我的解决方案: value(wire1,1). connected(wire2, wire1). connecte
我是一名优秀的程序员,十分优秀!