LOADING...

Preview

Pen ID
Unlock Campus Themeforest adv

 

Code

3D Parallax Buttons

Observe the 3D effect as you scroll around. The larger the monitor you use, the bigger the effect you'll be able to see.

Observe all the sides of the buttons here as you scroll down.

The effect is noticable but not distracting.

Looks great on iOS and Android devices as well.

The JavaScript won't be a performance hit.

However, having a hundred 3D transformed objects will most likely slow down mobile devices and weaker computers so use them wisely.

This title doesn't need JS, just like the <hr>s down below!

You can use this technique for both inline links and important call to action buttons.

Combining different types also seems to work well:



CSS
/* button variables */
html {
  height: 100%;
  overflow: hidden;
  background: #777;
}

/* the perspective set here is the most important declaration for the 3d look of the buttons (values over 1000px make the buttons look almost flat) */
body {
  margin: 0;
  height: 100%;
  overflow-y: scroll;
  font-family: sans-serif;
  font-size: 16px;
  perspective: 350px;
}

div {
  width: 600px;
  margin: 0 auto;
  padding: 20px 20px 600px;
  background: #fff;
  transform-style: preserve-3d;
}

h1, h2 {
  text-align: center;
}

h1, h2, hr {
  position: relative;
  background: #444;
  color: #fff;
  line-height: 2em;
  margin-left: -50px;
  margin-right: -50px;
  transform-style: preserve-3d;
  transform: translateZ(1px);
}
h1::before, h1::after, h2::before, h2::after, hr::before, hr::after {
  content: "";
  position: absolute;
  top: 0;
  width: 50px;
  height: 100%;
  background: #2b2b2b;
}
h1::before, h2::before, hr::before {
  left: -50px;
  transform-origin: 100% 0%;
  transform: rotateY(-130deg);
}
h1::after, h2::after, hr::after {
  right: -50px;
  transform-origin: 0% 0%;
  transform: rotateY(130deg);
}

hr {
  border: 0;
  height: 10px;
  background: #444;
}

button {
  position: relative;
  display: inline-block;
  padding: 4px 16px;
  border: 0;
  min-width: 48px;
  margin: 10px;
  font-size: 24px;
  font-weight: bold;
  background-color: #aad1ee;
  background-image: radial-gradient(ellipse at top, rgba(255, 255, 255, 0.15) 50%, rgba(0, 0, 0, 0));
  color: #222;
  transform-style: preserve-3d;
  transform: translateZ(18px);
  cursor: pointer;
  user-select: none;
  /* double colon syntax for pseudo elements because IE8 doesn't need to see them (no transforms) */
  /* pushing the button down; done with animations with steps because transitions are too buggy and jumpy */
}
button:focus {
  outline: none;
}
button::before, button::after {
  position: absolute;
  width: 100%;
  height: 100%;
  left: 0;
  top: 0;
  background: #4098d9;
  transform-origin: 0% 0%;
}
button::before {
  content: "";
  height: 18px;
  background: #6aafe1;
  transform: rotateX(-90deg);
}
button::after {
  content: "";
  width: 18px;
  transform: rotateY(90deg);
}
button.top-half::before {
  top: auto;
  bottom: 0;
  background: #267ebf;
  transform-origin: 100% 100%;
  transform: rotateX(90deg);
}
button.left-half::after {
  left: auto;
  right: 0;
  transform-origin: 100% 100%;
  transform: rotateY(-90deg);
}
button:active {
  animation: button 80ms steps(20) both;
}
button:active::before {
  animation: buttonbefore 80ms steps(20) both;
}
button:active::after {
  animation: buttonafter 80ms steps(20) both;
}

@keyframes button {
  to {
    transform: translateZ(9px);
  }
}
@keyframes buttonbefore {
  to {
    height: 9px;
  }
}
@keyframes buttonafter {
  to {
    width: 9px;
  }
}
/* hide pseudo elements in IE9 and IE10 (no 3D transforms) */
@media screen and (min-width: 0\0) {
  button::before, button::after {
    display: none;
  }
}
/* hide pseudo elements in non-webkit Opera) */
x:-o-prefocus, button::before, button::after {
  display: none;
}
JS
// changing the sides of the button only on load and on browser resize
$(window).on("load resize", function() {
	topHalf();
	leftHalf();
});

// when scrolling vertically only top and bottom sides need to be recalculated
$("body").scroll(function() {
	topHalf();
});

// toggle button class if it's in the top or bottom half of the screen
function topHalf() {
	$("button").each(function() {
		var self = $(this),
        offTop = self.offset().top,
        scrTop = $(window).scrollTop(),
        halfWindowHeight = ($(window).height())/2;

		self.toggleClass("top-half", (offTop - scrTop) < halfWindowHeight);
	});
}

// toggle button class if it's in left or right half of the screen
function leftHalf() {
	$("button").each(function() {
		var self = $(this),
        offLeft = self.offset().left,
        halfWindowWidth = ($(window).width())/2;

		self.toggleClass("left-half", offLeft < halfWindowWidth);
	});
}
Host Instantly Drag and Drop Website Builder

 

Description

3D pushable buttons with a real perspective and a parallax-like effect when scrolling. Made with CSS 3D transforms.
Term
Mon, 11/27/2017 - 21:37

Related Codes

Pen ID
Pen ID
Pen ID