简体   繁体   English

全宽图像可在CSS3和HTML5中进行div淡入淡出

[英]Full width image to div cross-fade in CSS3 and HTML5

I have a full screen width background on my site, but I want to replace it with an image slideshow..everything works correctly, but when the images load in, they become all distorted.. I think it has something to do with the width/height being 100% and also with the "background-size: cover". 我的网站上有全屏宽度的背景,但是我想用图像幻灯片替换它。.一切正常,但是当图像加载时,它们全部失真了。.我认为这与宽度有关/ height为100%,并且还带有“ background-size:cover”。 Further explaination would also be awesome. 进一步的解释也很棒。

I'd like to make the image div fit exactly in the parent div as the "background-image:" property of does. 我想使image div完全适合父div,就像do的“ background-image:”属性一样。 Any help would be appreciated. 任何帮助,将不胜感激。 Thank you very much! 非常感谢你!

Image DIV CSS: 图片DIV CSS:

#crossfade > img {
    width: 100%;
    height: 100%;
    position: absolute;
    top: 0px;
    left: 0px;
    color: transparent;
    opacity: 0;
    z-index: 0;
    -webkit-backface-visibility: hidden;
    -webkit-animation: imageAnimation 30s linear infinite 0s;
    -moz-animation: imageAnimation 30s linear infinite 0s;
    -o-animation: imageAnimation 30s linear infinite 0s;
    -ms-animation: imageAnimation 30s linear infinite 0s;
    animation: imageAnimation 30s linear infinite 0s;
}

Parent DIV CSS: 父级DIV CSS:

.main-content {
  margin-top: -5px;
  display: inline-block;
  content: "";
  position: relative;
  padding-top: -5px;
  height: 300px;
  left: 50%;
  transform: translateX(-50%);
  width: 100vw;
  background-image: url(../content/images/header_bg.jpg) no-repeat center center;
  background-size: cover;
  z-index: 0;
}

They get distorted because you forcefully set their width and height to 100% . 由于您将其widthheight强制设置为100%它们会变形。

You should use min-width and min-height instead and center them with the translate transformation. 您应该改用min-widthmin-height ,并使用translate转换将它们居中。

#crossfade > img {
    min-width: 100%;
    min-height: 100%;
    position: absolute;
    top: 50%;
    left: 50%;
    transform: translate(-50%, -50%);

    opacity: 0;
    z-index: 0;
    -webkit-backface-visibility: hidden;
    -webkit-animation: imageAnimation 30s linear infinite 0s;
    -moz-animation: imageAnimation 30s linear infinite 0s;
    -o-animation: imageAnimation 30s linear infinite 0s;
    -ms-animation: imageAnimation 30s linear infinite 0s;
    animation: imageAnimation 30s linear infinite 0s;
}

Additionally there are no negative paddings 此外,没有负填充

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM