【问题标题】:Image crossfading without HTML - ONLY CSS没有 HTML 的图像交叉淡入淡出 - 只有 CSS
【发布时间】:2015-01-29 07:16:07
【问题描述】:

我需要帮助来完成网页设计的作业。我们必须只使用 CSS 设计一个 Wordpress 页面。 我想在标题中使用 2 张图片进行交叉淡入淡出。

在我的代码的重要部分下方。当然,由于有两个背景图像,它不会像这样工作。动画准备好了,但是当img淡出时,只有一个白洞。

那么如何在 CSS 中放置 2 张图片?... 谢谢你的帮助! (抱歉英语不好:D)

#pageheader {
background-image: url('img/test.jpg');
background-image: url('img/bg.jpg');
height: 500px;
background-repeat: no-repeat;
background-position: center center;
background-size: 100% auto;

left:0;
-webkit-transition: opacity 1s ease-in-out;
-moz-transition: opacity 1s ease-in-out;
-o-transition: opacity 1s ease-in-out;
transition: opacity 1s ease-in-out;
}

@keyframes pageheaderFadeInOut {

0% {
opacity:1;
}

45% {
      opacity:1;
  }

55% {
    opacity:0;
    }

100% {
      opacity:0;
  }
}

#pageheader {
animation-name: pageheaderFadeInOut;
animation-timing-function: ease-in-out;
animation-iteration-count: infinite;
animation-duration: 10s;
animation-direction: alternate;
}

【问题讨论】:

    标签: css wordpress image cross-fade


    【解决方案1】:

    当您的动画运行时,您会看到“白洞”,因为您的两个背景图像都在您的 #pageheader div 内,您的动画正在使该 div 透明。

    由于您不想编辑 HTML,我们可以使用 :after 伪元素作为单独的元素来保存您的第二张图片。请注意,这仅适用于这两个图像。

    这是一个有效的jsfiddle

    CSS:
    #pageheader {
      background-image: url('img/test.jpg');
      height: 500px;
      width: 600px;
      background-repeat: no-repeat;
      background-position: center;
      background-size: cover;
      -webkit-transition: opacity 1s ease-in-out;
      -moz-transition: opacity 1s ease-in-out;
      -o-transition: opacity 1s ease-in-out;
      transition: opacity 1s ease-in-out;
    }
    
    #pageheader:after{
      content: '';
      display: block;
      width: 100%;
      height: 500px;
      background-image: url('img/bg.jpg');
      background-repeat: no-repeat;
      background-position: center;
      background-size: cover;
      animation-name: pageheaderFadeInOut;
      animation-timing-function: ease;
      animation-iteration-count: infinite;
      animation-duration: 10s;
     }
    
    
    @keyframes pageheaderFadeInOut {
      0% {opacity:1;}
      50% {opacity:0;}  
      100% {opacity:1;}
    }
    

    请注意,您需要添加浏览器前缀(就像我在 jsfiddle 中所做的那样)。

    【讨论】:

    • 非常感谢您!现在两张图片都显示了,但不在同一个地方...我会尝试修复它,但是有什么想法吗?...
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-08-05
    • 1970-01-01
    相关资源
    最近更新 更多