【问题标题】:How to resize rotation radius using css如何使用css调整旋转半径
【发布时间】:2020-04-18 21:41:36
【问题描述】:

如何调整小球的旋转半径,使其接触橙色容器边框

.bg {
    background-color: blue;

    display: flex;
    align-items: center;
    justify-content: center;
}

.ball-container {
    position: relative;
    display:flex;
    justify-content:center;
    align-items:center;
    background-color: orange;
    width: 250px;
    height: 250px;
}

.ball {
    position: absolute;
    width: 24px;
    height: 24px;
    background-color: white;
    border-radius: 50%;

    top: 125px - 12px;
    left: 125px - 12px;

    animation-name: ball_moves;
    animation-duration: 1.5s;
    animation-timing-function: ease-in-out;
    animation-iteration-count: infinite;
    animation-direction: initial;
}

.ball:nth-child(1) {
    transform-origin: top left;
}
.ball:nth-child(2) {
    transform-origin: top right;
}
.ball:nth-child(3) {
    transform-origin: bottom left;
}
.ball:nth-child(4) {
    transform-origin: bottom right;
}

@keyframes ball_moves {
    0% {
        transform: rotateZ(0);
    }
    100% {
        transform: rotateZ(360deg);
    }
}
<div class="bg">

    <div class="ball-container">
        <div class="ball"></div>
        <div class="ball"></div>
        <div class="ball"></div>
        <div class="ball"></div>
    </div>

</div>

【问题讨论】:

  • 调整变换原点值。
  • 我试过这个,但无法将所有四个球移动到不同的方向,因为它们现在正在向所有四个方向移动。你能帮我多一点吗?

标签: html css css-animations css-transforms


【解决方案1】:

使用 CSS 变量定义一个唯一的偏移量,可以使用 transform-origin 控制你的所有圆圈:

.bg {
    background-color: blue;

    display: flex;
    align-items: center;
    justify-content: center;
    margin:10px;
}

.ball-container {
    --d:35px; /* adjust this like you want (you can even use % value)*/
    /* 
      1) we first find the middle of each small square
        the size is 250/2=125 so we translate by (125-24)/2 = 50.5px
        this will put the origin in the middle and give us a rotation 
        following the "circumscribe" circle so we the circle will go outside
      2) we need to decrease it to keep the rotation inside
         the calculate is a bit complex but we decrease by around 15px to have 35px
    */
    
    position: relative;
    display:flex;
    justify-content:center;
    align-items:center;
    background: 
      linear-gradient(red,red) center/100% 1px,
      linear-gradient(red,red) center/1px 100%,
      orange;
    background-repeat:no-repeat;
    width: 250px;
    height: 250px;
}

.ball {
    position: absolute;
    width: 24px;
    height: 24px;
    background-color: white;
    border-radius: 50%;

    animation-name: ball_moves;
    animation-duration: 1.5s;
    animation-timing-function: ease-in-out;
    animation-iteration-count: infinite;
    animation-direction: initial;
}

.ball:nth-child(1) {
    transform-origin: calc(-1*var(--d)) calc(-1*var(--d));
}
.ball:nth-child(2) {
    transform-origin: calc(-1*var(--d)) calc(100% + var(--d));
}
.ball:nth-child(3) {
    transform-origin: calc(100% + var(--d)) calc(-1*var(--d));
}
.ball:nth-child(4) {
    transform-origin: calc(100% + var(--d)) calc(100% + var(--d));
}

@keyframes ball_moves {
    0% {
        transform: rotateZ(0);
    }
    100% {
        transform: rotateZ(360deg);
    }
}
<div class="bg">

    <div class="ball-container">
        <div class="ball"></div>
        <div class="ball"></div>
        <div class="ball"></div>
        <div class="ball"></div>
    </div>

</div>

<div class="bg">

    <div class="ball-container" style="--d:100%;"> <!-- 100% = 24px (size of circle) -->
        <div class="ball"></div>
        <div class="ball"></div>
        <div class="ball"></div>
        <div class="ball"></div>
    </div>

</div>

使用另一种语法:

.bg {
    background-color: blue;

    display: flex;
    align-items: center;
    justify-content: center;
    margin:10px;
}

.ball-container {
    --d:47px; /* adjust this like you want (you can even use % value)*/
    /* 
      1) we first find the middle of each small square
        the size is 250/2=125 so we translate by (125)/2 = 66.5px
        this will put the origin in the middle and give us a rotation 
        following the "circumscribe" circle so we the circle will go outside
      2) we need to decrease it to keep the rotation inside
         the calculate is a bit complex but we decrease by around 15px to have 47px
    */
    
    position: relative;
    display:flex;
    justify-content:center;
    align-items:center;
    background: 
      linear-gradient(red,red) center/100% 1px,
      linear-gradient(red,red) center/1px 100%,
      orange;
    background-repeat:no-repeat;
    width: 250px;
    height: 250px;
}

.ball {
    position: absolute;
    width: 24px;
    height: 24px;
    background-color: white;
    border-radius: 50%;

    animation-name: ball_moves;
    animation-duration: 1.5s;
    animation-timing-function: ease-in-out;
    animation-iteration-count: infinite;
    animation-direction: initial;
}

.ball:nth-child(1) {
    transform-origin: calc(50% - var(--d)) calc(50% - var(--d));
}
.ball:nth-child(2) {
    transform-origin: calc(50% - var(--d)) calc(50% + var(--d));
}
.ball:nth-child(3) {
    transform-origin: calc(50% + var(--d)) calc(50% - var(--d));
}
.ball:nth-child(4) {
    transform-origin: calc(50% + var(--d)) calc(50% + var(--d));
}

@keyframes ball_moves {
    0% {
        transform: rotateZ(0);
    }
    100% {
        transform: rotateZ(360deg);
    }
}
<div class="bg">

    <div class="ball-container">
        <div class="ball"></div>
        <div class="ball"></div>
        <div class="ball"></div>
        <div class="ball"></div>
    </div>

</div>

<div class="bg">

    <div class="ball-container" style="--d:200%;"> <!-- 100% = 48px (size of circle) -->
        <div class="ball"></div>
        <div class="ball"></div>
        <div class="ball"></div>
        <div class="ball"></div>
    </div>

</div>

【讨论】:

  • --d 中设置的值是任意的,旨在向您展示如何轻松解决问题,并供您调整。你可以在 css 代码中看到他放在它旁边的注释。如果您通过这些计算,您会发现它只是乘以负值来向后或添加到最大高度/宽度以向前
  • @WasiF 转换考虑坐标 0,0 所以在负步骤中我只是做(0 - 偏移)并进入正步骤我做(100% + 偏移),因为我需要考虑另一个角落对所有人进行对称旋转。现在对于偏移量,想象你的橙色正方形被分成 4 个小正方形,我需要将每个圆的原点放在正方形的中间,所以我需要进行平移。圆的宽度是 24 像素,每个小方块是 125 像素,所以偏移量需要是 (125 像素 - 24 像素)/2 = 50 像素,但这会绕角旋转,所以我会减小它。
  • @WasiF 我将其减小为内部旋转,当我使用 120% 时,这意味着圆的宽度/高度的 120%,即等于 28.8px。我同时使用 % 和 px 来表明两者的工作方式相同,但计算需要同时考虑您的正方形和圆形尺寸
  • @WasiF 这不是双倍,而是 100% + 偏移量......这是因为每个元素的引用是它的顶部/左侧而不是它的中心。所以如果我做 -d 我需要做 100% + d 因为 100% 会将我们移动到对面的角落然后我们添加一个偏移量
  • @WasiF 这是另一种语法,我想它会更容易理解:jsfiddle.net/pke2wL3f .. 我认为在所有情况下都是 50%,然后是 + 或 -
猜你喜欢
  • 1970-01-01
  • 2016-01-16
  • 1970-01-01
  • 2013-04-22
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-09-05
  • 1970-01-01
相关资源
最近更新 更多