javascript - 在 div 元素中循环图像

标签 javascript html

我是一名开始学习编码的初学者。我在一个有 4 层网页的网页上设计了一个神经科学相关的实验。我的第一层将是不变的。第二层在每一轮提问中显示不同的图像。第三层和第四层是在特定时间间隔和/或单击提交时显示的问题。我想知道如何每次显示不同的图像。准确地说,我可以使用 for 循环来完成吗!?

<html>

<head>
<Title> Experiment </title>
<link rel="stylesheet" type="text/css" href="displayfieldset.css">
</head>
<body>

<!-- Layer 1 of cross hair image -->
<div id="crosshair" style="background-color:black; position:absolute;     width:100%; height:100%; z-index:1; align:center">
<img src="crosshair.jpg" width="1350px" height="750px" >
</div> <!-- Layer 1 closed -->

<!-- Layer 2 of Images -->
<div id="piclayer" style="position:absolute ;width:98%; height:98%; z-    index:2; align:center; margin-left:0.5%; margin-top:0.5%">
    <img id="images" src="image1.jpg" style="width:1325px; height:720px; display:none">
</div> <!-- Layer 2 closed -->


<!-- Layer 3 Question 1 -->
<div id="questionone" style="z-index:3; position:absolute; display:none; margin-left: 180px">
<fieldset name="field1_1" id="field1_1">
<form name ="problem1_1" id="problem1_1"  >
  <b> Identify the problem shown in this image. </b>
  <br> 
  <br>
  <input type="text" name="answer1_1" id="answer1_1" maxlength="30" style="width: 400px">
  <br>
  <br>
  <input type="button" value="Submit" onclick="showdiv()"  >
</form>
</fieldset>
</div>

<!-- Layer 4 Question 2 -->
<div id="questiontwo" style=" position: absolute; z-index:5; align:center; display:none; margin-left: 180px">
<fieldset name="field1_2" id="field1_2" style="position:relative; align:center">
<form name ="problem1_2" id="problem1_2" >
  <b> Propose a solution to the problem. </b>
  <br> 
  <br>
  <input type="text" name="solution1_2" id="solution1_2" maxlength="30" style="height: 200px; width: 400px">
  <br>
  <br> 
  <br>
  <input type="button" value="Submit" onclick="hidediv()" >
  </form>
</fieldset>
</div>

<script>
  function showdiv()
  {
    document.getElementById('questiontwo').style.display = "block";
    document.getElementById('questionone').style.display = "none";
  }

  function hidediv()
  {
   document.getElementById('piclayer').style.display = 'none';
   document.getElementById('questionone').style.display = 'none';
   document.getElementById('questiontwo').style.display = 'none';
  }

  <!-- Time out for image -->
  setTimeout 
  ( function()
    {
      document.getElementById('images').style.display = 'block';
    }
    ,6000
  );

  <!-- Timeout for first question -->
  setTimeout 
  ( function()
   {
    document.getElementById('questionone').style.display = 'block';
   }
   ,12000
  );
</script>



</body>
</html>

最佳答案

根据您的编码,
“十字准线”图像,第 1 层始终显示在 z-index:1。
Layer-2 图像将在页面加载 6 秒后显示在 z-index:2。
Layer-3 div 将在页面加载 12 秒后显示在 z-index:3。
在第 3 层,有一个提交按钮。如果用户点击提交按钮,
第四层显示,第三层隐藏。
Layer-4 中再次有一个提交按钮,如果用户单击该按钮,所有图像将被隐藏。
那么,你想做什么?

是否要循环显示第 2 层的图像?如果是,这里是示例代码:

var x = 0;
function myFunction(){  
   
   var Layer2Images = document.querySelectorAll("img.images"); 
   if (x == Layer2Images.length)
      x=0;
   for (i = 0; i < Layer2Images.length; i++) {
    Layer2Images[i].style.display = 'none';
   }
   Layer2Images[x].style.display = 'block';
   x++;
}

setInterval(myFunction, 1000)
<!-- Layer 2 of Images -->
<div id="piclayer" style="position:absolute ;width:98%; height:98%; z-index:2; align:center; margin-left:0.5%; margin-top:0.5%">
   <img class="images" src="https://pixabay.com/static/uploads/photo/2012/05/29/00/43/car-49278_1280.jpg" style="width:400px;height:300px; display:none;">
   <img class="images" src="https://static.pexels.com/photos/24353/pexels-photo-large.jpg" style="width:400px; height:300px; display:none;">
   <img class="images" src="https://static.pexels.com/photos/16155/pexels-photo-large.jpg" style="width:400px; height:300px; display:none;">
</div> <!-- Layer 2 closed -->

关于javascript - 在 div 元素中循环图像,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40210122/

相关文章:

html - Django-模板 : Tap list item and make a detail page

javascript - 按描述过滤 JSON 对象 Express JS/Underscore JS

javascript - 使用 jQuery 隐藏然后追加数据

php - 下拉搜索菜单列表没有放在应有的位置

javascript - 如何在我的网站上使用 Mathquill

php - 文件类型输入不在循环中

javascript - for循环中的异步变量

php - 在html链接中调用javascript函数

java - 如何在java中获取XHTML文件对象以及如何使用它?

css - 如何使超链接更改溢出中的文本 :hidden div without jumping to the div