简体   繁体   English

以绝对位置居中图像(上,左)

[英]Centering an image with absolute position (top, left)

I am using parametric equations to position images around a circle ( How do I calculate a point on a circle's circumference? ) 我正在使用参数方程式将图像定位在圆周围( 如何计算圆的圆周上的点?

I am doing this in less, using simple math: 我使用简单的数学运算就更少了:

// x,y position on circumference:
// x = a + r * cos(t)
// y = b + r * sin(t)

.position {
left: @a + ( @r *  cos(@t) );
top: @b + ( @r *  sin(@t) );
}

The trouble I have is that this positions the image by the top left corner, not the center of the image, so doesn't account for the visual offset of height and width this has. 我遇到的麻烦是,这会将图像定位在图像的左上角而不是图像的中心,因此无法解决其具有的高度和宽度的视觉偏移。 Trying to adjust by height/2, width/2 doesn't work as the angle of each image is different. 尝试按高度/ 2进行调整,宽度/ 2不起作用,因为每个图像的角度都不同。

Is there an easy way to position an image this way so it is centred on x,y? 有没有一种简单的方法可以以这种方式定位图像,使其以x,y为中心?

.position {
    left: @a + ( @r *  cos(@t) );
    top: @b + ( @r *  sin(@t) );

    margin-left: -@r;
    margin-top: -@r;
}

Instead of calculations you simply could have done this though: 不用计算就可以做到:

top:50%;
left:50%; 
position:absolute; 

margin-left:-@r;
margin-top:-@r;

FIDDLE DEMO 现场演示

Subtracting Out Half-Width/Height Does Work 减去一半的宽度/高度确实有效

You stated you tried half-width/height calculations in your attempt to center the image on the circumference of a circle at some angular reference (as I understand your question). 您说过尝试将宽度/高度的一半计算出来, 以使图像在某个角度参考的圆圆周上居中 (据我所知,这是您的问题)。 It seems that should and does work. 似乎应该而且确实可行。 Check out this... 看看这个...

Example Fiddle 小提琴的例子

The circle and image colors are for visual reference, but the image positioning is done by: 圆圈和图像的颜色仅供参考,但是图像的定位方法是:

LESS

//define circle position
@r: 100px;
@x: 175px;
@y: 150px;
.setImage(@w, @h, @a) {
    //@a is angle from HORIZONTAL but moves clockwise 
    //(based on radians unless units are given)
    width: @w;
    height: @h;
    left: (@x + ( @r *  cos(@a) ) - (@w / 2));
    top: (@y + ( @r *  sin(@a) ) - (@h / 2));
}
.test1 {
  .setImage(40px, 40px, -35deg);
}
.test2 {
  .setImage(60px, 30px, -80deg);
}
.test3 {
  .setImage(90px, 20px, -150deg);
}
.test4 {
  .setImage(40px, 70px, -240deg);
}
.test5 {
  .setImage(20px, 90px, -295deg);
}

CSS Output CSS输出

.test1 {
  width: 40px;
  height: 40px;
  left: 236.9152044288992px;
  top: 72.64235636489539px;
}
.test2 {
  width: 60px;
  height: 30px;
  left: 162.36481776669305px;
  top: 36.5192246987792px;
}
.test3 {
  width: 90px;
  height: 20px;
  left: 43.39745962155612px;
  top: 90px;
}
.test4 {
  width: 40px;
  height: 70px;
  left: 104.99999999999996px;
  top: 201.60254037844385px;
}
.test5 {
  width: 20px;
  height: 90px;
  left: 207.26182617406997px;
  top: 195.630778703665px;
}

Works Even With Rotated Images 即使旋转图像也可以使用

Assuming you set your images to have transform-origin: center center then it works to keep them on center even if the images are rotated at some angle. 假设您将图像设置为具有transform-origin: center center那么即使图像旋转了某个角度,它也可以使它们居中。

See Rotated Image Example Fiddle 请参见旋转图像示例小提琴

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

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