Home > Mobile >  How to set transition for 2 backgrounds in an element?
How to set transition for 2 backgrounds in an element?

Time:02-12

in this code:

    #p1 {
        background-image: url(backimgs/first/1.jpg), url(backimgs/first/2.jpg);
        background-color: #05080d;
        background-position: left top, left bottom;
        background-size: 100% 35%, 100% 65%;
    }

I want when the page shows up, first show backimgs/first/1.jpg then after 1 sec show backimgs/first/2.jpg. how can I do it?

CodePudding user response:

You can't animate background-images. You can change it, but there won't be any smooth transition:

#p1 {
  background-image: url(backimgs/first/1.jpg), url(backimgs/first/2.jpg);
  background-color: #05080d;
  background-position: left top, left bottom;
  background-size: 100% 35%, 100% 65%;
  animation: change-bg;
  animation-duration: 1s;
}

@keyframes change-bg {
  0% {
    background-image: url(backimgs/first/1.jpg), url(backimgs/first/2.jpg);
    background-size: 100% 35%, 100% 65%;
  }
  100% {
    background-image: url(backimgs/first/2.jpg), url(backimgs/first/1.jpg);
    background-size: 100% 65%, 100% 35%;
  }
}

If you want a smooth transition - you can use ::before and ::after with a background and animate the opacity of them. Let me know with a comment if you need more info on this aproach, I'll edit the post and show, how it's done.

CodePudding user response:

You mention 'transition' in the title so you will need to control the two parts of the background separately.

To enable this, this snippet removes the backgrounds from the element itself, instead putting them onto two pseudo elements. The before pseudo element having the first image as background and the after pseudo element having the second one.

Separating the components in this way means we can animate the opacities, the first pseudo element going from opacity 0 to opacity 1 in the first second.

Note however that a little hack has been added to ths snippet. As the animation on the before pseudo element is to happen on load then there needs to be some method of waiting for the background image to load before the animation starts else there is a danger it will be part way through, or even finished, before the image is actually available.

I do not know the wider context of how you are testing for load being complete so have just put a delay in here for demo purposes. You'll need to decide what to do to avoid this inital load situation.

* {
  margin: 0;
  padding: 0;
}

#p1 {
  /* added for this demo */
  display: inline-block;
  width: 100vw;
  height: 100vh;
  position: relative;
}

#p1::before,
#p1::after {
  content: '';
  position: absolute;
  z-index: -1;
  width: 100%;
  left: 0;
  display: inline-block;
  background-color: #05080d;
  background-size: cover;
  background-repeat: no-repeat no-repeat;
  background-position: center center;
  animation: fadein 1s linear;
  animation-fill-mode: forwards;
  opacity: 0;
}

#p1::before {
  top: 0;
  height: 35%;
  background-image: url(https://picsum.photos/id/1018/1024/768);
  animation-delay: 1s;
  /* a hack to ensure it is loaded before start the animation */
}

#p1::after {
  bottom: 0;
  height: 65%;
  background-image: url(https://picsum.photos/id/1015/1024/768);
  animation-delay: 2s;
}

@keyframes fadein {
  0% {
    opacity: 0;
  }
  100% {
    opacity: 1;
  }
}
<div id="p1"></div>

  • Related