Pen Settings

HTML

CSS

CSS Base

Vendor Prefixing

Add External Stylesheets/Pens

Any URL's added here will be added as <link>s in order, and before the CSS in the editor. You can use the CSS from another Pen by using it's URL and the proper URL extention.

+ add another resource

JavaScript

Babel includes JSX processing.

Add External Scripts/Pens

Any URL's added here will be added as <script>s in order, and run before the JavaScript in the editor. You can use the URL of any other Pen and it will include the JavaScript from that Pen.

+ add another resource

Packages

Add Packages

Search for and use JavaScript packages from npm here. By selecting a package, an import statement will be added to the top of the JavaScript editor for this package.

Behavior

Save Automatically?

If active, Pens will autosave every 30 seconds after being saved once.

Auto-Updating Preview

If enabled, the preview panel updates automatically as you code. If disabled, use the "Run" button to update.

Format on Save

If enabled, your code will be formatted when you actively save your Pen. Note: your code becomes un-folded during formatting.

Editor Settings

Code Indentation

Want to change your Syntax Highlighting theme, Fonts and more?

Visit your global Editor Settings.

HTML

              
                <div class="div">
	<div>Use arrow left and right to go back and forth!</div>
	<br />
	<div>Main tween position: <span class="output">0</span></div>
	<div>Current target: <span class="output2">x</span></div>
	<div>timeScale: <span class="output3">1</span></div>
	<br />
	<div>key <span class="output4">x</span> pressed at time <span class="output5">x</span></div>
	<div><span class="output6">waiting...</span></div>
	<br />
	<div class="boxes">
		<div class="box red"></div>
		<div class="box">0</div>
		<div class="box">1</div>
		<div class="box">2</div>
		<div class="box">3</div>
		<div class="box">4</div>
		<div class="box">5</div>
		<div class="box">6</div>
		<div class="box">7</div>
		<div class="box">8</div>
		<div class="box">9</div>
	</div>
</div>
              
            
!

CSS

              
                
.box {
	z-index: 2;
	position: relative;
	
	width: 100px; height: 100px;
	background: fade(Crimson, 50%);
	
	display: flex;
	justify-content: center;
	align-items: center;
}

.box.red {
	z-index: 1;
	position: absolute; left: 0; top: 0;
	background: Crimson;
	color: white;
}

.boxes {
	position: relative;
	display: flex;
	justify-content: center;
	align-items: center;
}

html,body {
	width: 100%; height: 100%;
	background: #F7E98E; // #9CCDFE;
	overflow: hidden;
}
body {
	display: flex;
	justify-content: center;
	align-items: center;
}


              
            
!

JS

              
                const zoneW = 100,
	   totalStates = 9,
		tsIncrease = 1, // timescale increase factor after each successive input
		tl = new TimelineMax({paused: true});

tl.fromTo(".red", totalStates, { x: 0 }, { x: zoneW * totalStates, ease: Linear.easeNone });
// a linear tween that moves ".red" from the start to the end, taking 1sec/state

let targetState = 0,
	 currentTS = 1, // current timescale
	 currentPos = 0, // store the timeline's position when pressing a key
	 direction = 0, // 0 = paused, >0 = forwards, <0 = backwards
	 sub = null; // this will hold the tween that plays the main tl

$(window).on("keydown", function(e) {
	const key = e.which;
	currentPos = tl.time();
	
	if (key === 37 || key === 39) {
		// go to prev state
		if (key === 37) {
			// if we were going forwards, kill the sub tween and take the state just after if possible
			if (direction > 0) {
				killSubTween(false);
				targetState = Math.min(Math.ceil(currentPos), totalStates);
			}
			// otherwise decrease the target state if possible
			else {
				targetState = Math.max(targetState - 1, 0);
			}
			
			$(".output4").html("&larr;");
		}

		// go to next state
		else {
			// same logic as above
			if (direction < 0) {
				killSubTween(false);
				targetState = Math.max(Math.floor(currentPos), 0);
			}
			else {
				targetState = Math.min(targetState + 1, totalStates);
			}
			
			$(".output4").html("&rarr;");
		}
		
		direction = targetState - currentPos;

		if (!sub) { // if we weren't tweening, reset the timescale
			tl.timeScale(currentTS = 1);
		} else { // otherwise kill the current tween and increase the timescale
			killSubTween(false);
			tl.timeScale(currentTS = currentTS + tsIncrease);
		}

		sub = tl.tweenTo(targetState, {ease: Power2.easeOut, onComplete: killSubTween}); // make a new tween that moves the main timeline to the new target
	}
	
	updateOutput();
})

function killSubTween(resetDirection = true) {
	sub.pause().kill();
	sub = null;
	if (resetDirection) { direction = 0; }
	updateOutput();
}

function updateOutput() {
	$(".output2").text(targetState);
	$(".output3").text(currentTS);
	$(".output5").text(currentPos);
	$(".output6").text(
		direction === 0 ?
			"waiting..." :
			"moving " + (direction > 0 ?
				"forwards" :
				"backwards")
	);
}

tl.eventCallback("onUpdate", function() {
	$(".output").text(Number(tl.time()).toFixed(2));
})
              
            
!
999px

Console