简体   繁体   English

当您按空格键div向上移动

[英]When you press space div moves up

I'm trying to do a tricky thing in Javascript, but it's not going well. 我正在尝试用Javascript做一件棘手的事情,但是进展并不顺利。 Basically it's like: I have 2 balls(divs), one of them is on top: 100px and the other ball is on top 500px ( not actual values, just random ones ), and what I want to achieve is, when I press Space, the ball that is on top 500px, goes up to the top: 100px, and the other one does the opposite ( goes from top: 100px, to top: 500px, but with a cycle condition, like having speed, like a "for" that gives + 2px (or more) to the div until she reaches top: 500px, and vice versa. 基本上是这样的:我有2个球(div),其中一个在上面:100px,另一个球在上面500px(不是实际值,只是随机值),当我按下Space时,我想要实现的是,位于顶部500像素的球上升到顶部:100像素,而另一个球则相反(从顶部:100像素到顶部:500像素,但是具有循环条件,例如具有速度,例如“ ”,直到div达到顶部为止,它就会为div加上+ 2px(或更多):500px,反之亦然。

I hope you understand what I'm trying to make here, a hand would be really appreciated. 希望您能理解我在此所做的工作,非常感谢您的帮助。

First you want to look up for events. 首先,您要查找事件。

Events are " things " that happen to HTML elements. 事件是发生在HTML元素上的“ 事物 ”。 When JavaScript is used in HTML pages, JavaScript can " react " on these events. 在HTML页面中使用JavaScript时,JavaScript可以对这些事件“ 做出反应 ”。 Found here . 这里找到。

Next thing you want to learn about is how to manipulate with DOM objects . 接下来要了解的是如何使用DOM对象进行操作 You can learn more about it here . 您可以在此处了解更多信息。

When you will know more about those two things, you will be ready to solve this problem, that you are asking this community to do it instead of you. 当您对这两件事有更多的了解时,就可以解决这个问题了,您要让这个社区代替您来做。

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM