简体   繁体   English

使用JS移动div内的div

[英]Move divs that are inside a div using JS

Im trying to make a "spinner" much like on csgoempire.com, but I'm having some issues (I'm almost totally new to JS and I'm not good at HTML). 我试图像在csgoempire.com上那样制作一个“旋转器”,但是我遇到了一些问题(我对JS几乎完全陌生,并且我不太擅长HTML)。 I have tried a bunch of stuff like making one div for each picture, and having a separate JS function for each, but after some thinking I realised that it wouldn't be efficient at all and it would make it hard to change stuff later. 我已经尝试了很多方法,例如为每张图片制作一个div,并为每个图片都有一个单独的JS函数,但是经过一番思考,我意识到它根本没有效率,并且以后很难更改内容。 I then decided to make separate divs for each photo, but placing them all in another div which would be moved by the JS function. 然后,我决定为每张照片制作单独的div,但将它们全部放置在另一个由JS函数移动的div中。 This would make it a lot more efficient than before, but I can't seem to get it to work, and there are (hopefully) better ways of doing it. 这将使它比以前更加有效,但是我似乎无法使其正常运行,并且(有希望)有更好的方法来实现它。 Id really appreciate some tips on how I can make it more efficient, and a fix for my current problem :) 我真的很感谢我如何提高效率的一些技巧,以及针对当前问题的解决方法:)

Here is the code in all its glory: 这是所有代码的荣耀:

 console.log("Connected to server.") function spin() { console.log("Starting Spin."); var boxElement = document.getElementById('allBoxes'); var pos = 900; var id = id; setInterval(frame, 1); function frame() { if (pos == 100) { clearInterval(id) } else { pos--; boxElement.style.left = pos + 'px'; } } } 
  <div id="allBoxes"> <div id="box2" style="position:absolute; left:712pt; top:150pt; width:50px; height:50px;"><img src="http://www.theaa.ie/winterhub/wp-content/uploads/2015/12/AA-logo-50x50.png" /></div> <div id="box1" style="position:absolute; left:675pt; top:150pt; width:50px; height:50px;"><img src="data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAADIAAAAyCAMAAAAp4XiDAAAALVBMVEXGAA4fHx////98fHz8/Pzy8vItLS1xcXFnZ2eIiIhdXV2PCxQtHR64Aw9XFRmYSViLAAAAnklEQVRIie2UyQ6DMAxEyZAd2v//3JolAuWQdKyqAok5WDn4ZRQvGYZHj0SmqTsivs71PSS7KBEwJUaX24h3sOGMBAvn2y7RAuOBjICNvbcEAKkgckDoV0wuRkGwWH5R5HRGUr/I2FWff4toun9V5G8VY1upGRh+LPnh16wYv8ia70LR/SsilaaZSN71etNIMaKQzYhDViMWESMauZE+q+cGCDwyhBUAAAAASUVORK5CYII=" /></div> </div> <button onclick="spin()"> Click Me! </button> 

This was made by just googling around, and testing stuff, so I'm sure its insanely bad and inefficient :P (I dont even know if its possible to do what I want to do this way.. ) 这是通过四处搜查并测试东西来完成的,因此,我确定它是疯狂的糟糕和低效的:P(我什至不知道是否有可能这样做。)

The issue is that the spinner won't spin anymore.. It does when I move them separately, but not when they are both inside a div, and I move said div. 问题是旋转器不再旋转了。当我分别移动它们时,它会旋转,但是当它们都在div内时不会,并且我说div时,它会旋转。 Is there a way to move multiple divs by only moving one of them? 是否可以通过仅移动其中一个来移动多个div? Can you recommend another way of doing this if you know a better way that is more.. dynamic? 如果您知道更好的..动态方法,可以推荐另一种方法吗?

The end goal is to fetch each user's image from steam and use those images in the "spinner", make an arrow that points to the middle that will output the username of the person it lands on and have the "spinner" start once a timer reaches 0, but for now I'm just trying to learn, and I figured that the best way to learn is to have a project to work on.. (may have picked a bit of a difficult project though..) 最终目标是从蒸汽中获取每个用户的图像,并在“旋转器”中使用这些图像,使箭头指向中间,该箭头将输出其落地人员的用户名,并在计时器启动后启动“旋转器”达到0,但现在我只是想学习,我认为最好的学习方法是有一个项目要进行..(虽然可能会选择一个困难的项目..)

You didn't save the interval so you can stop it: 您没有保存间隔,因此可以将其停止:

...
var pos = 900;
var id = setInterval(frame, 1);
function frame() {
...

Made a jsFiddle with the solution: https://jsfiddle.net/bfrhw7rf/1/ 使用解决方案制作了jsFiddle: https ://jsfiddle.net/bfrhw7rf/1/


Consider using css to create the spinner, like here: https://projects.lukehaas.me/css-loaders/ 考虑使用css创建微调器,例如: https : //projects.lukehaas.me/css-loaders/

Since you are using a very short interval you are using up the single thread that runs your javascript, with css you are not blocking the rest of your code. 由于您使用的间隔很短,因此您用完了运行javascript的单线程,而使用CSS时,您不会阻塞其余的代码。

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

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