Pen Settings

HTML

CSS

CSS Base

Vendor Prefixing

Add External Stylesheets/Pens

Any URLs 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 its URL and the proper URL extension.

+ 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

Auto Save

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="cont">
	<div class='btn'>
		<div class="btn__side btn__front">
			<div class="eyes">
				<div class="eyes__eye">
					<div class="eyes__pupil"></div>
				</div>
				<div class="eyes__eye">
					<div class="eyes__pupil"></div>
				</div>
			</div>
			<p class='eyes__text eyes__text--front'>
					Hi! I can do back & front flips. Click in different areas to see.
			</p>
		</div>
		<div class="btn__side btn__back">
			<p class='eyes__text eyes__text--back'>

			</p>
			<div class="eyes eyes--back">
				<div class="eyes__eye">
					<div class="eyes__pupil"></div>
				</div>
				<div class="eyes__eye">
					<div class="eyes__pupil"></div>
				</div>
			</div>
		</div>
		<div class="btn__side btn__right"></div>
		<div class="btn__side btn__left"></div>
		<div class="btn__side btn__top"></div>
		<div class="btn__side btn__bottom"></div>
	</div>
  <a href="https://twitter.com/mrspok407" target="_blank" class="icon-link icon-link--twitter">
    <img src="https://cdn1.iconfinder.com/data/icons/logotypes/32/twitter-128.png">
  </a>
</div>


              
            
!

CSS

              
                html {
  box-sizing: border-box;
	font-family: 'Open Sans', sans-serif;
}

*,
*:before,
*:after {
  box-sizing: inherit;
  margin: 0;
  padding: 0;
}

.cont {
	display: flex;
	justify-content: center;
	align-items: center;
	height: 100vh;
	background-image: linear-gradient(to left bottom, #F2E3C6 0%, #A7A1A5 100%);
	perspective: 2000px;
	perspective-origin: 55% 100px;
}

.btn {
	position: relative;
	transform-style: preserve-3d;
	background-color: peru;
	cursor: pointer;
	transform: translate3d(0, 0, 0);
	will-change: transform, transition;
		
	&__side {
		position: absolute;
		top: -100px;
		left: -160px;
		background-color: rgba(peru, 1);
		width: 350px;
		height: 200px;
		border: 1px solid rgba(black, .1);
	}
	&__front {
		transform: translateZ(100px);
		transition: 200ms;
	}
	&__top {
		height: 25px;
		transform: rotateX(-90deg) translateY(-100px);
		transform-origin: top center;
	}
	&__bottom {
		height: 25px;
		transform: rotateX(90deg) translateY(100px) translateZ(-175px);
		transform-origin: bottom center;
	}
	&__left {
		width: 25px;
		transform: rotateY(270deg) translateX(75px);
		transform-origin: center left;
	}
	&__right {
		width: 25px;
		transform: rotateY(-270deg) translateX(-75px) translateZ(325px);
		transform-origin: top right;
	}
	&__back {
		transform: translateZ(75px) rotateY(180deg);
		box-shadow: 0 0 20px 10px rgba(black, .5);
		transition: 200ms;
	}
}

.eyes {
	display: flex;
	justify-content: center;
	margin-top: 35px;
	&--back {
		margin-top: 35px;
	}
	&__eye {
		position: relative;
		display: flex;
		justify-content: center;
		align-items: center;
		width: 75px;
		height: 75px;
		border-radius: 50%;
		background-color: #fff;
	}
	&__pupil {
		transform: translate3d(0, 0, 0);
		width: 30px;
		height: 30px;
		background-color: #000;
		border-radius: 50%;
		will-change: transform, box-shadow;
	}
	&__text {
		margin-top: 15px;
		text-align: center;
		font-size: 18px;
		color: #fff;
		user-select: none; 
		
		&--back {
			font-size: 28px;
			transform: rotateX(180deg) rotateY(180deg);
		}
	}
}

.backflip {
	transition: 500ms !important;
	transform: rotateX(-181deg) rotateY(1deg) !important;
	.btn__back {
		box-shadow: none;
	}
	.btn__front {
		box-shadow: 0 0 20px 10px rgba(black, .5);
	}
}

.frontflip {
	transition: 500ms !important;
	transform: rotateX(181deg) rotateY(1deg) !important;
	.btn__back {
		box-shadow: none;
	}
	.btn__front {
		box-shadow: 0 0 20px 10px rgba(black, .5);
	}
}

.right-backflip {
	transition: 500ms !important;
	transform: rotateY(-45deg) rotateX(175deg) !important;
	.btn__back {
		box-shadow: none;
	}
	.btn__front {
		box-shadow: 0 0 20px 10px rgba(black, .5);
	}
}

.left-backflip {
	transition: 500ms !important;
	transform: rotateY(45deg) rotateX(175deg) !important;
	.btn__back {
		box-shadow: none;
	}
	.btn__front {
		box-shadow: 0 0 20px 10px rgba(black, .5);
	}
}

.left-frontflip {
	transition: 500ms !important;
	transform: rotateY(-45deg) rotateX(-175deg) !important;
	.btn__back {
		box-shadow: none;
	}
	.btn__front {
		box-shadow: 0 0 20px 10px rgba(black, .5);
	}
}

.right-frontflip {
	transition: 500ms !important;
	transform: rotateY(45deg) rotateX(-175deg) !important;
	.btn__back {
		box-shadow: none;
	}
	.btn__front {
		box-shadow: 0 0 20px 10px rgba(black, .5);
	}
}

.icon-link {
  position: absolute;
  left: 5px;
  bottom: 5px;
  width: 50px;
  img {
    width: 100%;
    vertical-align: top;
  }
  &--twitter {
    left: auto;
    right: 5px;
  }
}

              
            
!

JS

              
                const btn = document.querySelector('.btn');
const btn__front = document.querySelector('.btn__front');
const btn__back = document.querySelector('.btn__back');
const whoa = document.querySelector('.letters');
const eyes = document.querySelectorAll('.eyes__pupil');
const backTxt = document.querySelector('.eyes__text--back');

let animation = false;
let animSpd = .3;
let clickX, clickY, rotateYvalue, rotateXvalue;

const mouseMoveHandler = function(e) {
	if(animation) return;
	let x = e.clientX;
	let y = e.clientY;
	let btnOffsLeft = btn.offsetLeft;
	let btnOffsTop = btn.offsetTop;
	rotateYvalue = (x - btnOffsLeft) / 10;
	rotateXvalue = (y - btnOffsTop) / 10;
	clickY = x - btnOffsLeft;
	clickX = y - btnOffsTop;
	let that = this;
	
	if (that === btn__front) {
		TweenMax.to(btn, animSpd, {
			transform: `translate3d(0,0,0)
									rotateY(${rotateYvalue}deg)
									rotateX(${-rotateXvalue}deg)`
		});
	}
	
	eyesMovement(that, rotateYvalue, rotateXvalue);
}

const eyesMovement = (that, x, y) => {
	let eyesLength = eyes.length;
	for(let i = 0; i < eyesLength; i++) {
		if (that === btn__front) {
			TweenMax.to(eyes[i], .2, {
				transform: `translate3d(${x}px, ${y}px, 0)`,
				boxShadow: `${-x * 1.5}px ${-y * 1.5}px 10px -5px rgba(0, 0, 0, .5)`
		});
		}	else if (that === btn__back) {
			TweenMax.to(eyes[i], .2, {
				transform: `translate3d(${x * -1}px, ${y * -1}px, 0)`,
				boxShadow: `${-x * -1.5}px ${-y * -1.5}px 10px -5px rgba(0, 0, 0, .3)`
		});
		}
	}
}

const mouseClickHandler = function() {
	if(animation) return;
	animation = true;
	
	let backflip = clickY > -75 && clickY < 105 && clickX < 0;
	let frontflip = clickY > -75 && clickY < 105 && clickX > 0;
	let leftBackflip = clickY < -75 && clickX < 0;
	let rightBackflip = clickY > 105 && clickX < 0;
	let leftFrontflip = clickY < -75 && clickX > 0;
	let rightFrontflip = clickY > 105 && clickX > 0; 
	
	if (backflip) {
		btn.classList.add('backflip');
		backTxt.innerHTML = 'Whoa! Backflip';
	} else if (frontflip) {
		btn.classList.add('frontflip');
		backTxt.innerHTML = 'Whoa! Frontflip';
	} else if (rightFrontflip) {
		btn.classList.add('right-frontflip');
		backTxt.innerHTML = 'Whoa! Right Frontflip';
	} else if (leftFrontflip) {
		btn.classList.add('left-frontflip');
		backTxt.innerHTML = 'Whoa! Left Frontflip';
	} else if (leftBackflip) {
		btn.classList.add('left-backflip');
		backTxt.innerHTML = 'Whoa! Left Backflip';
	} else if (rightBackflip) {
		btn.classList.add('right-backflip');
		backTxt.innerHTML = 'Whoa! Right Backflip';
	}
	
	setTimeout(function() {
		animation = false;
		eyesMovement(btn__back, rotateYvalue, rotateXvalue);
	}, 100);
}

const toDefault = function() {
	if(animation) return;
	animation = true;
	
	btn.style.transition = '500ms';
	btn.classList.remove('backflip', 'frontflip', 'right-backflip', 'left-backflip', 'left-frontflip', 'right-frontflip');
	
	setTimeout(function() {
		eyesMovement(btn__front, rotateYvalue, rotateXvalue);
	}, 100);
	
	setTimeout(function() {
		btn.style.transition = '0ms';
		backTxt.innerHTML = '';
		animation = false;
	}, 500);
}

btn__front.addEventListener('mousemove', mouseMoveHandler);
btn__back.addEventListener('mousemove', mouseMoveHandler);
btn__front.addEventListener('click', mouseClickHandler);
btn__back.addEventListener('click', toDefault);

btn.addEventListener('mouseleave', function () {
	if(animation) return;
	TweenMax.to(btn, .2, {
		transform: `translate3d(0,0,0)
								rotateY(0deg)
								rotateX(0deg)`
	});
	for(let i = 0, eyesLength = eyes.length; i < eyesLength; i++) {
		TweenMax.to(eyes[i], .2, {
			transform: `translate3d(0, 0, 0)`,
			boxShadow: `none`
		});
	}
})
              
            
!
999px

Console