So erstellen Sie mithilfe von CSS einen Cyberpunk 2077-Glitch-Effekt für ein Bild

Foto von Hyunwon Jang auf Unsplash
Foto von Hyunwon Jang auf Unsplash

Als ich auf die Cyberpunk 2077-Website im Nachrichtenbereich ging , stieß ich auf den "gleichen" Effekt. Es erschien, wenn Sie mit der Maus über das Post-Bild schwebten. Ich fand es interessant und entschied mich zu sehen, wie es funktioniert. In diesem Beitrag möchte ich mitteilen, was ich gefunden habe.





Um diesen Effekt zu erzielen, müssen wir das Bild zweimal anzeigen und übereinander legen. Der Effekt selbst wird in dem Bild angezeigt, das oben liegt.





, . .





<div class="container">
  <div class="img"></div>
  <div class="glitch"></div>
</div>
      
      



.





.img,
.glitch {
  display: block;
  background-image: url('https://images.unsplash.com/photo-1551771685-c367c9127a03?ixid=MXwxMjA3fDB8MHxwaG90by1wYWdlfHx8fGVufDB8fHw%3D&ixlib=rb-1.2.1&auto=format&fit=crop&w=1000&q=80');
  background-repeat: no-repeat;
  background-size: cover;
  width: 564px;
  height: 376px;
}
      
      



2 . , . .





.glitch {
  position: absolute;
  top: 0;
  left: 0;
  right: 0;
  bottom: 0;
}
.container {
  position: relative;
}
      
      



. . .





.glitch:hover {
 -webkit-animation-duration:2s;
 animation-duration:2s;
 -webkit-animation-timing-function:linear;
 animation-timing-function:linear;
 -webkit-animation-iteration-count:infinite;
 animation-iteration-count:infinite;
 -webkit-animation-name:glitch-anim;
 animation-name:glitch-anim;
 -webkit-animation-direction:alternate;
 animation-direction:alternate
}
      
      



:





  • animation-duration - , 2 ;





  • animation-timing-function - , ;





  • animation-iteration-count - ;





  • animation-name - , ;





  • Animationsrichtung - Die Richtung der Animation mit dem alternativen Wert. Die Animation geht von Anfang bis Ende und dann zurück.





Das letzte Detail bleibt - um die Animation selbst zu beschreiben. Hierfür verwenden wir die Regel @keyframes



.





Spoiler
@keyframes glitch-anim {
 0% {
  opacity:1;
  -webkit-transform:translateZ(0);
  transform:translateZ(0);
  -webkit-clip-path:polygon(0 2%,100% 2%,100% 5%,0 5%);
  clip-path:polygon(0 2%,100% 2%,100% 5%,0 5%)
 }
 2% {
  -webkit-clip-path:polygon(0 78%,100% 78%,100% 100%,0 100%);
  clip-path:polygon(0 78%,100% 78%,100% 100%,0 100%);
  -webkit-transform:translate(-5px);
  transform:translate(-5px)
 }
 6% {
  -webkit-clip-path:polygon(0 78%,100% 78%,100% 100%,0 100%);
  clip-path:polygon(0 78%,100% 78%,100% 100%,0 100%);
  -webkit-transform:translate(5px);
  transform:translate(5px)
 }
 8% {
  -webkit-clip-path:polygon(0 78%,100% 78%,100% 100%,0 100%);
  clip-path:polygon(0 78%,100% 78%,100% 100%,0 100%);
  -webkit-transform:translate(-5px);
  transform:translate(-5px)
 }
 9% {
  -webkit-clip-path:polygon(0 78%,100% 78%,100% 100%,0 100%);
  clip-path:polygon(0 78%,100% 78%,100% 100%,0 100%);
  -webkit-transform:translate(0);
  transform:translate(0)
 }
 10% {
  -webkit-clip-path:polygon(0 54%,100% 54%,100% 44%,0 44%);
  clip-path:polygon(0 54%,100% 54%,100% 44%,0 44%);
  -webkit-transform:translate3d(5px,0,0);
  transform:translate3d(5px,0,0)
 }
 13% {
  -webkit-clip-path:polygon(0 54%,100% 54%,100% 44%,0 44%);
  clip-path:polygon(0 54%,100% 54%,100% 44%,0 44%);
  -webkit-transform:translateZ(0);
  transform:translateZ(0)
 }
 13.1% {
  -webkit-clip-path:polygon(0 0,0 0,0 0,0 0);
  clip-path:polygon(0 0,0 0,0 0,0 0);
  -webkit-transform:translate3d(5px,0,0);
  transform:translate3d(5px,0,0)
 }
 15% {
  -webkit-clip-path:polygon(0 60%,100% 60%,100% 40%,0 40%);
  clip-path:polygon(0 60%,100% 60%,100% 40%,0 40%);
  -webkit-transform:translate3d(5px,0,0);
  transform:translate3d(5px,0,0)
 }
 20% {
  -webkit-clip-path:polygon(0 60%,100% 60%,100% 40%,0 40%);
  clip-path:polygon(0 60%,100% 60%,100% 40%,0 40%);
  -webkit-transform:translate3d(-5px,0,0);
  transform:translate3d(-5px,0,0)
 }
 20.1% {
  -webkit-clip-path:polygon(0 0,0 0,0 0,0 0);
  clip-path:polygon(0 0,0 0,0 0,0 0);
  -webkit-transform:translate3d(5px,0,0);
  transform:translate3d(5px,0,0)
 }
 25% {
  -webkit-clip-path:polygon(0 85%,100% 85%,100% 40%,0 40%);
  clip-path:polygon(0 85%,100% 85%,100% 40%,0 40%);
  -webkit-transform:translate3d(5px,0,0);
  transform:translate3d(5px,0,0)
 }
 30% {
  -webkit-clip-path:polygon(0 85%,100% 85%,100% 40%,0 40%);
  clip-path:polygon(0 85%,100% 85%,100% 40%,0 40%);
  -webkit-transform:translate3d(-5px,0,0);
  transform:translate3d(-5px,0,0)
 }
 30.1% {
  -webkit-clip-path:polygon(0 0,0 0,0 0,0 0);
  clip-path:polygon(0 0,0 0,0 0,0 0)
 }
 35% {
  -webkit-clip-path:polygon(0 63%,100% 63%,100% 80%,0 80%);
  clip-path:polygon(0 63%,100% 63%,100% 80%,0 80%);
  -webkit-transform:translate(-5px);
  transform:translate(-5px)
 }
 40% {
  -webkit-clip-path:polygon(0 63%,100% 63%,100% 80%,0 80%);
  clip-path:polygon(0 63%,100% 63%,100% 80%,0 80%);
  -webkit-transform:translate(5px);
  transform:translate(5px)
 }
 45% {
  -webkit-clip-path:polygon(0 63%,100% 63%,100% 80%,0 80%);
  clip-path:polygon(0 63%,100% 63%,100% 80%,0 80%);
  -webkit-transform:translate(-5px);
  transform:translate(-5px)
 }
 50% {
  -webkit-clip-path:polygon(0 63%,100% 63%,100% 80%,0 80%);
  clip-path:polygon(0 63%,100% 63%,100% 80%,0 80%);
  -webkit-transform:translate(0);
  transform:translate(0)
 }
 55% {
  -webkit-clip-path:polygon(0 10%,100% 10%,100% 0,0 0);
  clip-path:polygon(0 10%,100% 10%,100% 0,0 0);
  -webkit-transform:translate3d(5px,0,0);
  transform:translate3d(5px,0,0)
 }
 60% {
  -webkit-clip-path:polygon(0 10%,100% 10%,100% 0,0 0);
  clip-path:polygon(0 10%,100% 10%,100% 0,0 0);
  -webkit-transform:translateZ(0);
  transform:translateZ(0);
  opacity:1
 }
 60.1% {
  -webkit-clip-path:polygon(0 0,0 0,0 0,0 0);
  clip-path:polygon(0 0,0 0,0 0,0 0);
  opacity:1
 }
 to {
  -webkit-clip-path:polygon(0 0,0 0,0 0,0 0);
  clip-path:polygon(0 0,0 0,0 0,0 0);
  opacity:1
 }
}
      
      







Erklärungen zum Code.





Die Animation ist prozentual in Teile aufgeteilt. Bestimmte Änderungen finden in jedem Teil statt.





Die Eigenschaft clip-path



schneidet ein Polygon aus dem Bild und wendet verschiedene Effekte darauf an. Beispiel: transform: translate(-5px)



- verschiebt das Polygon nach links und oben.





Sie können sehen, was hier passiert ist .








All Articles