简体   繁体   English

我不知道如何与交通信号灯同步播放动画

[英]I don't know how to loop my animation in sync with traffic lights

I have this code below I have made and I am attempting to run the animation when the traffic light is on green/index=2. 我在下面制作了此代码,并且当交通信号灯在green / index = 2上时尝试运行动画。 i have literally tried everything i can so please any boffins out there show me how to loop these two parts of the code in sync. 我已经尽力尝试了所有可以做到的事情,所以请所有棺材向我展示如何循环同步这两部分代码。

 <!DOCTYPE html> <html> <body> <h1>The best GCSE traffic lights sequence any examiner has ever seen!</h1> <img id="light" src="Traff 1.jpg"> <style> #container { width: 600px; height: 475px; position: absolute; background: #000; } #animate { width: 300px; height: 170px; position: absolute; background: url(car.jpg); } </style> <div id ="container"> <div id ="animate"></div> </div> <script> var list = [ "Traff 1.jpg", "traff 2.jpg", "traff 3.jpg", "traff 4.jpg" ]; var index = 0; (function nextlight() { setInterval(function(){ index = index + 1; if (index == 4) index = 0; var image = document.getElementById('light'); image.src=list[index]; }, 3000); })() </script> <script> (function myMove() { var elem = document.getElementById("animate"); var pos = 0; var id = setInterval(frame,10); function frame() { if (pos == 300) { pos = 0; } else { pos++; elem.style.top = pos + 'px'; elem.style.left = pos + 'px'; } } })() </script> </body> </html> 

It's simple. 这很简单。 You just need a check in the function frame so that it moves the position only if the index is 2 . 您只需要在功能frame进行检查,以便仅在index2的情况下才移动位置。

function frame() {
    if (index !== 2) {
        return;
    }
    ...
}

Working example (used colors instead of images): 工作示例(使用颜色代替图像):

 <!DOCTYPE html> <html> <body> <h1>The best GCSE traffic lights sequence any examiner has ever seen!</h1> <!-- <img id="light" src="Traff 1.jpg"> --> <div id="light">Traff Light</div> <style> #container { margin: 30px 0; width: 900px; height: 500px; position: absolute; background: #000; } #animate { width: 300px; height: 170px; position: absolute; background: blue; /* background: url(car.jpg); */ } #light { background: red; border: 5px solid cyan; height: 50px; width: 100px; } </style> <div id="container"> <div id="animate"></div> </div> <script> var list = [ "Traff 1.jpg", "traff 2.jpg", "traff 3.jpg", "traff 4.jpg" ]; var tlight = ['red', 'yellow', 'green', 'grey']; var index = 0; (function nextlight() { setInterval(function() { index = index + 1; if (index == 4) index = 0; var image = document.getElementById('light'); //image.src = list[index]; image.style.background = tlight[index]; }, 3000); })(); </script> <script> (function myMove() { var elem = document.getElementById("animate"); var pos = 0; var id = setInterval(frame, 10); function frame() { if (index !== 2) { return; } if (pos == 300) { pos = 0; } else { pos++; elem.style.top = pos + 'px'; elem.style.left = pos + 'px'; } } })(); </script> </body> </html> 

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 我的for循环不起作用,我也不知道为什么 - my for loop doesn't work and I don't know why 想在我的代码中使用 for 循环,但我不知道怎么可能 - A want to use the for loop in my code, but I don't know how is it possible 我的作业中出现无限循环错误; 不知道如何修复 - Infinite Loop error in my assignment; don't know how to fix 我不知道如何解决成绩计算器功能 - I don't know how to fix my grade calculator function 我不知道如何解决我的反向数组/字符串 - I don't know how to fix my reverse array/string 如何在交通信号灯序列上切换灯? - How do I switch lights on a traffic light sequence? 我正在尝试学习链表,我不知道为什么我的 object 中有一个循环 - am trying to learn Linked list and i don't know why there is a loop in my object 我有一个无循环功能问题,我不知道如何或是否应该费心解决 - I have a no-loop-func issue that I don't know how or if I should bother solving 我如何才能使此幻灯片代码片段无限循环(我当前的代码无法正常工作,并且我不知道自己在哪里犯错误) - How can I make this slide code snippet loop infinetly ( my current code is not working and I don't know where I'm making mistakes) 我不想将一串数字格式化为 javascript 中的货币,但我不知道如何格式化我的字符串 - I wan't to format a string of numbers as a currency in javascript but i don't know how to format my string
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM