CSS Rotating Images

后端 未结 1 1411
一生所求
一生所求 2021-01-27 13:13

In short: I am trying to mimic this slider but with animations, not transitions, so I can have autoplay option: codepen.io/dudleystorey/pen/HrFBx

Explanation: I am work

1条回答
  •  囚心锁ツ
    2021-01-27 14:11

    Here is what you can do, add a class (here anim) to the figure and change the hover rule from

    figure:hover img.first { transform: rotateY(180deg);  } 
    figure:hover img.second { transform: rotateY(0deg) translateZ(1px);  }
    

    to

    figure.anim img.first {
      animation-name: rotateus;
      animation-duration: 1s;
      animation-fill-mode: forwards;
    } 
    figure.anim img.second { 
      animation-name: rotateus2;
      animation-duration: 1s;
      animation-fill-mode: forwards;
    }
    

    I also added a @keyframes rule and change from transition-delay to animation-delay

    Updated: Based on a comment I also added some text

    Stack snippet

    body { margin: 0; background: #121; } 
    figure { 
      margin: 0;
      position: relative;
      perspective: 1800px;
      transform-style: preserve-3d;
      max-width: 1000px; 
      padding-bottom: 56.25%; 
    }
    
    body::before,
    body::after { 
      content: '';
      position: absolute;
      top: 0;
      left: 0;
      width: 100%;
      height: 100%;
      z-index: -1;
      background: url(https://s3-us-west-2.amazonaws.com/s.cdpn.io/4273/autumn-face.jpg) no-repeat left top;
      background-size: 100%;
    }
    body::after { 
      background: url(https://s3-us-west-2.amazonaws.com/s.cdpn.io/4273/julia.jpg) no-repeat left top;
      background-size: 100%;
      animation-name: swapbkg;
      animation-duration: 8s;
      animation-iteration-count: infinite;
    }
    
    div { 
      top: 0;
      left: 0;
      position: absolute;
      color: white;
      font-size: 40px;
      opacity: 1;
      z-index: 10;
      transform: translate(10px, 10px);
      animation-duration: 8s;
      animation-iteration-count: infinite;
    }
    div:nth-of-type(2) { 
      animation-name: swaptext2;
    }
    div:nth-of-type(1) { 
      animation-name: swaptext1;
    }
    
    
    figure img { 
      top: 0;
      left: 0;
      position: absolute;
      width: 100%;
    }
    
    figure.anim img.first {
      animation-name: rotateus2;
      animation-duration: 8s;
      animation-iteration-count: infinite;
    } 
    figure.anim img.second { 
      animation-name: rotateus;
      animation-duration: 8s;
      animation-iteration-count: infinite;
    }
    
    @keyframes rotateus {
      0%, 80%, 100%  {
        transform: rotateY(0deg);
      }
      30%, 50% {
        transform: rotateY(180deg);
      }
    } 
    @keyframes rotateus2 {
      0%, 80%, 100%  {
        transform: rotateY(-180deg) translateZ(1px);
      }
      30%, 50% {
        transform: rotateY(0deg) translateZ(1px); 
      }  
    }
    @keyframes swapbkg {
      0%, 39% {
        z-index: -1;
      }
      40%, 80% {
        z-index: -2;
      }
    } 
    @keyframes swaptext1 {
      0%, 20%, 50%, 100% {
        opacity: 0;
      }
      25%, 45% {
        opacity: 1;
      }
    } 
    @keyframes swaptext2 {
      0%, 70%, 100% {
        opacity: 0;
      }
      75%, 95% {
        opacity: 1;
      }
    } 
    
    figure img:nth-child(1), figure img:nth-child(11) { 
      clip: rect(0px, 100px, 840px, 0px);
      transform-origin: 50px 0px;
    }
    figure img:nth-child(2), figure img:nth-child(12) {
      clip: rect(0px, 200px, 840px, 100px);
      transform-origin: 150px 0px;
      animation-delay: 100ms;
    }
    figure img:nth-child(3), figure img:nth-child(13) {
      clip: rect(0px, 300px, 840px, 200px);
      transform-origin: 250px 0px;
      animation-delay: 200ms;  
    }
    figure img:nth-child(4), figure img:nth-child(14) {
      clip: rect(0px, 400px, 840px, 300px);
      transform-origin: 350px 0px;
      animation-delay: 300ms; 
    }
    figure img:nth-child(5), figure img:nth-child(15) {
      clip: rect(0px, 500px, 840px, 400px);
      transform-origin: 450px 0px;
      animation-delay: 400ms;
    }
    figure img:nth-child(6), figure img:nth-child(16) {
      clip: rect(0px, 600px, 840px, 500px);
      transform-origin: 550px 0px;
      animation-delay: 500ms; 
    }
    figure img:nth-child(7), figure img:nth-child(17) {
      clip: rect(0px, 700px, 840px, 600px);
      transform-origin: 650px 0px;
      animation-delay: 600ms;
    }
    figure img:nth-child(8), figure img:nth-child(18) {
      clip: rect(0px, 800px, 840px, 700px);
      transform-origin: 750px 0px;
      animation-delay: 700ms;
    }
    figure img:nth-child(9), figure img:nth-child(19) {
      clip: rect(0px, 900px, 840px, 800px);
      transform-origin: 850px 0px;
      animation-delay: 800ms; 
    }
    figure img:nth-child(10), figure img:nth-child(20) {
      clip: rect(0px, 1000px, 840px, 900px);
      transform-origin: 950px 0px;
      animation-delay: 900ms;  
    }
    Here is a text
    Here is another text


    If to keep transition and fire it with a script, change hover rule to this

    figure.anim img.first { transform: rotateY(180deg);  } 
    figure.anim img.second { transform: rotateY(0deg) translateZ(1px);  }
    

    I also added an event handler, which add the class after page been loaded

    Stack snippet

    window.addEventListener('load', function() {
      var fig = document.querySelector('figure');
      setTimeout(function () {
        fig.classList.add('anim');
      }, 500)
    })
    body { margin: 0; background: #121; } 
    figure { 
      margin: 0;
      position: relative;
      perspective: 1800px;
      transform-style: preserve-3d;
      max-width: 1000px; 
      padding-bottom: 56.25%; 
    }
    figure img { 
      top: 0;
      left: 0;
      position: absolute;
      transition: 1s;
      width: 100%;
    }
    figure img.first { transform: rotateY(0deg); }
    figure img.second { transform: rotateY(-180deg) translateZ(1px); }
    
    figure.anim img.first { transform: rotateY(180deg);  } 
    figure.anim img.second { transform: rotateY(0deg) translateZ(1px);  }
    
    figure img:nth-child(1), figure img:nth-child(11) { 
      clip: rect(0px, 100px, 840px, 0px);
      transform-origin: 50px 0px;
    }
    figure img:nth-child(2), figure img:nth-child(12) {
      clip: rect(0px, 200px, 840px, 100px);
      transform-origin: 150px 0px;
      transition-delay: 100ms;
    }
    figure img:nth-child(3), figure img:nth-child(13) {
      clip: rect(0px, 300px, 840px, 200px);
      transform-origin: 250px 0px;
      transition-delay: 200ms;  
    }
    figure img:nth-child(4), figure img:nth-child(14) {
      clip: rect(0px, 400px, 840px, 300px);
      transform-origin: 350px 0px;
      transition-delay: 300ms; 
    }
    figure img:nth-child(5), figure img:nth-child(15) {
      clip: rect(0px, 500px, 840px, 400px);
      transform-origin: 450px 0px;
      transition-delay: 400ms;
    }
    figure img:nth-child(6), figure img:nth-child(16) {
      clip: rect(0px, 600px, 840px, 500px);
      transform-origin: 550px 0px;
      transition-delay: 500ms; 
    }
    figure img:nth-child(7), figure img:nth-child(17) {
      clip: rect(0px, 700px, 840px, 600px);
      transform-origin: 650px 0px;
      transition-delay: 600ms;
    }
    figure img:nth-child(8), figure img:nth-child(18) {
      clip: rect(0px, 800px, 840px, 700px);
      transform-origin: 750px 0px;
      transition-delay: 700ms;
    }
    figure img:nth-child(9), figure img:nth-child(19) {
      clip: rect(0px, 900px, 840px, 800px);
      transform-origin: 850px 0px;
      transition-delay: 800ms; 
    }
    figure img:nth-child(10), figure img:nth-child(20) {
      clip: rect(0px, 1000px, 840px, 900px);
      transform-origin: 950px 0px;
      transition-delay: 900ms;  
    }

    0 讨论(0)
提交回复
热议问题