【问题标题】:CSS Keyframe AnimationsCSS 关键帧动画
【发布时间】:2021-10-07 03:16:16
【问题描述】:

我有一些非常基本的 HTML 和 CSS,并试图实现一个自动幻灯片放映的关键帧动画。我复制了一个教程,它工作得非常好,但我自己却很难配置它。

html & CSS 是:

#SLIDE_BG {
  width: 100%;
  height: 100vh;
  background-position: center center;
  background-size: cover;
  background-repeat: no-repeat;
  backface-visibility: hidden;
  animation: slideBg 8s linear infinite 0s;
  animation-timing-function: ease-in-out;
  background-image: url('/assets/images/jack-test/beatles-one.jpg');
}

@keyframes slideBg {
  0% {
    background-image: url('/assets/images/jack-test/beatles-oone.jpeg');
  }
  25% {
    background-image: url('/assets/images/jack-test/beatles-two.jpeg');
  }
  50% {
    background-image: url('/assets/images/jack-test/jimi-hendix.jpeg');
  }
  75% {
    background-image: url('/assets/images/jack-test/led-zeppelin.jpeg');
  }
  100% {
    background-image: url('/assets/images/jack-test/rock-band.jpeg');
  }
}
<div id="SLIDE_BG"></div>

【问题讨论】:

  • 背景变了,有什么问题?
  • @Azu 他们不在我这边?
  • @Rana 不幸的是,我更正了这一点,但仍然无法正常工作
  • 不,页面上的图片是全尺寸的。
  • 你是在本地运行代码

标签: html css frontend css-animations keyframe


【解决方案1】:

您必须使用url(在线托管)才能使用background-image。如果您使用的是本地电脑而不是使用它,则您可以在线或在同一文件夹中托管图像。

#SLIDE_BG {
  width: 100%;
  height: 100vh;
  background-position: center center;
  background-size: cover;
  background-repeat: no-repeat;
  backface-visibility: hidden;
  animation: slideBg 8s linear infinite 0s;
  animation-timing-function: ease-in-out;
  background-image: url('https://jooinn.com/images/dramatic-landscape-7.jpg');
}

@keyframes slideBg {
  0% {
    background-image: url('https://jooinn.com/images/dramatic-landscape-7.jpg');
  }
  25% {
    background-image: url('http://www.thewowstyle.com/wp-content/uploads/2015/01/nature-image.jpg');
  }
  50% {
    background-image: url('https://images.designtrends.com/wp-content/uploads/2016/01/04085621/A-Cold-Sunset-Background.jpg');
  }
  75% {
    background-image: url('https://jooinn.com/images/hdr-landscape-1.jpg');
  }
  100% {
    background-image: url('https://www.shutterstock.com/blog/wp-content/uploads/sites/5/2016/03/fall-trees-road-1.jpg');
  }
}
<div id="SLIDE_BG"></div>

更新:

您也可以在PC中使用图像的本地路径并记住一些要点:

  • 如果它保存在保存 HTML 文件的同一驱动器 (C: D: .....) 中,则使用以驱动器名称开头的 background-image: url('') 中的完整路径,即 @987654329 @.

  • 如果图像保存在保存HTML文件的同一文件夹中,则可以使用直接名称,即 background-image: url('imageName.png')将起作用

  • 如果图片保存在保存HTML文件的子文件夹中,则可以使用短路径,即htmlFileFolder(包含html文件)> folder1> folder2> image.png那么url 将是这样的folder1/folder2/image.png 而不是/folder1/folder2/image.png

  • 如果图像保存在HTML文件或HTML文件子文件夹以外的文件夹中,则必须使用完整路径

【讨论】:

    【解决方案2】:

    您需要在下面正确检查图像路径

    #SLIDE_BG {
      width: 100%;
      height: 100vh;
      background-position: center center;
      background-size: cover;
      background-repeat: no-repeat;
      backface-visibility: hidden;
      animation: slideBg 8s linear infinite 0s;
      animation-timing-function: ease-in-out;
      background-image: url('https://dummyimage.com/800x300');         
    }
    
    @keyframes slideBg {
      0% {
        background-image: url('https://upload.wikimedia.org/wikipedia/commons/2/29/Dscn7471_sunset-sundog_crop_800x300.jpg');
      }
      25% {
        background-image: url('https://www.thegrandsiba.com/wp-content/uploads/2017/06/MG_28423-800x300.jpg');
      }
      50% {
        background-image: url('https://dummyimage.com/800x300');
      }
      75% {
        background-image: url('https://images-wixmp-ed30a86b8c4ca887773594c2.wixmp.com/f/5e2a19d4-f261-4548-b4a7-1e2f5ad139af/d99najf-e9049704-bfc4-4836-952d-9315403bd60f.gif');
      }
      100% {
        background-image: url('https://www.businessclass.co.uk/wp-content/uploads/sites/11/2016/04/Toronto-800x300-800x300.jpg');
      }
    }
    

    【讨论】: