背景幻灯片徒步旅行队

时间:2017-06-19 02:11:56

标签: html css safari css-animations keyframe

所以我为主页创建了一个幻灯片,它在chrome上工作正常但是出于某种原因,在safari上它只显示渐变背景但没有显示图像。

#hero-image{
    background-size:cover;
    -webkit-background-size: cover;
    background-repeat: no-repeat;
    -moz-animation: slide 15s infinite;
    -o-animation: slide 15s infinite;
    animation: slide 15s infinite;
    -webkit-animation: slide 15s infinite; /* Safari 4.0 - 8.0 */

    #front-search-box{
        color:white;
        position:absolute;
        background-color:transparent;
    }
}



@-webkit-keyframes slide {
    0% {

        background: -webkit-linear-gradient(top,rgba(0,0,0,1),rgba(0,0,0,0.3)), url("BlackBackground2.png");
    }

    50%{
        background: -webkit-linear-gradient(top,rgba(0,0,0,1),rgba(0,0,0,0.3)), url("BlackBackground3.png");
    }

    100%{
        background: -webkit-linear-gradient(top,rgba(0,0,0,1),rgba(0,0,0,0.3)), url("BlackBackground4.png");
    }
}

@-moz-keyframes slide{
    0% {
        background: linear-gradient(rgba(0,0,0,1),rgba(0,0,0,0.3)), url("BlackBackground2.png");
    }

    50%{
    background: linear-gradient(rgba(0,0,0,1),rgba(0,0,0,0.3)), url("BlackBackground3.png");
    }

    100%{
        background: linear-gradient(rgba(0,0,0,1),rgba(0,0,0,0.3)), url("BlackBackground4.png");
    }
}

@-o-keyframes slide{
    0% {
        background: linear-gradient(rgba(0,0,0,1),rgba(0,0,0,0.3)), url("BlackBackground2.png");
    }

    50%{
    background: linear-gradient(rgba(0,0,0,1),rgba(0,0,0,0.3)), url("BlackBackground3.png");
    }

    100%{
        background: linear-gradient(rgba(0,0,0,1),rgba(0,0,0,0.3)), url("BlackBackground4.png");
    }
}

@keyframes slide{
    0% {
        background: linear-gradient(rgba(0,0,0,1),rgba(0,0,0,0.3)), url("BlackBackground2.png");
    }

    50%{
    background: linear-gradient(rgba(0,0,0,1),rgba(0,0,0,0.3)), url("BlackBackground3.png");
    }

    100%{
        background: linear-gradient(rgba(0,0,0,1),rgba(0,0,0,0.3)), url("BlackBackground4.png");
    }
}

1 个答案:

答案 0 :(得分:1)

Safari需要设置默认的background-image属性,以便能够在关键帧动画中为其设置动画。但请注意,此浏览器确实在图像状态之间进行动画处理(缩小+不透明度转换)。

另外,如果您的问题中粘贴的代码是CSS,请注意,与#front-search-box一样嵌套声明在语法上是不正确的。

最后,请注意,如果您在动画中设置了background属性,它将重置之前可能设置的所有更精确的规则(例如background-cover)。由于此处您只是更改图像,因此请使用background-image属性。

更新了示例,没有所有过时的供应商前缀。

#hero-image {
  width: 100vw;
  height: 100vh;
  background-size: cover;
  -webkit-background-size: cover;
  background-repeat: no-repeat;
  animation: slide 15s infinite;
  /* we need to set the default here for safari */
  background-image: linear-gradient(rgba(0, 0, 0, 1), rgba(0, 0, 0, 0.3)), url("aaa.png");
}

@keyframes slide {
  0% {
    background-image: linear-gradient(rgba(0, 0, 0, 1), rgba(0, 0, 0, 0.3)), url("https://upload.wikimedia.org/wikipedia/commons/thumb/5/55/John_William_Waterhouse_A_Mermaid.jpg/800px-John_William_Waterhouse_A_Mermaid.jpg");
  }
  50% {
    background-image: linear-gradient(rgba(0, 0, 0, 1), rgba(0, 0, 0, 0.3)), url("https://upload.wikimedia.org/wikipedia/commons/4/47/PNG_transparency_demonstration_1.png");
  }
  100% {
    background-image: linear-gradient(rgba(0, 0, 0, 1), rgba(0, 0, 0, 0.3)), url("https://upload.wikimedia.org/wikipedia/commons/d/dd/Heptagon_pavilion%2C_HK_%26_Kln_Fuk_Tak_Buddhist_Association.JPG");
  }
}
<div id="hero-image"></div>