1

I've been trying to teach myself some css animations with keyframes, and I'm trying to create something in which a small square drops down, then out of that square, a rentangle protrudes from the left, it then displays some text after 8 or so seconds and then the rentangle retreats back into the smaller square (to the right) and the smaller square retreats upwards into 'thin air'. If you're wondering what this is for it's an alert notification when someones follows me on Twitch TV while I livestream. Here is a JSFiddle of my efforts so far. For some reason on JSFiddle the content doesn't appear before the animation, however on the the alert service i use it does happen. I've linked their tester here, so you can see what I mean.

HTML CODE:

<html>

<head>
  <title>GR412 Twitch Follower Alert</title>
  <link href="Twitch\followeralert.css" rel="stylesheet" type="text/css" media="screen" />
</head>

<body>
  <div class="follower-container">

    <div class="left-square-container">

    </div>

    <div class="right-retangle-container">

      <div class="header">
        <span class='keyword name'>{name}</span> is now following you!
      </div>

    </div>

  </div>

</body>

</html>

CSS CODE:

@keyframes slideInFromAbove {
  0% {
    transform: translateY(-100%);
  }

  100% {
    transform: translateY(0);
  }
}

@keyframes slideInFromTheLeft {
  0% {
    transform: translateX(-100%);
  }

  100% {
    transform: translateX(0);
  }
}

@keyframes slideInFromBelow {
  0% {
    transform: translateY(0);
  }

  100% {
    transform: translateY(100%);
  }
}

@keyframes slideInFromTheRight {
  0% {
    transform: translateX(0);
  }

  100% {
    transform: translateX(100%);
  }
}

.follower-container {
  display: flex;
  font-family: 'Roboto';
  position: absolute;
  top: 50%;
  left: 50%;
  transform: translateX(-50%) translateY(-50%);
}

.left-square-container {
  width: 75px;
  height: 75px;
  background: #313131;
  animation: 1s 1s 1 slideInFromAbove;
}

.right-retangle-container {
  width: 500px;
  height: 75px;
  background: #212121;
  animation: 1s 2s 1 slideInFromTheLeft;
}

.header {
  font-size: 24px;
  color: #ffffff;
  text-align: center; /*vertical alignment of text*/
  position: relative; /*horizontal alignment of text*/
  top: 50%; /*horizontal alignment of text*/
  transform: translateY(-50%); /*horizontal alignment of text*/
  margin: 10px,
    10px,
    10px,
    10px; /*GOT TO HERE, THIS COULD BE CAUSING TWITCHING*/
}

.keyword:not(.user_message) {
  color: #0d47a1;
}

However there are some issues, first being that the content appears first, then does the animation. I would like it so you start with an empty screen and then the animation ensures that the square drops down first, then the rentangle protrudes from the square and finally the text is displayed. These three components should hold for 8 seconds then as already described another animation should hide each component in the order specified in the first paragraph.

The second issue is that when the rentangle protrudes, it doesn't do it from the right hand edge of the square, rather it does it from the left. So it overlaps the square, which ruins the effect.

I've based my code off this exsisting question:css3 transition animation on load?, which has helped a lot, but it doesn't help with my specifc needs.

Any help would be appreicated, and if something isn't clear let me know.

Note, if the second link doesn't work, let me know and i'll sort it.

Thanks, GR412.

uitwaa
  • 615
  • 1
  • 12
  • 24
SneakyShrike
  • 723
  • 1
  • 10
  • 31

1 Answers1

1

Issue 1: You need to set the styles of the initial placement for the content.

Issue 2: position: relative; z-index: /*some value*/ So you can properly layer the content.

You also need to use animation-fill-mode: forwards This sets the end styles to the end styles of @keyframes associate with it.

I've tweaked your timing. Here's a plnkr of it. Read the comments in the CSS

You end up having to calculate percentages. I would consider working out a calculation that can accept variables for scss/less/sass etc.

CSS comments:

/*
to calculate these percentages:

([seconds of portion of animation] x 100)/[total seconds of animation]

1)  slideInFromAbove starts
2)  slideInFromTheLeft starts
3)  slideInFromTheLeft ends
4)  slideInFromAbove ends


slideInFromAbove:  
  1)  slide down
  2)  hold 
  2)  slide up


slideInFromTheLeft:
  1)  slide right
  2)  hold
  3)  slide left


*/
Tamb
  • 748
  • 11
  • 20
  • This is exactly what i wanted, thanks a lot for the help. I just need to study the code now and do a bit of tinkering – SneakyShrike Jun 23 '17 at 09:28
  • Sorry to bother you, but i was wondering how you worked out the % values of both the slideInFromAbove keyframe and the slideInFromTheLeft keyframe. I've tried reading the comments for the actual css that uses the keyframes, but i'm not quite understanding the maths. Any clarififcation is appreicated. – SneakyShrike Jun 24 '17 at 10:48
  • 1
    It gets tricky with multiple animations. But the basic formula is: ( [time of portion of animation in seconds] × 100 ) ÷ [total animation time in seconds ]. I'm mobile rn, so I will comment the CSS later. But the idea is: A starts then B starts. Then B finishes then A finishes. A is going to be the longer animation. Figure out how long you want the portions of animation to be (.4s for the left square to slide in) then plug that .4 into the formula and there's your %. I will amend my answer to include this – Tamb Jun 24 '17 at 13:44
  • I see, I'm pretty awful at maths, but i should be be able to work this out eventully. Thanks again. – SneakyShrike Jun 26 '17 at 21:38
  • Hi again, i'm wondering if in your forumula the [time of portion of animation in seconds] would be .4s and the [total animation time in seconds ] would be 9.6 seconds? That would be the left square coming in from above. because i've tried doing .4 x 100 / 9.6 = 4.166666 which cleary isn't right. It needs to = 6% for the first part of the slideInFromAbove keyframe right? I'm still struggling to understand. Could you maybe do an example of how you did the actual percentages for the slideInFromAbove keyframe?. Sorry if this is hassle. – SneakyShrike Jul 09 '17 at 13:25
  • You calculated that correctly. I commented the css incorrectly The time is actually .57s. The idea is to punch in the time you want for that portion and use the percentage. But the actual time of that slide down is .57 seconds. Sorry – Tamb Jul 10 '17 at 14:01
  • Oh okay, and i'm wondering, is this not to work out `@keyframes slideInFromAbove { 0% { transform: translateY(-100%); } 6%{ transform: translateY(0); } 98%{ transform: translateY(0); } 100% { transform: translateY(-100%); } }` because i thought that was what the formula was for. Also how did you get .57?, is the timing not .4 anymore? Once again i'm sorry if i'm pestering you, but this is really confusing me. Also does this: `/* timing (~.57 duration + 8s hold + ~.2s removal of self + animation of right + removal of right) */` = 9.57 then it's rounded to 9.6? – SneakyShrike Jul 11 '17 at 10:40