使用Javascript的图像加载触发器
Images onload trigger using Javascript
我正在为一个网站构建一个网页,该网站将用作心理实验(从用户那里收集数据)。
页面应按以下顺序操作:
1.显示加号图像250毫秒。
2.显示图像阵列中的图像750毫秒。
3.显示一个包含用户应该回答的问题的表单,依此类推
我需要在尚未完成的时候迭代这些步骤,以显示数组中的所有图像
我有HTML代码:
<img src="http://brain.netii.net/images/Blank.png" id="pictures" name="pictures"/>
<form id="inputs" action="#" class="validate" method="post">
<label class="quesLabel" for="assoInput1">First association:</label>
<div>
<input required pattern="([a-z]*|[A-Z]*|[ 't]*)*" type="text" id="assoInput1" name="asso1" title="please enter first association" autocomplete="off" value="">
</div>
<br>
<input type="submit" value="Submit">
</form>
CSS代码我有:
#container
{
width: 960px;
padding: 0;
margin: 0 auto;
}
#pictures
{
width: 960px;
height: 720px;
padding: 0;
}
#inputs
{
width: 960px;
height: 720px;
margin: 100px 50px;
display: none;
}
JS代码我有:
window.onload = function()
{
preloader();
pictureImg = document.getElementById("pictures");
inputForm = document.getElementById("inputs");
inputForm.onsubmit = function()
{
//alert('Form: onsubmit func');
LoadPlus();
};
LoadPlus();
//LoadImage();
};
function LoadPlus()
{
inputForm.style.display="none";
//stop condition:
if (imagesArr.length <= 0) //hope that here is no error here
{
//alert("All images loaded");
window.location.href = "start_experiment.html";
}
var url = plus;
var img = new Image();
img.src = url;
img.onload = function()
{
//alert("Plus loaded.");
setTimeout(LoadImage, 250);
};
pictureImg.src = img.src;
pictureImg.style.display="block";
}
function LoadImage()
{
var randomNum;
if (imagesArr.length > 0)
{
randomNum = Math.floor(Math.random() * imagesArr.length);
console.log(imagesArr[randomNum]);
}
else
{
alert ("ERROR: out of array boundaries!");
}
var url = "http://brain.netii.net/images/Practice" + imagesArr[randomNum] +".png";
imagesArr.splice(randomNum, 1);
var img = new Image();
img.src = url;
img.onload = function()
{
//alert("image #" + randomNum + " loaded, loading next..");
setTimeout(LoadForm, 750);
};
pictureImg.src = img.src;
}
function LoadForm()
{
//blank only pictures div
inputForm.reset();
inputForm.style.display="block";
pictureImg.style.display="none";
//alert('Form loaded');
}
在这里你可以看到页面。
我的问题从提交表单开始,onload触发器加载加号图像两次,而不是一次(有时第一个加号会显示很短的时间,有时甚至会显示加号后面的图片)。
有没有其他方法可以实现这一点?有人能帮我吗?
谢谢你,Max
我在这里的建议旨在以编程的方式表达您的每个请求。标记、样式和脚本一直保持简单。它不是为了优化或速度而编写的,而是作为现有代码的替代产品。请说明您认为对您的用例有用和适用的内容。
通过将所有图像(包括加号图像)放置在标记中,您可以根据需要轻松地引用它们,而无需在LoadImage()
和LoadPlus()
中重复不必要的.onload()
调用。
onload触发器不应该加载更多的图像,因为它发生了。。。
当所有内容(例如图像)也已被加载时。
<!-- index.html -->
<!-- Loads but hides all images from appearing onload. -->
<head>
<style> img { display: none; } </style>
</head>
<body>
<img id="plus" src="http://brain.netii.net/images/Plus.png">
<img id="picture" src="http://brain.netii.net/images/Practice7.png">
<form id="superform">
<label for="first">First association:</label>
<input type="text" name="name">
<label for="second">Second association:</label>
<input type="text" name="name">
<label for="third">Third association:</label>
<input type="text" name="name">
<input type="submit" value="Submit" id="submit">
</form>
<script>
// `window.onload` only runs when all content from the DOM (example: images) has been loaded
window.onload = function () {
var submit = document.getElementById('submit');
// Set an event listener anytime the submit is clicked
submit.addEventListener('click', function (event) {
// Assuming the method is defined, this prevents an actual "submission" from occuring
if (event.preventDefault) {
event.preventDefault();
}
// Immediately hide the form and show the plus symbol
document.getElementById('superform').style.display = 'none';
document.getElementById('plus').style.display = 'block';
// Run this timer 250ms after the click event occurs
window.setTimeout(function () {
// Immediately hide the plus but show the picture
document.getElementById('plus').style.display = 'none';
document.getElementById('picture').style.display = 'block';
// Run this timer 1000ms (1 second) after the click event occurs
window.setTimeout(function () {
// Immediately hide the picture but show the form again.
document.getElementById('picture').style.display = 'block';
document.getElementById('superform').style.display = 'block';
}, 1000); // 2nd timer ends.
}, 250); // 1st timer ends.
}, false); // end of `submit` event listener.
} // end of `onload` callback definition.
</script>
</body>
相关文章:
- 如何使用url加载程序在webpack中导入多个图像
- 如何在生成下载文件时显示加载动画
- 有没有任何方法可以将控制器从文件加载到ui路由器$stateProvider中
- JS触发器值检查在文档加载后添加到页面的元素在加载时更改AND
- 从 blob 加载视频预览时的 Jquery 触发器函数
- 触发器不会'不处理通过ajax加载的内容
- jQuery - 触发器功能使我的页面不停地重新加载
- jQuery:触发器不会在加载了 .load() 的元素上触发
- 更新失败,无法加载触发器.错误为“未捕获” 您必须使用 Parse.initialize 指定密钥
- 使用Javascript的图像加载触发器
- jquery触发器在动态加载新内容后不起作用
- Lightbox2页面加载触发器
- 触发器只工作在点击,但不加载时没有它
- 对每个图像使用Jquery异步图像加载器(JAIL)并将data-src属性作为触发器是明智的吗?
- 单击由ajax加载的锚上的触发器
- 如何检测响应滚动触发器加载的新内容?
- Jquery mouseenter触发器加载
- DOM加载后定义的触发器事件-JIRA打开新的问题模式
- 触发器以编程方式选择加载方法
- Fancytree触发器以编程方式加载