<link href="//netdna.bootstrapcdn.com/bootstrap/3.0.0/css/bootstrap.min.css" rel="stylesheet" id="bootstrap-css">
<script src="//netdna.bootstrapcdn.com/bootstrap/3.0.0/js/bootstrap.min.js"></script>
<script src="//code.jquery.com/jquery-1.11.1.min.js"></script>
<!------ Include the above in your HEAD tag ---------->
<!DOCTYPE html><html class=''>
<head><script src='//production-assets.codepen.io/assets/editor/live/console_runner-079c09a0e3b9ff743e39ee2d5637b9216b3545af0de366d4b9aad9dc87e26bfd.js'></script><script src='//production-assets.codepen.io/assets/editor/live/events_runner-73716630c22bbc8cff4bd0f07b135f00a0bdc5d14629260c3ec49e5606f98fdd.js'></script><script src='//production-assets.codepen.io/assets/editor/live/css_live_reload_init-2c0dc5167d60a5af3ee189d570b1835129687ea2a61bee3513dee3a50c115a77.js'></script><meta charset='UTF-8'><meta name="robots" content="noindex"><link rel="shortcut icon" type="image/x-icon" href="//production-assets.codepen.io/assets/favicon/favicon-8ea04875e70c4b0bb41da869e81236e54394d63638a1ef12fa558a4a835f1164.ico" /><link rel="mask-icon" type="" href="//production-assets.codepen.io/assets/favicon/logo-pin-f2d2b6d2c61838f7e76325261b7195c27224080bc099486ddd6dccb469b8e8e6.svg" color="#111" /><link rel="canonical" href="https://codepen.io/TylerK/pen/Gpdqqq?depth=everything&order=popularity&page=7&q=react&show_forks=false" />
<link rel='stylesheet prefetch' href='https://cdnjs.cloudflare.com/ajax/libs/normalize/5.0.0/normalize.min.css'>
<style class="cp-pen-styles">html, body {
width: 100%;
height: 100%;
}
.center {
width: 100%;
height: 100%;
display: -webkit-box;
display: -ms-flexbox;
display: flex;
-webkit-box-pack: center;
-ms-flex-pack: center;
justify-content: center;
-webkit-box-align: center;
-ms-flex-align: center;
align-items: center;
}
.ph-wrapper {
position: relative;
-webkit-transition: all 0.3s ease-out;
transition: all 0.3s ease-out;
margin: 0;
}
.ph-wrapper .ph-shadow,
.ph-wrapper .ph-layers,
.ph-wrapper .ph-layer,
.ph-wrapper .ph-lighting {
-webkit-transition: all 0.3s ease-out;
transition: all 0.3s ease-out;
}
.ph-wrapper .ph-shadow,
.ph-wrapper .ph-layers,
.ph-wrapper .ph-layer,
.ph-wrapper .ph-lighting {
width: 100%;
height: 100%;
position: absolute;
top: 0;
left: 0;
bottom: 0;
right: 0;
}
.ph-wrapper img {
border-radius: 6px;
}
.ph-wrapper .ph-text > * {
margin: 0;
padding: 0;
width: 100%;
height: 100%;
display: -webkit-box;
display: -ms-flexbox;
display: flex;
-webkit-box-pack: center;
-ms-flex-pack: center;
justify-content: center;
-webkit-box-align: center;
-ms-flex-align: center;
align-items: center;
text-align: center;
font-size: 4rem;
color: #fff;
font-weight: 300;
}
.ph-wrapper .ph-shadow {
height: 90%;
width: 90%;
left: 5%;
top: 5%;
background: none;
}
.ph-wrapper .ph-lighting {
background-image: -webkit-linear-gradient(315deg, rgba(255, 255, 255, 0) 0%, rgba(255, 255, 255, 0) 33%);
background-image: linear-gradient(135deg, rgba(255, 255, 255, 0) 0%, rgba(255, 255, 255, 0) 33%);
}
.ph-wrapper .ph-heading {
margin: 0;
padding: 0;
color: #fff;
font-size: 3rem;
text-shadow: 0px 5px 10px rgba(0, 0, 0, 0.5);
}
</style></head><body>
<div class='center'>
<div id='app'></div>
</div>
<script src='//production-assets.codepen.io/assets/common/stopExecutionOnTimeout-b2a7b3fe212eaa732349046d8416e00a9dec26eb7fd347590fbced3ab38af52e.js'></script><script src='//cdnjs.cloudflare.com/ajax/libs/react/0.14.1/react.js'></script><script src='//cdnjs.cloudflare.com/ajax/libs/react/0.14.1/react-dom.js'></script>
<script >'use strict';
function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }
function _possibleConstructorReturn(self, call) { if (!self) { throw new ReferenceError("this hasn't been initialised - super() hasn't been called"); } return call && (typeof call === "object" || typeof call === "function") ? call : self; }
function _inherits(subClass, superClass) { if (typeof superClass !== "function" && superClass !== null) { throw new TypeError("Super expression must either be null or a function, not " + typeof superClass); } subClass.prototype = Object.create(superClass && superClass.prototype, { constructor: { value: subClass, enumerable: false, writable: true, configurable: true } }); if (superClass) Object.setPrototypeOf ? Object.setPrototypeOf(subClass, superClass) : subClass.__proto__ = superClass; }
var config = {
scale: 1.03, // How large to scale the item: 1.00 -> 1.10~
rotation: 0.3, // Rotation modifier: 0.1 (more) -> 0.5 (less)
alpha: 0.4, // Alpha channel modifer: 1.01 -> 1.1~
shadow: 8 // How much the shadow moves
};
var ParallaxHover = function (_React$Component) {
_inherits(ParallaxHover, _React$Component);
ParallaxHover.propTypes = function propTypes() {
return {
children: React.Proptypes.node.isRequired,
width: React.Proptypes.string.isRequired,
height: React.Proptypes.string.isRequired
};
};
function ParallaxHover(props) {
_classCallCheck(this, ParallaxHover);
var _this = _possibleConstructorReturn(this, _React$Component.call(this, props));
_this.state = {
rotateX: 0,
rotateY: 0,
shadowMovement: 20,
shadowSize: 50,
scale: 1,
angle: 0,
alpha: 0
};
return _this;
}
ParallaxHover.prototype.__buildState = function __buildState(rotateX, rotateY, shadowMovement, shadowSize, scale, angle, alpha) {
this.setState({
rotateX: rotateX,
rotateY: rotateY,
shadowMovement: shadowMovement,
shadowSize: shadowSize,
scale: scale,
angle: angle,
alpha: alpha
});
};
ParallaxHover.prototype.__buildTransformStrings = function __buildTransformStrings(rotateX, rotateY, scale) {
return {
WebkitTransform: 'perspective(1000px) scale(' + scale + ') rotateX(' + rotateX + 'deg) rotateY(' + rotateY + 'deg)',
MozTransform: 'perspective(1000px) scale(' + scale + ') rotateX(' + rotateX + 'deg) rotateY(' + rotateY + 'deg)',
transform: 'perspective(1000px) scale(' + scale + ') rotateX(' + rotateX + 'deg) rotateY(' + rotateY + 'deg)'
};
};
ParallaxHover.prototype.__calculateDistance = function __calculateDistance(bounds, offsetX, offsetY) {
var distanceX = Math.pow(offsetX - bounds.width / 2, 2);
var distanceY = Math.pow(offsetY - bounds.height / 2, 2);
return Math.floor(Math.sqrt(distanceX + distanceY));
};
ParallaxHover.prototype.__calculateAlphaFromCenter = function __calculateAlphaFromCenter(current) {
var max = Math.max(this.props.width, this.props.height);
return current / max * config.alpha;
};
ParallaxHover.prototype.__handleMouseMove = function __handleMouseMove(_ref) {
var pageX = _ref.pageX;
var pageY = _ref.pageY;
var nativeEvent = _ref.nativeEvent;
var width = this.props.width;
var height = this.props.height;
var _document$body = document.body;
var scrollTop = _document$body.scrollTop;
var scrollLeft = _document$body.scrollLeft;
var bounds = this.refs.wrapper.getBoundingClientRect();
var centerX = this.props.width / 2;
var centerY = this.props.height / 2;
var widthMultiplier = 320 / this.props.width;
var offsetX = 0.52 - (pageX - bounds.left - scrollLeft) / width;
var offsetY = 0.52 - (pageY - bounds.top - scrollTop) / height;
var deltaX = pageX - bounds.left - scrollLeft - centerX;
var deltaY = pageY - bounds.top - scrollTop - centerY;
var rotateX = (deltaY - offsetY) * (0.08 * widthMultiplier);
var rotateY = (offsetX - deltaX) * (0.04 * widthMultiplier);
var angleRad = Math.atan2(deltaY, deltaX);
var angleRaw = angleRad * 180 / Math.PI - 90;
var angleDeg = angleRaw < 0 ? angleRaw + 360 : angleRaw;
var distanceFromCenter = this.__calculateDistance(bounds, nativeEvent.offsetX, nativeEvent.offsetY);
var shadowMovement = centerY * 0.25;
var shadowSize = 120;
var alpha = this.__calculateAlphaFromCenter(distanceFromCenter);
this.__buildState(rotateX, rotateY, shadowMovement, shadowSize, config.scale, angleDeg, alpha);
};
ParallaxHover.prototype.__handleMouseLeave = function __handleMouseLeave() {
this.__buildState(0, 0, 20, 50, 1, 0, 0);
};
ParallaxHover.prototype.__renderChildren = function __renderChildren(children) {
var _this2 = this;
var st = this.state;
if (!Array.isArray(children)) {
var styles = this.__buildTransformStrings(st.rotateX, st.rotateY, st.scale);
console.log(this.__buildTransformStrings(st.rotateX, st.rotateY, st.scale));
return React.createElement(
'div',
{ style: styles, className: 'ph-layer' },
children
);
}
return children.map(function (layer, key) {
var num = key + 1;
var rotateX = Math.floor(st.rotateX / num);
var rotateY = Math.floor(st.rotateY / num);
var styles = _this2.__buildTransformStrings(rotateX, rotateY, st.scale);
var textClass = undefined;
if (layer.ref === 'text') {
textClass = 'ph-text';
var shadow = {
textShadow: rotateY * 0.5 + 'px ' + rotateX * 0.5 + 'px 10px rgba(0, 0, 0, 0.5)'
};
styles = Object.assign({}, shadow, styles);
}
return React.createElement(
'div',
{ style: styles, className: 'ph-layer ' + textClass, key: key },
layer
);
});
};
ParallaxHover.prototype.render = function render() {
var st = this.state;
var baseTransforms = this.__buildTransformStrings(st.rotateX, st.rotateY, st.scale);
var stylesWrapper = Object.assign({}, baseTransforms, {
width: this.props.width,
height: this.props.height
});
var stylesShadow = Object.assign({}, baseTransforms, {
boxShadow: '0px ' + st.shadowMovement + 'px ' + st.shadowSize + 'px rgba(0, 0, 0, 0.6)'
});
var stylesLighting = Object.assign({}, baseTransforms, {
backgroundImage: 'linear-gradient(' + st.angle + 'deg, rgba(255,255,255, ' + st.alpha + ') 0%, rgba(255,255,255,0) 40%)'
});
return React.createElement(
'div',
{ style: { transformStyle: 'preserve-3d' } },
React.createElement(
'figure',
{ ref: 'wrapper', className: 'ph-wrapper', style: stylesWrapper, onMouseMove: this.__handleMouseMove.bind(this), onMouseLeave: this.__handleMouseLeave.bind(this) },
React.createElement('div', { className: 'ph-shadow', style: stylesShadow }),
React.createElement(
'div',
{ className: 'ph-layers' },
this.__renderChildren(this.props.children)
),
React.createElement('div', { className: 'ph-lighting', style: stylesLighting })
)
);
};
return ParallaxHover;
}(React.Component);
var App = React.createClass({
displayName: 'App',
render: function render() {
return React.createElement(
'div',
null,
React.createElement(
ParallaxHover,
{ width: '500', height: '300' },
React.createElement('img', { ref: 'image', src: 'http://lorempixel.com/500/300/abstract/' }),
React.createElement(
'h1',
{ ref: 'text' },
'Parallax Hover'
)
)
);
}
});
var Node = document.getElementById('app');
ReactDOM.render(React.createElement(App), Node);
//# sourceURL=pen.js
</script>
</body></html>