【发布时间】:2019-10-30 20:41:54
【问题描述】:
我想要使用 HTML 和 CSS 将六张图片排列成一个圆圈。圆圈不应该旋转或类似的东西 - 有人对如何做到这一点有任何想法吗?
【问题讨论】:
-
@SteveRobbins JS 对于这个问题确实不是必需的。这可以在纯 CSS 中完成
我想要使用 HTML 和 CSS 将六张图片排列成一个圆圈。圆圈不应该旋转或类似的东西 - 有人对如何做到这一点有任何想法吗?
【问题讨论】:
我没有适合您的纯 CSS 解决方案,但这可能会对您有所帮助。使用 jQuery 的解决方案。即使您有超过 6 张图片,这也会对您有所帮助。
Here 是现场演示。
【讨论】:
您可以使用相对定位来实现:
<div id='box1'> </div>
<div id='box2'> </div>
<div id='box3'> </div>
<div id='box4'> </div>
<div id='box5'> </div>
<div id='box6'> </div>
div{
position:absolute;
width:20px;
height:20px;
}
#box1
{
border:1px solid red;
right:50%;
}
#box2
{
border:1px solid purple;
top:30%;
right:10%;
}
#box3
{
border:1px solid orange;
top:30%;
left:10%;
}
#box4
{
border:1px solid red;
top:60%;
right:10%;
}
#box5
{
border: 1px solid green;
top:60%;
left:10%
}
#box6
{
border:1px solid red;
top:90%;
right:50%;
}
Js Fiddle 在这里:http://jsfiddle.net/E4j2R/
【讨论】:
这是笔:https://codepen.io/HugoGiraudel/pen/Bigqr Mixin 将项目放在一个圆圈上
允许定位任何类型的直接子代
<ul class='circle-container'>
<li><img src='http://lorempixel.com/100/100/city'></li>
<li><img src='http://lorempixel.com/100/100/nature'></li>
<li><img src='http://lorempixel.com/100/100/abstract'></li>
<li><img src='http://lorempixel.com/100/100/abstract'></li>
<li><img src='http://lorempixel.com/100/100/cats'></li>
<li><img src='http://lorempixel.com/100/100/food'></li>
<li><img src='http://lorempixel.com/100/100/animals'></li>
<li><img src='http://lorempixel.com/100/100/business'></li>
<li><img src='http://lorempixel.com/100/100/people'></li>
</ul>
/// @param {Integer} $nb-items - Number or items
/// @param {Length} $circle-size - Container size
/// @param {Length} $item-size - Item size
/// @param {String | false} $class-for-IE - Base class name for old IE
@mixin distribute-on-circle(
$nb-items,
$circle-size,
$item-size,
$class-for-IE: false
) {
$half-item: ($item-size / 2);
$half-parent: ($circle-size / 2);
position: relative; /* 1 */
width: $circle-size;
height: $circle-size;
padding: 0;
border-radius: 50%;
list-style: none; /* 2 */
box-sizing: content-box; /* 3 */
> * { /* 4 */
display: block;
position: absolute;
top: 50%;
left: 50%;
width: $item-size;
height: $item-size;
margin: -$half-item;
}
$angle: (360 / $nb-items);
$rot: 0;
@for $i from 1 through $nb-items {
@if not $class-for-IE {
> :nth-of-type(#{$i}) {
transform: rotate($rot * 1deg) translate($half-parent) rotate($rot * -1deg);
}
} @else {
> .#{$class-for-IE}#{$i} {
// If CSS transforms are not supported
$mt: sin($rot * pi() / 180) * $half-parent - $half-item;
$ml: cos($rot * pi() / 180) * $half-parent - $half-item;
margin: $mt 0 0 $ml;
}
}
$rot: ($rot + $angle);
}
}
.circle-container {
@include distribute-on-circle(8, 20em, 6em, false);
margin: 5em auto 0;
border: solid 5px tomato;
}
.circle-container img {
display: block;
width: 100%;
border-radius: 50%;
filter: grayscale(100%);
&:hover {
filter: grayscale(0);
}
}
它允许您自定义项目的数量.. 还支持 IE :) 很酷的解决方案
【讨论】:
Here@blasteralfred Ψ 解决方案的 jQuery 免费分支
function arrangeImagesInCircle(props = {}) {
const getWidthHeight = element => {
const { width, height } = element.getBoundingClientRect();
return [width, height];
}
const radius = props.radius || 200;
const imageSelector = props.imageSelector || 'img';
const container = props.container;
const [containerWidth, containerHeight] = getWidthHeight(container);
return imageSelector => {
const fields = container.querySelectorAll(imageSelector);
const step = (2 * Math.PI) / fields.length;
var angle = 0
fields.forEach(field => {
const [fieldWidth, fieldHeight] = getWidthHeight(field);
const x = Math.round(containerWidth / 2 + radius * Math.cos(angle) - fieldWidth / 2);
const y = Math.round(containerHeight / 2 + radius * Math.sin(angle) - fieldHeight / 2);
if (window.console) console.log(x, y);
field.style.left = x + 'px';
field.style.top = y + 'px';
angle += step;
});
}
}
【讨论】: