在 CSS 中旋转地球仪

Rotating globe in CSS

我正在 CSS 中创建旋转地球效果。我在 CSS 中创建了地球:

body {
  background-color: #111;
}

#earth {
    width: 300px;
    height: 300px;
    background: url(https://web.archive.org/web/20150807125159if_/http://www.noirextreme.com/digital/Earth-Color4096.jpg);
    border-radius: 50%;
    background-size: 610px;
    box-shadow: inset 8px 36px 80px 36px rgb(0, 0, 0),
    inset -6px 0 12px 4px rgba(255, 255, 255, 0.3);
    animation-name: rotate;
    animation-duration: 12s;
    animation-iteration-count: infinite;
    animation-timing-function: linear;
    -webkit-animation-name: rotate;
    -webkit-animation-duration: 12s;
    -webkit-animation-iteration-count: infinite;
    -webkit-animation-timing-function: linear;
}

@keyframes rotate {
    from { background-position: 0px 0px; }
    to { background-position: 500px 0px; }
}
@-webkit-keyframes rotate {
    from { background-position: 0px 0px; }
    to { background-position: 500px 0px; }
}
<div id="earth"></div>

但是它停止了,然后图像重置并重新开始。我希望它平稳移动而不会抽搐。非常感谢!

background-position: 500px 0px;中将500px替换为610px,即background-size

body {
  background-color: #111;
}
#earth {
  width: 300px;
  height: 300px;
  background: url(https://web.archive.org/web/20150807125159if_/http://www.noirextreme.com/digital/Earth-Color4096.jpg);
  border-radius: 50%;
  background-size: 610px;
  box-shadow: inset 8px 36px 80px 36px rgb(0, 0, 0), inset -6px 0 12px 4px rgba(255, 255, 255, 0.3);
  animation-name: rotate;
  animation-duration: 12s;
  animation-iteration-count: infinite;
  animation-timing-function: linear;
  -webkit-animation-name: rotate;
     -webkit-animation-duration: 12s;
     -webkit-animation-iteration-count: infinite;
     -webkit-animation-timing-function: linear;
}
@keyframes rotate {
  from {
    background-position: 0px 0px;
  }
  to {
    background-position: 610px 0px;
  }
}
@-webkit-keyframes rotate {
  from {
    background-position: 0px 0px;
  }
  to {
    background-position: 610px 0px;
  }
}
<div id="earth"></div>

您的代码中的问题是图像大小 (610px) 和动画的偏移量 (500px) 不同并且在重置时它跳跃的动画 (110px)。

我喜欢使用一个简单的技巧来代替以像素为单位定义动画偏移:以百分比定义它
我没有告诉它移动 610px,而是让它移动 100%.

100% 方法的好处是,如果您更改图片,则不必更改 CSS 中的所有硬编码值,IMO 应该是首选方法。

请注意:从 0 到 -100% 似乎会创建一个跃点。因为我们需要旋转到正确的方向,所以我尝试从 100% 开始并将其移动到 0,但此时图像不再存在。

@keyframes rotate {
   from { background-position:  100%  0; }
   to {   background-position:    0   0; }
}

这是片段,但使用 100% 而不是像素值:
* 请注意:动画仍然跳动,但我无法测试新代码,因为图像已不存在。逻辑有效,但此实现似乎无效。以下代码只是用TS的代码做的demo

body {
  background-color: #111;
}
#earth {
  width: 300px;
  height: 300px;
  background: url(https://web.archive.org/web/20150807125159if_/http://www.noirextreme.com/digital/Earth-Color4096.jpg);
  border-radius: 50%;
  background-size: 610px;
  box-shadow: inset 8px 36px 80px 36px rgb(0, 0, 0), inset -6px 0 12px 4px rgba(255, 255, 255, 0.3);
  animation-name: rotate;
  animation-duration: 12s;
  animation-iteration-count: infinite;
  animation-timing-function: linear;
}
@keyframes rotate {
   from { background-position:  100%  0; }
   to {   background-position:    0   0; }
}
<div id="earth"></div>

body {
  background-color: #111;
}

#earth {
    width: 300px;
    height: 300px;
    background: url(https://zippin.online/wp-content/uploads/2022/02/registration-bg.png);
    border-radius: 50%;
    background-size: 610px;
    box-shadow: inset 8px 36px 80px 36px rgb(0, 0, 0),
    inset -6px 0 12px 4px rgba(255, 255, 255, 0.3);
    animation-name: rotate;
    animation-duration: 12s;
    animation-iteration-count: infinite;
    animation-timing-function: linear;
    -webkit-animation-name: rotate;
    -webkit-animation-duration: 12s;
    -webkit-animation-iteration-count: infinite;
    -webkit-animation-timing-function: linear;
}

@keyframes rotate {
    from { background-position: 0px 0px; }
    to { background-position: 500px 0px; }
}
@-webkit-keyframes rotate {
    from { background-position: 0px 0px; }
    to { background-position: 500px 0px; }
}
<div id="earth"></div>