Javascript:防止随机大小的图像重叠
我有一个画廊,其中的图像经受着粗暴的处理:它们在容器中随机调整大小和位置。而且它们是圆形的,并且通过单击鼠标来改变大小。请放心,它实际上比听起来简单得多。
好的,这是我的意思的一个(可爱的)示例: http: //dl.dropbox.com/u/5550635/Test%20rounded%20gallery/example2.htm
我的问题非常明显例如,图像大部分时间都是重叠的。
同样的情况,当您单击其中一张图像时,其他图像会向下移动,并且应该形成一条不重叠的线,它们之间可能至少有 5 个像素的边距。
您可以在示例页面上看到注释的代码。
CSS 非常简单:
.project { //class given to the div containing the images
border-radius: 100px;
width: 200px;
height: 200px;
overflow: hidden;
}
#space { //container
width: 550px;
height: 700px;
background: #ccc;
border: 2px solid blue;
}
.parent_project { //parent of the .project div, position absolutely (to fix a problem between position and border-radius)
position: absolute;
}
我在网络上查找了一种 Javascript 解决方案来防止图像重叠,但我找不到适合我的具体情况的解决方案。 如果您看看我的问题并分享一些您的科学知识,我将非常感激。谢谢!
I have a gallery in which images endure a rough treatment: they are sized and positioned randomly in a container. Also they are rounded and change size over a mouseclick.. be reassured, it's actually much simpler than it sounds.
Okay, here's a (cute) example of what I mean: http://dl.dropbox.com/u/5550635/Test%20rounded%20gallery/example2.htm
My problem is pretty obvious with the example, images are overlapping most of the time.
Same thing for when you click on one of the images, the other ones go down, and should form a line without overlapping, with maybe a minimum 5 pixels margin between them.
You can see the commented code on the example page.
The CSS is pretty simple:
.project { //class given to the div containing the images
border-radius: 100px;
width: 200px;
height: 200px;
overflow: hidden;
}
#space { //container
width: 550px;
height: 700px;
background: #ccc;
border: 2px solid blue;
}
.parent_project { //parent of the .project div, position absolutely (to fix a problem between position and border-radius)
position: absolute;
}
I've looked around the web for a Javascript solution to prevent overlapping images, but I couldn't find one that fits my specificities.
If you just take a look at my problem and share a bit of your science, i'd be very grateful. Thanks!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您需要跟踪圆的中点和半径。 http://jsfiddle.net/pimvdb/5L9FN/
然后,要检查重叠,您可以计算计算每两个中点之间的距离,并检查它是否小于两个半径:
You'd need to keep track of the circles' midpoints and radii. http://jsfiddle.net/pimvdb/5L9FN/
Then, to check for overlapping you could calculate the distance between each two midpoints and check whether it is smaller than the two radii: