全屏图像上的CSS幻灯片动画

如何解决全屏图像上的CSS幻灯片动画

我的目标网页上有两张图片,它们以默认的淡入淡出动画来回移动。我的第一个问题是我想使用滑动动画,但是我没有找到任何更改它的选项。我的第二个问题是渐变动画仅适用于chrome 。有什么方法可以在其他浏览器中解决吗?

我的代码:

/* basicStyle.css */
* {
  margin: 0;
  padding: 0;
}

.container {
  width: 100%;
  height: 100%;
}

.lead {
  font-size: 1.5rem;
}

.navbar {
  position: fixed;
  top: 0;
  z-index: 1;
  display: flex;
  width: 100%;
  height: 60px;
  background: rgba(0,0.7);
}

.navbar ul {
  display: flex;
  list-style: none;
  width: 100%;
  justify-content: center;
}

.navbar ul li {
  margin: 0 1rem;
  padding: 1rem;
}

.navbar ul li a {
  text-decoration: none;
  text-transform: uppercase;
  color: #f4f4f4;
}

.navbar ul li a:hover {
  color: skyblue;
}

section {
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  text-align: center;
  width: 100%;
}

section h1 {
  font-size: 4rem;
}

/* Section Images */
section#home {
  background: black;
  min-height: 100vh;
}

section#gallery {
  background: red;
  min-height: 100vh;
}

section#about {
  background: green;
  min-height: 100vh;
}

section#contact {
  background: blue;
  min-height: 100vh;
}


/* LandingPage.css */
* {
  margin: 0;
  padding: 0;
}

body {
  margin: 0;
  padding: 0;
  font-family: 'Montserrat';
  font-size: 17px;
  color: #fff;
  line-height: 1.6;
}

#showcase {
  background: url('https://images.unsplash.com/photo-1597368208802-2bec16fba411?ixlib=rb-1.2.1&ixid=eyJhcHBfaWQiOjEyMDd9&auto=format&fit=crop&w=1051&q=80')
  100vw 0 no-repeat,url('https://images.unsplash.com/photo-1597390520089-9f46046ea040?ixlib=rb-1.2.1&ixid=eyJhcHBfaWQiOjEyMDd9&auto=format&fit=crop&w=1041&q=80')
  0 0 no-repeat;
  background-size: cover;
  background-position: center;
  height: 100vh;
  display: flex;
  flex-direction: column;
  justify-content: center;
  align-items: center;
  text-align: center;
  padding: 0 20px;
  animation: slide-right 15s forwards 2s infinite;
}

@keyframes slide-right {
  0% {
    background-position: 100vw 0,0 0;
  }
  50% {
    background-position: 0vw 0,0 0;
  }
  100% {
    background-position: 100vw 0,0 0;
  }
}

#showcase h1 {
  font-size: 50px;
  line-height: 1.2;
}

#showcase p {
  font-size: 20px;
  color: #fff;
}

#showcase .button {
  font-size: 18px;
  text-decoration: none;
  color: #fff;
  border: #fff 1px solid;
  padding: 10px 20px;
  border-radius: 10px;
  margin-top: 20px;
}

.main-section div.iScrollIndicator {
  background: white !important;
}

.navbar {
  position: fixed;

  top: 0;

  z-index: 1;
  display: flex;
  width: 100%;
  height: 70px;
  background: rgba(57,77,95,0.7);
  z-index: 2;
}

.navbar ul {
  display: flex;
  list-style: none;
  width: 100%;
  justify-content: center;
}

.navbar ul li {
  margin: 10px;
  padding: 5px;
}

.navbar ul li a {
  text-decoration: none;
  text-transform: uppercase;
  color: #f4f4f4;
  font-size: 30px;
}

.buttons {
  display: flex;
  justify-content: center;
}

.buttons .button {
  margin: 10px;
  display: grid;
  place-items: center;
}
<div class="container">
  <!-- <nav class="navbar">
    <ul>
      <li><a href="#home">Home</a></li>
      <li><a href="#about">About</a></li>
      <li><a href="#service">Service</a></li>
      <li><a href="#contact">Contact</a></li>
    </ul>
  </nav> -->
  <header id="showcase">
    <h1>Welcome!</h1>
    <div class="buttons">
      <a href="#about" class="button"><span>About</span></a>
      <a href="#gallery" class="button"><span>Gallery</span></a>
    </div>
  </header>
  <section id="about">
    <h1>About</h1>
  </section>
  <section id="gallery">
    <h1>Gallery</h1>
  </section>
  <section id="contact">
    <h1>Contact</h1>
  </section>
</div>

解决方法

我受到this answer的启发,并创建了以下代码段:

它使用多个背景,将顶层的屏幕设置为右侧的屏幕大小,然后将其移入。

非常简单,您可以轻松添加更多图像。代码注释向您展示了如何:)

* {
  margin: 0;
  padding: 0;
}

.container {
  width: 100%;
  height: 100%;
}

.lead {
  font-size: 1.5rem;
}

.navbar {
  position: fixed;
  top: 0;
  z-index: 1;
  display: flex;
  width: 100%;
  height: 60px;
  background: rgba(0,0.7);
}

.navbar ul {
  display: flex;
  list-style: none;
  width: 100%;
  justify-content: center;
}

.navbar ul li {
  margin: 0 1rem;
  padding: 1rem;
}

.navbar ul li a {
  text-decoration: none;
  text-transform: uppercase;
  color: #f4f4f4;
}

.navbar ul li a:hover {
  color: skyblue;
}

section {
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  text-align: center;
  width: 100%;
}

section h1 {
  font-size: 4rem;
}

/* Section Images */
section#home {
  background: black;
  min-height: 100vh;
}

section#gallery {
  background: red;
  min-height: 100vh;
}

section#about {
  background: green;
  min-height: 100vh;
}

section#contact {
  background: blue;
  min-height: 100vh;
}

/* LandingPage.css */
* {
  margin: 0;
  padding: 0;
}

body {
  margin: 0;
  padding: 0;
  font-family: "Montserrat";
  font-size: 17px;
  color: #fff;
  line-height: 1.6;
}

#showcase {
  /*/ Last image first,add 100vw for each additional image. For infinite,make first picture the last /*/
  background: url("https://images.unsplash.com/photo-1597390520089-9f46046ea040?ixlib=rb-1.2.1&ixid=eyJhcHBfaWQiOjEyMDd9&auto=format&fit=crop&w=1041&q=80")
      300vw 0 no-repeat,url("https://images.unsplash.com/photo-1521020781921-ce0d582b7665?ixlib=rb-1.2.1&ixid=eyJhcHBfaWQiOjEyMDd9&auto=format&fit=crop&w=1350&q=80")
      200vw 0 no-repeat,url("https://images.unsplash.com/photo-1597368208802-2bec16fba411?ixlib=rb-1.2.1&ixid=eyJhcHBfaWQiOjEyMDd9&auto=format&fit=crop&w=1051&q=80")
      100vw 0 no-repeat,url("https://images.unsplash.com/photo-1597390520089-9f46046ea040?ixlib=rb-1.2.1&ixid=eyJhcHBfaWQiOjEyMDd9&auto=format&fit=crop&w=1041&q=80")
      0 0 no-repeat;
  /*/ For every image,one cover /*/
  background-size: cover,cover,cover;
  height: 100vh;
  display: flex;
  flex-direction: column;
  justify-content: center;
  align-items: center;
  text-align: center;
  padding: 0 20px;
  animation: slide-right 8s forwards 1s infinite;
}
/*/ For four pictures,make keyframes at 0%,25%,75%,100%,for 6 pictures make 0%,20%,40%,60%,80%,100% etc./*/
@keyframes slide-right {
  0% {
    /*/ Add one 100vw 0 for each picture at every stage. It's always like a reverse stairway downwards /*/
    /* 3 100vw's*/
    background-position: 100vw 0,100vw 0,0 0;
  }
  33% {
    /*2 100vw's*/
    background-position: 100vw 0,0 0,0 0;
  }
  66% {
    /* 1 100vw'*/
    background-position: 100vw 0,0 0;
  }
  100% {
    /* The end - none left :)*/
    background-position: 0 0,0 0;
  }
}

#showcase h1 {
  font-size: 50px;
  line-height: 1.2;
}

#showcase p {
  font-size: 20px;
  color: #fff;
}

#showcase .button {
  font-size: 18px;
  text-decoration: none;
  color: #fff;
  border: #fff 1px solid;
  padding: 10px 20px;
  border-radius: 10px;
  margin-top: 20px;
}

.main-section div.iScrollIndicator {
  background: white !important;
}

.navbar {
  position: fixed;

  top: 0;

  z-index: 1;
  display: flex;
  width: 100%;
  height: 70px;
  background: rgba(57,77,95,0.7);
  z-index: 2;
}

.navbar ul {
  display: flex;
  list-style: none;
  width: 100%;
  justify-content: center;
}

.navbar ul li {
  margin: 10px;
  padding: 5px;
}

.navbar ul li a {
  text-decoration: none;
  text-transform: uppercase;
  color: #f4f4f4;
  font-size: 30px;
}

.buttons {
  display: flex;
  justify-content: center;
}

.buttons .button {
  margin: 10px;
  display: grid;
  place-items: center;
}
<div class="container">
  <!-- <nav class="navbar">
        <ul>
          <li><a href="#home">Home</a></li>
          <li><a href="#about">About</a></li>
          <li><a href="#service">Service</a></li>
          <li><a href="#contact">Contact</a></li>
        </ul>
      </nav> -->
  <header id="showcase">
    <h1>Welcome!</h1>

    <div class="buttons">
      <a href="#about" class="button"><span>About</span></a>
      <a href="#gallery" class="button"><span>Gallery</span></a>
    </div>
  </header>
  <section id="about">
    <h1>About</h1>
  </section>
  <section id="gallery">
    <h1>Gallery</h1>
  </section>
  <section id="contact">
    <h1>Contact</h1>
  </section>
</div>

,

尝试以一种方式重新排列background-position部分中的animation,该部分的数字以视口宽度(vw)为单位。

如果background排列中有3张图像,则如下图所示

  1. 100vw 0,0 0
  2. 100vw 0,0 0
  3. 0 0,0 0

,您还可以在代码中的<html>标签上简单地添加scroll-behavior属性,以使smooth滚动,从而在发生滚动事件时也使页面看起来更好。

示例

html{scroll-behavior: smooth;}
* {
    margin: 0;
    padding: 0;
    box-sizing: border-box;
    color: #fff;
}
section,#showcase {
    display: flex;
    flex-direction: column;
    align-items: center;
    justify-content: center;
}
section#gallery,section#home,section#about,section#contact,#showcase{min-height: 100vh;}
section{width: 100%;}
section h1 {font-size: 4rem;}
section#home {background: black;}
section#gallery {background: red;}
section#about {background: green;}
section#contact {background: blue;}
#showcase {
    background: url("https://images.unsplash.com/photo-1597390520089-9f46046ea040?ixlib=rb-1.2.1&ixid=eyJhcHBfaWQiOjEyMDd9&auto=format&fit=crop&w=1041&q=80")
    200vw 0 no-repeat,url("https://images.unsplash.com/photo-1597368208802-2bec16fba411?ixlib=rb-1.2.1&ixid=eyJhcHBfaWQiOjEyMDd9&auto=format&fit=crop&w=1051&q=80")
    100vw 0 no-repeat,url("https://images.unsplash.com/photo-1597390520089-9f46046ea040?ixlib=rb-1.2.1&ixid=eyJhcHBfaWQiOjEyMDd9&auto=format&fit=crop&w=1041&q=80")
    0 0 no-repeat;
    background-size: cover;
    animation: slide-right 5s ease-in-out infinite;
}
@keyframes slide-right {
0% {background-position: 100vw 0,0 0;}
60% {background-position: 100vw 0,0 0;}
100% {background-position: 0 0,0 0;}
}

#showcase h1 {font-size: 50px;}
#showcase .button {
    font-size: 18px;
    text-decoration: none;
    border: #fff 1px solid;
    padding: 10px 20px;
    border-radius: 10px;
    margin-top: 20px;
}
.buttons {
    display: flex;
    justify-content: center;
}
.buttons .button {margin: 10px;}
<html>
<div class="container">
  <header id="showcase">
    <h1>Welcome!</h1>
    <div class="buttons">
      <a href="#about" class="button"><span>About</span></a>
      <a href="#gallery" class="button"><span>Gallery</span></a>
    </div>
  </header>
  <section id="about">
    <h1>About</h1>
  </section>
  <section id="gallery">
    <h1>Gallery</h1>
  </section>
  <section id="contact">
    <h1>Contact</h1>
  </section>
</div>
</html>

为了避免不必要的代码和DRY(不要重复自己),我也进行了过多的编辑,但希望对您有所帮助。

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐


依赖报错 idea导入项目后依赖报错,解决方案:https://blog.csdn.net/weixin_42420249/article/details/81191861 依赖版本报错:更换其他版本 无法下载依赖可参考:https://blog.csdn.net/weixin_42628809/a
错误1:代码生成器依赖和mybatis依赖冲突 启动项目时报错如下 2021-12-03 13:33:33.927 ERROR 7228 [ main] o.s.b.d.LoggingFailureAnalysisReporter : *************************** APPL
错误1:gradle项目控制台输出为乱码 # 解决方案:https://blog.csdn.net/weixin_43501566/article/details/112482302 # 在gradle-wrapper.properties 添加以下内容 org.gradle.jvmargs=-Df
错误还原:在查询的过程中,传入的workType为0时,该条件不起作用 &lt;select id=&quot;xxx&quot;&gt; SELECT di.id, di.name, di.work_type, di.updated... &lt;where&gt; &lt;if test=&qu
报错如下,gcc版本太低 ^ server.c:5346:31: 错误:‘struct redisServer’没有名为‘server_cpulist’的成员 redisSetCpuAffinity(server.server_cpulist); ^ server.c: 在函数‘hasActiveC
解决方案1 1、改项目中.idea/workspace.xml配置文件,增加dynamic.classpath参数 2、搜索PropertiesComponent,添加如下 &lt;property name=&quot;dynamic.classpath&quot; value=&quot;tru
删除根组件app.vue中的默认代码后报错:Module Error (from ./node_modules/eslint-loader/index.js): 解决方案:关闭ESlint代码检测,在项目根目录创建vue.config.js,在文件中添加 module.exports = { lin
查看spark默认的python版本 [root@master day27]# pyspark /home/software/spark-2.3.4-bin-hadoop2.7/conf/spark-env.sh: line 2: /usr/local/hadoop/bin/hadoop: No s
使用本地python环境可以成功执行 import pandas as pd import matplotlib.pyplot as plt # 设置字体 plt.rcParams[&#39;font.sans-serif&#39;] = [&#39;SimHei&#39;] # 能正确显示负号 p
错误1:Request method ‘DELETE‘ not supported 错误还原:controller层有一个接口,访问该接口时报错:Request method ‘DELETE‘ not supported 错误原因:没有接收到前端传入的参数,修改为如下 参考 错误2:cannot r
错误1:启动docker镜像时报错:Error response from daemon: driver failed programming external connectivity on endpoint quirky_allen 解决方法:重启docker -&gt; systemctl r
错误1:private field ‘xxx‘ is never assigned 按Altʾnter快捷键,选择第2项 参考:https://blog.csdn.net/shi_hong_fei_hei/article/details/88814070 错误2:启动时报错,不能找到主启动类 #
报错如下,通过源不能下载,最后警告pip需升级版本 Requirement already satisfied: pip in c:\users\ychen\appdata\local\programs\python\python310\lib\site-packages (22.0.4) Coll
错误1:maven打包报错 错误还原:使用maven打包项目时报错如下 [ERROR] Failed to execute goal org.apache.maven.plugins:maven-resources-plugin:3.2.0:resources (default-resources)
错误1:服务调用时报错 服务消费者模块assess通过openFeign调用服务提供者模块hires 如下为服务提供者模块hires的控制层接口 @RestController @RequestMapping(&quot;/hires&quot;) public class FeignControl
错误1:运行项目后报如下错误 解决方案 报错2:Failed to execute goal org.apache.maven.plugins:maven-compiler-plugin:3.8.1:compile (default-compile) on project sb 解决方案:在pom.
参考 错误原因 过滤器或拦截器在生效时,redisTemplate还没有注入 解决方案:在注入容器时就生效 @Component //项目运行时就注入Spring容器 public class RedisBean { @Resource private RedisTemplate&lt;String
使用vite构建项目报错 C:\Users\ychen\work&gt;npm init @vitejs/app @vitejs/create-app is deprecated, use npm init vite instead C:\Users\ychen\AppData\Local\npm-