Jason Axelrod Jason Axelrod - 3 months ago 5
CSS Question

Animate a div from offscreen, without absolute positions?

I have a scoreboard... This scoreboard is a series of inline-block divs to keep it horizontal:

<div class="top">

<div class="score1 color0 inline slow">
<div class="inner color3 slow">
<div class="title">0</div>
</div>
</div>

<div class="player1 color0 inline slow">
<div class="inner color2 slow">
<div class="title">KDZaster</div>
</div>
</div>

<div class="round color0 inline slow">
<div class="inner color1 slow">
<div class="title">Grand Finals</div>
</div>
</div>

<div class="player2 color0 inline slow">
<div class="inner color2 slow">
<div class="title">DarthArma</div>
</div>
</div>

<div class="score2 color0 inline slow">
<div class="inner color3 slow">
<div class="title">0</div>
</div>
</div>

</div>


I have included a jsFiddle: https://jsfiddle.net/Lsxhmky7/

This page has a whole bunch of animations that I am doing with both CSS and jQuery (which are not included in the jsFiddle)... I've got all the animations working, except one.

One of the things I would like to do is so that the two score arrows on the left and right sides slide in from the edges of the screen.

I know I can do this with absolute positioning, but this grid doesn't have anything else in absolute positions, and I don't want it to be. I want it to be inline so that if widths of certain elements have to change, the page moves with it.

Is there a way I can animate the moving of these two elements from offscreen, while maintaining the inline positioning of all other blocks?

Answer

Fairly simple with transform: translate();

Move the arrow offscreen by default using transformX, then animate it moving back into position.

animation: slideInLeft 2s 1 forwards;

animation: [name] [length] [iterations] [end]

This runs the animation only once, for 2 seconds then stops at the end state.

.score1 {
    width: var(--score-width);
    -webkit-clip-path: polygon(
        0 0,
        0 calc(var(--main-height) / 2),
        0 var(--main-height),
        calc(var(--score-width) - var(--arrow-width)) var(--main-height),
        var(--score-width) calc(var(--main-height) / 2),
        calc(var(--score-width) - var(--arrow-width)) 0
    );
  transform: translateX(-100px);
  animation: slideInLeft 2s 1 forwards;
}

@keyframes slideInLeft {
  from { transform: translate(-100px); }
  to { transform: translateX(0); }
}

Reverse for the right arrow.

:root {
	--main-color0: rgba(000,000,000,0.5);
	--main-color1: rgba(255,000,000,0.5);
	--main-color2: rgba(000,255,255,0.5);
	--main-color3: rgba(255,255,255,0.5);
	
	--main-height: 50px;
	--main-gutter: -10px;
	--main-padding: 10px;
	--main-border: 4px;
	
	--font-family: 'Franklin Gothic';
	--font-color: #FFFFFF;
	--font-large: 24px;
	--font-small: 14px;
	--font-shadow: 1px 1px 0 #000, 1px -1px 0 #000, -1px 1px 0 #000, -1px -1px 0 #000;

	--arrow-width: 15px;
	--round-width: 180px;
	--player-width: 300px;
	--score-width: 50px;
	
	--out-speed: 0.5s;
	--in-speed: 2s;
}

body {
	color: var(--font-color);
	font-family: var(--font-family);
	font-size: var(--font-large);
	text-shadow: var(--font-shadow);
	line-height: var(--main-height);

	margin: 10px 0;
	text-align: center;
	background-color: #ccc;
}

.slow { transition: all var(--in-speed) ease; }
.fast { transition: all var(--out-speed) linear; }
.inline { display: inline-block; vertical-align: middle; margin: 0 -7px; }
.inner { -webkit-clip-path: inset(var(--main-border) 0); }

.color0 { background-color: var(--main-color0); }
.color1 { background-color: var(--main-color1); }
.color2 { background-color: var(--main-color2); }
.color3 { background-color: var(--main-color3); }

.title {
	overflow: hidden;
	text-overflow: ellipsis;
	white-space: nowrap;
	padding: 0 10px;
}

.round {
	font-size: var(--font-small);
	text-transform: uppercase;
	width: var(--round-width);
	-webkit-clip-path: polygon(
		var(--arrow-width) 0,
		0 calc(var(--main-height) / 2),
		var(--arrow-width) var(--main-height),
		calc(var(--round-width) - var(--arrow-width)) var(--main-height),
		var(--round-width) calc(var(--main-height) / 2),
		calc(var(--round-width) - var(--arrow-width)) 0
	);
}

.player1 {
	width: var(--player-width);
	-webkit-clip-path: polygon(
		0 0,
		var(--arrow-width) calc(var(--main-height) / 2),
		0 var(--main-height),
		var(--player-width) var(--main-height),
		calc(var(--player-width) - var(--arrow-width)) calc(var(--main-height) / 2),
		var(--player-width) 0
	);
}
.player2 {
	width: var(--player-width);
	-webkit-clip-path: polygon(
		0 0,
		var(--arrow-width) calc(var(--main-height) / 2),
		0 var(--main-height),
		var(--player-width) var(--main-height),
		calc(var(--player-width) - var(--arrow-width)) calc(var(--main-height) / 2),
		var(--player-width) 0
	);
}
.player1 .title { padding: 0 20px; text-align: right; }
.player2 .title { padding: 0 20px; text-align: left; }



.score1 {
	width: var(--score-width);
	-webkit-clip-path: polygon(
		0 0,
		0 calc(var(--main-height) / 2),
		0 var(--main-height),
		calc(var(--score-width) - var(--arrow-width)) var(--main-height),
		var(--score-width) calc(var(--main-height) / 2),
		calc(var(--score-width) - var(--arrow-width)) 0
	);
  transform: translateX(-100px);
  animation: slideInLeft 2s 1 forwards;
}

@keyframes slideInLeft {
  from { transform: translate(-100px); }
  to { transform: translateX(0); }
}
.score2 {
	width: var(--score-width);
	-webkit-clip-path: polygon(
		var(--arrow-width) 0,
		0 calc(var(--main-height) / 2),
		var(--arrow-width) var(--main-height),
		var(--score-width) var(--main-height),
		var(--score-width) calc(var(--main-height) / 2),
		var(--score-width) 0
	);
}

.score1 .title { padding: 0 20px 0 0; text-align: right; }
.score2 .title { padding: 0 0 0 20px; text-align: left; }
	<div class="top">
		
		<div class="score1 color0 inline slow">
			<div class="inner color3 slow">
				<div class="title">0</div>
			</div>
		</div>
		
		<div class="player1 color0 inline slow">
			<div class="inner color2 slow">
				<div class="title">KDZaster</div>
			</div>
		</div>
		
		<div class="round color0 inline slow">
			<div class="inner color1 slow">
				<div class="title">Grand Finals</div>
			</div>
		</div>
		
		<div class="player2 color0 inline slow">
			<div class="inner color2 slow">
				<div class="title">DarthArma</div>
			</div>
		</div>
		
		<div class="score2 color0 inline slow">
			<div class="inner color3 slow">
				<div class="title">0</div>
			</div>
		</div>
	
	</div>

Comments